Skip to content

Commit

Permalink
wallet: derive all keys in generateNonce for multisig
Browse files Browse the repository at this point in the history
  • Loading branch information
rithvikvibhu committed Sep 15, 2022
1 parent fb5501c commit 5e5ab24
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1215,9 +1215,13 @@ class Wallet extends EventEmitter {
const lo = value >>> 0;
const index = (hi ^ lo) & 0x7fffffff;

const {publicKey} = account.accountKey.derive(index);
const publicKeys = [];
for (const accountKey of [account.accountKey, ...account.keys]) {
publicKeys.push(accountKey.derive(index).publicKey);
}
publicKeys.sort(Buffer.compare);

return blake2b.multi(address.hash, publicKey, nameHash);
return blake2b.multi(address.hash, publicKeys[0], nameHash);
}

/**
Expand Down
74 changes: 74 additions & 0 deletions test/wallet-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3273,4 +3273,78 @@ describe('Wallet', function() {
assert.strictEqual(bal.clocked, secondHighest);
});
});

describe('Wallet nonce generation', function () {
const name = 'random-name';
const nameHash = rules.hashString(name);
const value = 1e6;

before(async () => {
await wdb.open();
});

after(async () => {
await wdb.close();
});

it('should generate nonce (pubkeyhash account)', async () => {
const wallet = await wdb.create({
master: KEY1
});
const addr = await wallet.receiveAddress();
assert.strictEqual(
addr.toString(network),
'hs1qyz8n88g6v5033r6fyj4jxkz29mqk5dgkdv7md7'
);

const nonce = await wallet.generateNonce(nameHash, addr, value);
assert.bufferEqual(
nonce,
Buffer.from(
'63d757a0e55b99db90b47fbfae81e3779801ec4d390c021ee30eafb6108e08ad',
'hex'
)
);
});

it('should generate nonce (multisig accounts)', async () => {
const alice = await wdb.create({
master: KEY1,
type: 'multisig',
m: 2,
n: 2
});
const bob = await wdb.create({
master: KEY2,
type: 'multisig',
m: 2,
n: 2
});

// Initialize both multisig wallets
await alice.addSharedKey(0, await bob.accountKey(0));
await bob.addSharedKey(0, await alice.accountKey(0));

// P2SH address common for alice and bob
const addr = await alice.receiveAddress();
assert.strictEqual(
addr.toString(network),
'hs1q8nnrenk92qdmm32zfv56shvhj6n6g8kq3ermpz50k5zxh4qjq34qk7v4d2'
);

// Generate nonce for same inputs from both wallets
const aliceNonce = await alice.generateNonce(nameHash, addr, value);
const bobNonce = await bob.generateNonce(nameHash, addr, value);

// Same nonce is generated by alice and bob
assert.bufferEqual(aliceNonce, bobNonce);
assert.bufferEqual(
aliceNonce,
Buffer.from(
'3c6fcdeca8a5c4cd76cb84af2fbc47737f6a0dc1d135a9aefb20786944ea081a',
'hex'
)
);
});
});
});

0 comments on commit 5e5ab24

Please sign in to comment.