Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Make Wallet Mist compatible #3282
Browse files Browse the repository at this point in the history
  • Loading branch information
ngotchac committed Dec 9, 2016
1 parent ffd8314 commit 837caac
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 4 deletions.
77 changes: 75 additions & 2 deletions js/src/util/wallets.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see <http://www.gnu.org/licenses/>.

import { range } from 'lodash';
import { range, uniq } from 'lodash';

import { bytesToHex, toHex } from '~/api/util/format';
import { validateAddress } from '~/util/validation';

export default class WalletsUtils {

Expand All @@ -26,10 +27,82 @@ export default class WalletsUtils {

static fetchOwners (walletContract) {
const walletInstance = walletContract.instance;

return walletInstance
.m_numOwners.call()
.then((mNumOwners) => {
return Promise.all(range(mNumOwners.toNumber()).map((idx) => walletInstance.getOwner.call({}, [ idx ])));
const promises = range(mNumOwners.toNumber())
.map((idx) => walletInstance.getOwner.call({}, [ idx ]));

return Promise
.all(promises)
.then((owners) => {
const uniqOwners = uniq(owners);

// If all owners are the zero account : must be Mist wallet contract
if (uniqOwners.length === 1 && /^(0x)?0*$/.test(owners[0])) {
return WalletsUtils.fetchMistOwners(walletContract, mNumOwners.toNumber());
}

return owners;
});
});
}

static fetchMistOwners (walletContract, mNumOwners) {
const walletAddress = walletContract.address;

return WalletsUtils
.getMistOwnersOffset(walletContract)
.then((result) => {
if (!result || result.offset === -1) {
return [];
}

const owners = [ result.address ];

if (mNumOwners === 1) {
return owners;
}

const initOffset = result.offset + 1;
let promise = Promise.resolve();

range(initOffset, initOffset + mNumOwners - 1).forEach((offset) => {
promise = promise
.then(() => {
return walletContract.api.eth.getStorageAt(walletAddress, offset);
})
.then((result) => {
const resultAddress = '0x' + (result || '').slice(-40);
const { address } = validateAddress(resultAddress);

owners.push(address);
});
});

return promise.then(() => owners);
});
}

static getMistOwnersOffset (walletContract, offset = 3) {
return walletContract.api.eth
.getStorageAt(walletContract.address, offset)
.then((result) => {
if (result && !/^(0x)?0*$/.test(result)) {
const resultAddress = '0x' + result.slice(-40);
const { address, addressError } = validateAddress(resultAddress);

if (!addressError) {
return { offset, address };
}
}

if (offset >= 100) {
return { offset: -1 };
}

return WalletsUtils.getMistOwnersOffset(walletContract, offset + 1);
});
}

Expand Down
4 changes: 2 additions & 2 deletions js/src/views/Wallet/Details/details.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ export default class WalletDetails extends Component {
return null;
}

const ownersList = owners.map((address) => (
const ownersList = owners.map((address, idx) => (
<InputAddress
key={ address }
key={ `${idx}_${address}` }
value={ address }
disabled
text
Expand Down

0 comments on commit 837caac

Please sign in to comment.