|
| 1 | +import assert from 'node:assert/strict'; |
| 2 | + |
| 3 | +import { BitGoAPI } from '@bitgo/sdk-api'; |
| 4 | + |
| 5 | +import { |
| 6 | + Zec, |
| 7 | + Tzec, |
| 8 | + getZcashAddressKind, |
| 9 | + isShieldedZcashAddress, |
| 10 | + isValidZcashAddress, |
| 11 | +} from '../../../../../src/impl/zec'; |
| 12 | + |
| 13 | +// ZIP-316 unified-address test vectors, copied from |
| 14 | +// BitGoWASM/packages/wasm-utxo/test/fixtures/zcash/unified_address.json so |
| 15 | +// both repos test against the same known-good data. |
| 16 | +const zip316Mainnet = { |
| 17 | + unified: |
| 18 | + 'u1pg2aaph7jp8rpf6yhsza25722sg5fcn3vaca6ze27hqjw7jvvhhuxkpcg0ge9xh6drsgdkda8qjq5chpehkcpxf87rnjryjqwymdheptpvnljqqrjqzjwkc2ma6hcq666kgwfytxwac8eyex6ndgr6ezte66706e3vaqrd25dzvzkc69kw0jgywtd0cmq52q5lkw6uh7hyvzjse8ksx', |
| 19 | +}; |
| 20 | +const testnetWallet = { |
| 21 | + unified: |
| 22 | + 'utest1w5m0qcnp8egl8qa296n70n8nvj0tqnzk90p7f48v7mjhhdrdqs8vgqydslg5plmzefawefnpmgmlm6hcy38m972erwxs04s02cq2prhguz8kqly75m6zjy56m08d5jnycgtpqtjeprte576gkmrxyszepgx76yzuwhh7m4lfz9jaq7unjk0x5ant46juxz73hsc6q4v3dqtzww00vps', |
| 23 | + transparentAddress: 'tmM4DvLVJKXZt5ydn1tqYTHvahpKSwgjuRk', |
| 24 | +}; |
| 25 | + |
| 26 | +describe('Zcash address validation', function () { |
| 27 | + let bitgo: BitGoAPI; |
| 28 | + let zec; |
| 29 | + let tzec; |
| 30 | + |
| 31 | + before(function () { |
| 32 | + bitgo = new BitGoAPI({ env: 'mock' }); |
| 33 | + bitgo.register('zec', Zec.createInstance); |
| 34 | + bitgo.register('tzec', Tzec.createInstance); |
| 35 | + zec = bitgo.coin('zec'); |
| 36 | + tzec = bitgo.coin('tzec'); |
| 37 | + }); |
| 38 | + |
| 39 | + it('recognizes a mainnet unified address as shielded', function () { |
| 40 | + assert.strictEqual(zec.isValidAddress(zip316Mainnet.unified), true); |
| 41 | + assert.strictEqual(getZcashAddressKind(zip316Mainnet.unified, 'zec'), 'shielded'); |
| 42 | + assert.strictEqual(isShieldedZcashAddress(zip316Mainnet.unified, 'zec'), true); |
| 43 | + assert.strictEqual(isValidZcashAddress(zip316Mainnet.unified, 'zec'), true); |
| 44 | + }); |
| 45 | + |
| 46 | + it('recognizes a testnet unified address as shielded', function () { |
| 47 | + assert.strictEqual(tzec.isValidAddress(testnetWallet.unified), true); |
| 48 | + assert.strictEqual(getZcashAddressKind(testnetWallet.unified, 'tzec'), 'shielded'); |
| 49 | + assert.strictEqual(isShieldedZcashAddress(testnetWallet.unified, 'tzec'), true); |
| 50 | + assert.strictEqual(isValidZcashAddress(testnetWallet.unified, 'tzec'), true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('recognizes a testnet transparent address as transparent', function () { |
| 54 | + assert.strictEqual(tzec.isValidAddress(testnetWallet.transparentAddress), true); |
| 55 | + assert.strictEqual(getZcashAddressKind(testnetWallet.transparentAddress, 'tzec'), 'transparent'); |
| 56 | + assert.strictEqual(isValidZcashAddress(testnetWallet.transparentAddress, 'tzec'), true); |
| 57 | + }); |
| 58 | + |
| 59 | + it('recognizes a mainnet transparent (P2PKH) address as transparent', function () { |
| 60 | + const address = 't1cN2ZVWzWcVRrnfeQzmkpLhzQ4dYRv8yRY'; |
| 61 | + assert.strictEqual(zec.isValidAddress(address), true); |
| 62 | + assert.strictEqual(getZcashAddressKind(address, 'zec'), 'transparent'); |
| 63 | + assert.strictEqual(isValidZcashAddress(address, 'zec'), true); |
| 64 | + }); |
| 65 | + |
| 66 | + it('rejects a garbage string', function () { |
| 67 | + const garbage = 'not-a-real-address'; |
| 68 | + assert.strictEqual(zec.isValidAddress(garbage), false); |
| 69 | + assert.strictEqual(getZcashAddressKind(garbage, 'zec'), undefined); |
| 70 | + assert.strictEqual(isValidZcashAddress(garbage, 'zec'), false); |
| 71 | + }); |
| 72 | + |
| 73 | + it('rejects a unified address checked against the wrong network', function () { |
| 74 | + assert.strictEqual(tzec.isValidAddress(zip316Mainnet.unified), false); |
| 75 | + assert.strictEqual(getZcashAddressKind(zip316Mainnet.unified, 'tzec'), undefined); |
| 76 | + assert.strictEqual(isShieldedZcashAddress(zip316Mainnet.unified, 'tzec'), false); |
| 77 | + assert.strictEqual(isValidZcashAddress(zip316Mainnet.unified, 'tzec'), false); |
| 78 | + }); |
| 79 | +}); |
0 commit comments