|
| 1 | +import { addressToPublicKey, publicKeyToAddress, addressToScriptPublicKey } from '@/lib/kaspa-util'; |
| 2 | + |
| 3 | +describe('addressToPublicKey', () => { |
| 4 | + it('should be able to round-trip', () => { |
| 5 | + const testAddr = 'kaspa:qqs7krzzwqfgk9kf830smtzg64s9rf3r0khfj76cjynf2pfgrr35saatu88xq'; |
| 6 | + const pubkey = addressToPublicKey(testAddr); |
| 7 | + const roundtripPublicKey = publicKeyToAddress(Buffer.from(pubkey.publicKey), false); |
| 8 | + |
| 9 | + expect(roundtripPublicKey).toBe(testAddr); |
| 10 | + }); |
| 11 | + |
| 12 | + it('should be able to convert a schnorr-based address to a public key', () => { |
| 13 | + const expectedPublicKey = Buffer.from('21eb0c4270128b16c93c5f0dac48d56051a6237dae997b58912695052818e348', 'hex'); |
| 14 | + const expectedVersion = 0; |
| 15 | + const address = 'kaspa:qqs7krzzwqfgk9kf830smtzg64s9rf3r0khfj76cjynf2pfgrr35saatu88xq'; |
| 16 | + |
| 17 | + const result = addressToPublicKey(address); |
| 18 | + expect(Buffer.from(result.publicKey)).toStrictEqual(expectedPublicKey); |
| 19 | + expect(result.version).toBe(expectedVersion); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should be able to convert a p2sh address to a public key', () => { |
| 23 | + const expectedPublicKey = Buffer.from('f38031f61ca23d70844f63a477d07f0b2c2decab907c2e096e548b0e08721c79', 'hex'); |
| 24 | + const expectedVersion = 8; |
| 25 | + const address = 'kaspa:precqv0krj3r6uyyfa36ga7s0u9jct0v4wg8ctsfde2gkrsgwgw8jgxfzfc98'; |
| 26 | + |
| 27 | + const result = addressToPublicKey(address); |
| 28 | + expect(Buffer.from(result.publicKey)).toStrictEqual(expectedPublicKey); |
| 29 | + expect(result.version).toBe(expectedVersion); |
| 30 | + }); |
| 31 | + |
| 32 | + it('should be able to convert a ECDSA-based address to a public key', () => { |
| 33 | + const expectedPublicKey = Buffer.from('02d5fdc7ad11a65d0bbe7882fc3dbc91b5861d182dcce79f7c1be5bfd30a677cd6', 'hex'); |
| 34 | + const expectedVersion = 1; |
| 35 | + const address = 'kaspa:qypdtlw845g6vhgtheug9lpahjgmtpsarqkueeul0sd7t07npfnhe4s7fd82n0v'; |
| 36 | + |
| 37 | + const result = addressToPublicKey(address); |
| 38 | + expect(Buffer.from(result.publicKey)).toStrictEqual(expectedPublicKey); |
| 39 | + expect(result.version).toBe(expectedVersion); |
| 40 | + }); |
| 41 | +}); |
| 42 | + |
| 43 | +describe('publicKeyToAddress', () => { |
| 44 | + it('should be able to convert to a schnorr-based address', () => { |
| 45 | + const expectedAddress = 'kaspa:qqs7krzzwqfgk9kf830smtzg64s9rf3r0khfj76cjynf2pfgrr35saatu88xq'; |
| 46 | + const publicKey = Buffer.from('21eb0c4270128b16c93c5f0dac48d56051a6237dae997b58912695052818e348', 'hex'); |
| 47 | + |
| 48 | + expect(publicKeyToAddress(publicKey)).toBe(expectedAddress); |
| 49 | + expect(publicKeyToAddress(publicKey, false)).toBe(expectedAddress); |
| 50 | + expect(publicKeyToAddress(publicKey, false, 'schnorr')).toBe(expectedAddress); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should be able to convert to a schnorr-based address without prefix', () => { |
| 54 | + const expectedAddress = 'qqs7krzzwqfgk9kf830smtzg64s9rf3r0khfj76cjynf2pfgrr35saatu88xq'; |
| 55 | + const publicKey = Buffer.from('21eb0c4270128b16c93c5f0dac48d56051a6237dae997b58912695052818e348', 'hex'); |
| 56 | + |
| 57 | + expect(publicKeyToAddress(publicKey, true)).toBe(expectedAddress); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should be able to convert to a p2sh address', () => { |
| 61 | + const expectedAddress = 'kaspa:precqv0krj3r6uyyfa36ga7s0u9jct0v4wg8ctsfde2gkrsgwgw8jgxfzfc98'; |
| 62 | + const publicKey = Buffer.from('f38031f61ca23d70844f63a477d07f0b2c2decab907c2e096e548b0e08721c79', 'hex'); |
| 63 | + |
| 64 | + expect(publicKeyToAddress(publicKey, false, 'p2sh')).toBe(expectedAddress); |
| 65 | + }); |
| 66 | + |
| 67 | + it('should be able to convert to a ECDSA-based address', () => { |
| 68 | + const expectedAddress = 'kaspa:qypdtlw845g6vhgtheug9lpahjgmtpsarqkueeul0sd7t07npfnhe4s7fd82n0v'; |
| 69 | + const publicKey = Buffer.from('02d5fdc7ad11a65d0bbe7882fc3dbc91b5861d182dcce79f7c1be5bfd30a677cd6', 'hex'); |
| 70 | + |
| 71 | + expect(publicKeyToAddress(publicKey, false, 'ecdsa')).toBe(expectedAddress); |
| 72 | + }); |
| 73 | +}); |
| 74 | + |
| 75 | +describe('addressToScriptPublicKey', () => { |
| 76 | + it('should be able to convert a schnorr address into script public key', () => { |
| 77 | + const expectedScriptPublicKey = '2021eb0c4270128b16c93c5f0dac48d56051a6237dae997b58912695052818e348ac'; |
| 78 | + const address = 'kaspa:qqs7krzzwqfgk9kf830smtzg64s9rf3r0khfj76cjynf2pfgrr35saatu88xq'; |
| 79 | + |
| 80 | + const result = addressToScriptPublicKey(address); |
| 81 | + expect(result).toBe(expectedScriptPublicKey); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should be able to convert an ecdsa address into script public key', () => { |
| 85 | + const expectedScriptPublicKey = '2102d5fdc7ad11a65d0bbe7882fc3dbc91b5861d182dcce79f7c1be5bfd30a677cd6ab'; |
| 86 | + const address = 'kaspa:qypdtlw845g6vhgtheug9lpahjgmtpsarqkueeul0sd7t07npfnhe4s7fd82n0v'; |
| 87 | + |
| 88 | + const result = addressToScriptPublicKey(address); |
| 89 | + expect(result).toBe(expectedScriptPublicKey); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should be able to convert a p2sh address into script public key', () => { |
| 93 | + const expectedScriptPublicKey = 'aa20f38031f61ca23d70844f63a477d07f0b2c2decab907c2e096e548b0e08721c7987'; |
| 94 | + const address = 'kaspa:precqv0krj3r6uyyfa36ga7s0u9jct0v4wg8ctsfde2gkrsgwgw8jgxfzfc98'; |
| 95 | + |
| 96 | + const result = addressToScriptPublicKey(address); |
| 97 | + expect(result).toBe(expectedScriptPublicKey); |
| 98 | + }); |
| 99 | +}); |
0 commit comments