Skip to content

Commit 77664ed

Browse files
committed
Add tests for kaspa-util
1 parent 9fd2a34 commit 77664ed

File tree

2 files changed

+105
-13
lines changed

2 files changed

+105
-13
lines changed

Diff for: lib/kaspa-util.js

+6-13
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,14 @@ function checksumToArray(checksum) {
8989
return result.reverse();
9090
}
9191

92-
export function publicKeyToAddress(hashBuffer, stripPrefix) {
92+
export function publicKeyToAddress(hashBuffer, stripPrefix, type = 'schnorr') {
9393
function getTypeBits(type) {
9494
switch (type) {
95-
case 'pubkey':
95+
case 'schnorr':
9696
return 0;
97-
case 'scripthash':
97+
case 'ecdsa':
98+
return 1;
99+
case 'p2sh':
98100
return 8;
99101
default:
100102
throw new Error('Invalid type:' + type);
@@ -103,7 +105,7 @@ export function publicKeyToAddress(hashBuffer, stripPrefix) {
103105

104106
var eight0 = [0, 0, 0, 0, 0, 0, 0, 0];
105107
var prefixData = prefixToArray('kaspa').concat([0]);
106-
var versionByte = getTypeBits('pubkey');
108+
var versionByte = getTypeBits(type);
107109
var arr = Array.prototype.slice.call(hashBuffer, 0);
108110
var payloadData = convertBits([versionByte].concat(arr), 8, 5);
109111
var checksumData = prefixData.concat(payloadData).concat(eight0);
@@ -115,16 +117,7 @@ export function publicKeyToAddress(hashBuffer, stripPrefix) {
115117
}
116118
}
117119

118-
// FIXME: Add these to unit tests
119-
// const testAddr = "kaspa:qzese5lc37m2a9np8k5gect4l2jj8svyqq392p7aa7mxsqarjg9sjgxr4wvru";
120-
// const pubkey = addressToPublicKey(testAddr);
121-
// const res = publicKeyToAddress(Buffer.from(pubkey), false);
122-
123-
// console.info(addressToScriptPublicKey(testAddr));
124-
125-
// console.info(testAddr === res, testAddr, res);
126120
export function addressToPublicKey(address) {
127-
// return Script.fromAddress(new Address(address, "kaspa")).toBuffer().toString("hex");
128121
const addrPart = address.split(':')[1];
129122

130123
const payload = convertBits(base32.decode(addrPart), 5, 8);

Diff for: tests/kaspa-util.test.js

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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

Comments
 (0)