-
Notifications
You must be signed in to change notification settings - Fork 112
/
derivation.js
67 lines (55 loc) · 2.29 KB
/
derivation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const kaspa = require('../../../../nodejs/kaspa');
const {
Mnemonic,
XPrv,
DerivationPath,
PublicKey,
NetworkType,
} = kaspa;
kaspa.initConsolePanicHook();
(async () => {
//const mnemonic = Mnemonic.random();
const mnemonic = new Mnemonic("hunt bitter praise lift buyer topic crane leopard uniform network inquiry over grain pass match crush marine strike doll relax fortune trumpet sunny silk")
console.log("mnemonic:", mnemonic);
const seed = mnemonic.toSeed();
console.log("seed:", seed);
// kaspa
let xPrv = new XPrv(seed);
// derive full path upto second address of receive wallet
let pubkey1 = xPrv.derivePath("m/44'/111111'/0'/0/1").toXPub().toPublicKey();
console.log("address", pubkey1.toAddress(NetworkType.Mainnet));
// create receive wallet
let receiveWalletXPub = xPrv.derivePath("m/44'/111111'/0'/0").toXPub();
// derive receive wallet for second address
let pubkey2 = receiveWalletXPub.deriveChild(1, false).toPublicKey();
console.log("address", pubkey2.toAddress(NetworkType.Mainnet));
// create change wallet
let changeWalletXPub = xPrv.derivePath("m/44'/111111'/0'/1").toXPub();
// derive change wallet for first address
let pubkey3 = changeWalletXPub.deriveChild(0, false).toPublicKey();
console.log("address", pubkey2.toAddress(NetworkType.Mainnet));
if (pubkey1.toString() != pubkey2.toString()){
throw new Error("pubkeyes dont match")
}
// ---
// xprv with ktrv prefix
const ktrv = xPrv.intoString("ktrv");
console.log("ktrv", ktrv)
//create DerivationPath
const path = new DerivationPath("m/1'");
path.push(2, true);
path.push(3, false);
console.log(`path: ${path}`);
// derive by path string
console.log("xPrv1", xPrv.derivePath("m/1'/2'/3").intoString("xprv"))
// derive by DerivationPath object
console.log("xPrv3", xPrv.derivePath(path).intoString("xprv"))
// create XPrv from ktrvxxx string and derive it
console.log("xPrv2", XPrv.fromXPrv(ktrv).derivePath("m/1'/2'/3").intoString("xprv"))
// get xpub
let xPub = xPrv.toXPub();
// derive xPub
console.log("xPub", xPub.derivePath("m/1").intoString("xpub"));
// get publicKey from xPub
console.log("publicKey", xPub.toPublicKey().toString());
})();