|
1 | 1 | import * as assert from 'assert'; |
2 | 2 | import 'should'; |
3 | | -import { getDerivationPath } from '@bitgo/sdk-lib-mpc'; |
| 3 | +import { deriveUnhardenedMps, getDerivationPath } from '@bitgo/sdk-lib-mpc'; |
| 4 | +import { Ecdsa } from '../../../../../src/account-lib/mpc'; |
4 | 5 |
|
5 | 6 | function getAddressVerificationModule() { |
6 | 7 | return require('../../../../../src/bitgo/utils/tss/addressVerification'); |
7 | 8 | } |
8 | 9 |
|
9 | 10 | const getExtractCommonKeychain = () => getAddressVerificationModule().extractCommonKeychain; |
| 11 | +const getVerifyEddsaTssWalletAddress = () => getAddressVerificationModule().verifyEddsaTssWalletAddress; |
| 12 | +const getVerifyMPCWalletAddress = () => getAddressVerificationModule().verifyMPCWalletAddress; |
| 13 | + |
| 14 | +// RFC 8032 test vector: known valid Ed25519 public key + arbitrary chaincode = 128 hex chars. |
| 15 | +const TEST_PK = 'd75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a'; |
| 16 | +const TEST_CHAINCODE = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'; |
| 17 | +const TEST_KEYCHAIN = TEST_PK + TEST_CHAINCODE; |
| 18 | + |
| 19 | +// secp256k1 generator point G (compressed, 33 bytes = 66 hex) + same chaincode = 130 hex chars. |
| 20 | +const ECDSA_PK = '0279be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'; |
| 21 | +const ECDSA_KEYCHAIN = ECDSA_PK + TEST_CHAINCODE; |
10 | 22 |
|
11 | 23 | describe('TSS Address Verification - Derivation Path with Prefix', function () { |
12 | 24 | const commonKeychain = |
@@ -61,3 +73,174 @@ describe('TSS Address Verification - Derivation Path with Prefix', function () { |
61 | 73 | }); |
62 | 74 | }); |
63 | 75 | }); |
| 76 | + |
| 77 | +describe('verifyEddsaTssWalletAddress', function () { |
| 78 | + const keychains = [ |
| 79 | + { commonKeychain: TEST_KEYCHAIN }, |
| 80 | + { commonKeychain: TEST_KEYCHAIN }, |
| 81 | + { commonKeychain: TEST_KEYCHAIN }, |
| 82 | + ]; |
| 83 | + const isValidAddress = (addr: string) => addr.length === 64; |
| 84 | + const getAddressFromPublicKey = (pk: string) => pk; |
| 85 | + |
| 86 | + describe('MPCv2 wallets (Silence Labs / MPS formula)', function () { |
| 87 | + it('verifies a correct address derived with deriveUnhardenedMps at index 0', async function () { |
| 88 | + const verifyEddsaTssWalletAddress = getVerifyEddsaTssWalletAddress(); |
| 89 | + const expectedAddress = deriveUnhardenedMps(TEST_KEYCHAIN, 'm/0').slice(0, 64); |
| 90 | + |
| 91 | + const result = await verifyEddsaTssWalletAddress( |
| 92 | + { address: expectedAddress, keychains, index: 0, multisigTypeVersion: 'MPCv2' }, |
| 93 | + isValidAddress, |
| 94 | + getAddressFromPublicKey |
| 95 | + ); |
| 96 | + result.should.be.true(); |
| 97 | + }); |
| 98 | + |
| 99 | + it('verifies a correct address derived with deriveUnhardenedMps at index 1', async function () { |
| 100 | + const verifyEddsaTssWalletAddress = getVerifyEddsaTssWalletAddress(); |
| 101 | + const expectedAddress = deriveUnhardenedMps(TEST_KEYCHAIN, 'm/1').slice(0, 64); |
| 102 | + |
| 103 | + const result = await verifyEddsaTssWalletAddress( |
| 104 | + { address: expectedAddress, keychains, index: 1, multisigTypeVersion: 'MPCv2' }, |
| 105 | + isValidAddress, |
| 106 | + getAddressFromPublicKey |
| 107 | + ); |
| 108 | + result.should.be.true(); |
| 109 | + }); |
| 110 | + |
| 111 | + it('rejects an address derived at a different index', async function () { |
| 112 | + const verifyEddsaTssWalletAddress = getVerifyEddsaTssWalletAddress(); |
| 113 | + const addressFromIndex0 = deriveUnhardenedMps(TEST_KEYCHAIN, 'm/0').slice(0, 64); |
| 114 | + |
| 115 | + const result = await verifyEddsaTssWalletAddress( |
| 116 | + { address: addressFromIndex0, keychains, index: 1, multisigTypeVersion: 'MPCv2' }, |
| 117 | + isValidAddress, |
| 118 | + getAddressFromPublicKey |
| 119 | + ); |
| 120 | + result.should.be.false(); |
| 121 | + }); |
| 122 | + |
| 123 | + it('rejects a random address that was not derived from the keychain', async function () { |
| 124 | + const verifyEddsaTssWalletAddress = getVerifyEddsaTssWalletAddress(); |
| 125 | + const randomAddress = 'ab'.repeat(32); // 64 hex chars, wrong address |
| 126 | + |
| 127 | + const result = await verifyEddsaTssWalletAddress( |
| 128 | + { address: randomAddress, keychains, index: 0, multisigTypeVersion: 'MPCv2' }, |
| 129 | + isValidAddress, |
| 130 | + getAddressFromPublicKey |
| 131 | + ); |
| 132 | + result.should.be.false(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('verifies a correct MPCv2 address for SMC wallet using derivedFromParentWithSeed', async function () { |
| 136 | + const verifyEddsaTssWalletAddress = getVerifyEddsaTssWalletAddress(); |
| 137 | + const seed = 'smc-seed-123'; |
| 138 | + const prefix = getDerivationPath(seed); |
| 139 | + const expectedAddress = deriveUnhardenedMps(TEST_KEYCHAIN, `${prefix}/0`).slice(0, 64); |
| 140 | + |
| 141 | + const result = await verifyEddsaTssWalletAddress( |
| 142 | + { |
| 143 | + address: expectedAddress, |
| 144 | + keychains, |
| 145 | + index: 0, |
| 146 | + multisigTypeVersion: 'MPCv2', |
| 147 | + derivedFromParentWithSeed: seed, |
| 148 | + }, |
| 149 | + isValidAddress, |
| 150 | + getAddressFromPublicKey |
| 151 | + ); |
| 152 | + result.should.be.true(); |
| 153 | + }); |
| 154 | + |
| 155 | + it('rejects an MPCv2 address derived at the wrong index when derivedFromParentWithSeed is set', async function () { |
| 156 | + const verifyEddsaTssWalletAddress = getVerifyEddsaTssWalletAddress(); |
| 157 | + const seed = 'smc-seed-123'; |
| 158 | + const prefix = getDerivationPath(seed); |
| 159 | + const addressAtIndex1 = deriveUnhardenedMps(TEST_KEYCHAIN, `${prefix}/1`).slice(0, 64); |
| 160 | + |
| 161 | + const result = await verifyEddsaTssWalletAddress( |
| 162 | + { |
| 163 | + address: addressAtIndex1, |
| 164 | + keychains, |
| 165 | + index: 0, |
| 166 | + multisigTypeVersion: 'MPCv2', |
| 167 | + derivedFromParentWithSeed: seed, |
| 168 | + }, |
| 169 | + isValidAddress, |
| 170 | + getAddressFromPublicKey |
| 171 | + ); |
| 172 | + result.should.be.false(); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('non-MPCv2 wallets (MPCv1 formula)', function () { |
| 177 | + it('rejects an MPCv2-derived address when multisigTypeVersion is not set', async function () { |
| 178 | + const verifyEddsaTssWalletAddress = getVerifyEddsaTssWalletAddress(); |
| 179 | + // MPCv2 (Silence Labs) and MPCv1 formulas produce different addresses for the same keychain. |
| 180 | + // Without multisigTypeVersion: 'MPCv2', the MPCv1 formula is used, so the MPCv2-derived |
| 181 | + // address should not match. |
| 182 | + const mpcv2Address = deriveUnhardenedMps(TEST_KEYCHAIN, 'm/0').slice(0, 64); |
| 183 | + |
| 184 | + const result = await verifyEddsaTssWalletAddress( |
| 185 | + { address: mpcv2Address, keychains, index: 0 }, |
| 186 | + isValidAddress, |
| 187 | + getAddressFromPublicKey |
| 188 | + ); |
| 189 | + |
| 190 | + result.should.be.false(); |
| 191 | + }); |
| 192 | + }); |
| 193 | +}); |
| 194 | + |
| 195 | +describe('verifyMPCWalletAddress - ECDSA (secp256k1)', function () { |
| 196 | + const ecdsaKeychains = [ |
| 197 | + { commonKeychain: ECDSA_KEYCHAIN }, |
| 198 | + { commonKeychain: ECDSA_KEYCHAIN }, |
| 199 | + { commonKeychain: ECDSA_KEYCHAIN }, |
| 200 | + ]; |
| 201 | + // secp256k1 compressed public key is 33 bytes = 66 hex chars |
| 202 | + const isValidEcdsaAddress = (addr: string) => addr.length === 66; |
| 203 | + const getAddressFromPublicKey = (pk: string) => pk; |
| 204 | + |
| 205 | + it('ignores multisigTypeVersion MPCv2 and uses Ecdsa derivation for secp256k1 wallets', async function () { |
| 206 | + const verifyMPCWalletAddress = getVerifyMPCWalletAddress(); |
| 207 | + const expectedAddress = new Ecdsa().deriveUnhardened(ECDSA_KEYCHAIN, 'm/0').slice(0, 66); |
| 208 | + |
| 209 | + const result = await verifyMPCWalletAddress( |
| 210 | + { |
| 211 | + address: expectedAddress, |
| 212 | + keychains: ecdsaKeychains, |
| 213 | + index: 0, |
| 214 | + multisigTypeVersion: 'MPCv2', |
| 215 | + keyCurve: 'secp256k1', |
| 216 | + }, |
| 217 | + isValidEcdsaAddress, |
| 218 | + getAddressFromPublicKey |
| 219 | + ); |
| 220 | + result.should.be.true(); |
| 221 | + }); |
| 222 | + |
| 223 | + it('verifies a correct secp256k1 address at index 1', async function () { |
| 224 | + const verifyMPCWalletAddress = getVerifyMPCWalletAddress(); |
| 225 | + const expectedAddress = new Ecdsa().deriveUnhardened(ECDSA_KEYCHAIN, 'm/1').slice(0, 66); |
| 226 | + |
| 227 | + const result = await verifyMPCWalletAddress( |
| 228 | + { address: expectedAddress, keychains: ecdsaKeychains, index: 1, keyCurve: 'secp256k1' }, |
| 229 | + isValidEcdsaAddress, |
| 230 | + getAddressFromPublicKey |
| 231 | + ); |
| 232 | + result.should.be.true(); |
| 233 | + }); |
| 234 | + |
| 235 | + it('rejects an address derived at a different index', async function () { |
| 236 | + const verifyMPCWalletAddress = getVerifyMPCWalletAddress(); |
| 237 | + const addressAtIndex0 = new Ecdsa().deriveUnhardened(ECDSA_KEYCHAIN, 'm/0').slice(0, 66); |
| 238 | + |
| 239 | + const result = await verifyMPCWalletAddress( |
| 240 | + { address: addressAtIndex0, keychains: ecdsaKeychains, index: 1, keyCurve: 'secp256k1' }, |
| 241 | + isValidEcdsaAddress, |
| 242 | + getAddressFromPublicKey |
| 243 | + ); |
| 244 | + result.should.be.false(); |
| 245 | + }); |
| 246 | +}); |
0 commit comments