|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +let DashHd = require("./"); |
| 4 | +//let DashHd = require("dashhd"); |
| 5 | +let DashPhrase = require("dashphrase"); |
| 6 | + |
| 7 | +let words = "zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo zoo wrong"; |
| 8 | +let secret = "TREZOR"; |
| 9 | + |
| 10 | +// m/44'/5'/0'/0/0 |
| 11 | +let wif000 = "XCGKuZcKDjNhx8DaNKK4xwMMNzspaoToT6CafJAbBfQTi57buhLK"; |
| 12 | +let addr000 = "XrZJJfEKRNobcuwWKTD3bDu8ou7XSWPbc9"; |
| 13 | + |
| 14 | +let xprv00 = |
| 15 | + "xprvA2L7qar7dyJNhxnE47gK5J6cc1oEHQuAk8WrZLnLeHTtnkeyP4w6Eo6Tt65trtdkTRtx8opazGnLbpWrkhzNaL6ZsgG3sQmc2yS8AxoMjfZ"; |
| 16 | +let xpub00 = |
| 17 | + "xpub6FKUF6P1ULrfvSrhA9DKSS3MA3digsd27MSTMjBxCczsfYz7vcFLnbQwjP9CsAfEJsnD4UwtbU43iZaibv4vnzQNZmQAVcufN4r3pva8kTz"; |
| 18 | + |
| 19 | +// m/44'/5'/1'/1/1 |
| 20 | +let wif111 = "XF9murLtNpJaZXbwMxqJ6BhigEtu9NxfBCJDBokCJcqFkYkz3itz"; |
| 21 | +let addr111 = "XueHW2ELMxoXzXcaHMxmwVWhcADE1W5s8c"; |
| 22 | + |
| 23 | +async function getWalletKeys() { |
| 24 | + let seed = await DashPhrase.toSeed(words, secret); |
| 25 | + let wallet = await DashHd.fromSeed(seed); |
| 26 | + |
| 27 | + // m/44'/5'/0'/0/0 |
| 28 | + let accountIndex = 0; |
| 29 | + let account = await wallet.deriveAccount(accountIndex); |
| 30 | + |
| 31 | + let use = DashHd.RECEIVE; |
| 32 | + let xkey = await account.deriveXKey(use); |
| 33 | + let xkey1 = await DashHd.derivePath(wallet, "m/44'/5'/0'/0"); |
| 34 | + let xprv = await DashHd.toXPrv(xkey); |
| 35 | + if (xprv !== xprv00) { |
| 36 | + throw new Error("wallet xprv derivation mismatch v canonical"); |
| 37 | + } |
| 38 | + let xprv1 = await DashHd.toXPrv(xkey1); |
| 39 | + if (xprv !== xprv1) { |
| 40 | + throw new Error("wallet xprv derivation mismatch"); |
| 41 | + } |
| 42 | + let xpub = await DashHd.toXPub(xkey); |
| 43 | + if (xpub !== xpub00) { |
| 44 | + throw new Error("wallet xpub derivation mismatch v canonical"); |
| 45 | + } |
| 46 | + let xpub1 = await DashHd.toXPub(xkey1); |
| 47 | + if (xpub !== xpub1) { |
| 48 | + throw new Error("wallet xpub derivation mismatch"); |
| 49 | + } |
| 50 | + console.info(` HD XKey Path: m/44'/5'/${accountIndex}'/${use}`); |
| 51 | + console.info(` XPrv: `, xprv.slice(0, 56)); |
| 52 | + console.info(` `, xprv.slice(56)); |
| 53 | + console.info(` XPub: `, xpub.slice(0, 56)); |
| 54 | + console.info(` `, xpub.slice(56)); |
| 55 | + console.info(); |
| 56 | + |
| 57 | + let addressIndex = 0; |
| 58 | + let key = await xkey.deriveKey(addressIndex); |
| 59 | + let key1 = await DashHd.derivePath(wallet, "m/44'/5'/0'/0/0"); |
| 60 | + |
| 61 | + let wif = await DashHd.toWif(key.privateKey); |
| 62 | + if (wif !== wif000) { |
| 63 | + throw new Error("wallet wif derivation mismatch"); |
| 64 | + } |
| 65 | + let wif1 = await DashHd.toWif(key1.privateKey); |
| 66 | + if (wif !== wif1) { |
| 67 | + throw new Error("wallet wif derivation mismatch"); |
| 68 | + } |
| 69 | + let addr = await DashHd.toAddr(key.publicKey); |
| 70 | + if (addr !== addr000) { |
| 71 | + throw new Error("wallet addr derivation mismatch"); |
| 72 | + } |
| 73 | + let addr1 = await DashHd.toAddr(key1.publicKey); |
| 74 | + if (addr !== addr1) { |
| 75 | + throw new Error("wallet addr derivation mismatch"); |
| 76 | + } |
| 77 | + let xkey2 = await DashHd.fromXKey(xpub); |
| 78 | + let key2 = await xkey2.deriveKey(addressIndex); |
| 79 | + let addr2 = await DashHd.toAddr(key2.publicKey); |
| 80 | + if (addr !== addr2) { |
| 81 | + throw new Error("wallet xpub addr derivation mismatch"); |
| 82 | + } |
| 83 | + |
| 84 | + console.info( |
| 85 | + ` HD Key Path: m/44'/5'/${accountIndex}'/${use}/${addressIndex}`, |
| 86 | + ); |
| 87 | + console.info(` WIF: ${wif}`); |
| 88 | + console.info(` Address: ${addr}`); |
| 89 | + console.info(); |
| 90 | + console.info(); |
| 91 | + |
| 92 | + // m/44'/5'/1'/1/1 |
| 93 | + accountIndex = 1; |
| 94 | + account = await wallet.deriveAccount(accountIndex); |
| 95 | + |
| 96 | + use = DashHd.CHANGE; |
| 97 | + xkey = await account.deriveXKey(use); |
| 98 | + xkey1 = await DashHd.derivePath(wallet, "m/44'/5'/1'/1"); |
| 99 | + xprv = await DashHd.toXPrv(xkey); |
| 100 | + xprv1 = await DashHd.toXPrv(xkey1); |
| 101 | + if (xprv !== xprv1) { |
| 102 | + throw new Error("wallet xprv derivation mismatch"); |
| 103 | + } |
| 104 | + xpub = await DashHd.toXPub(xkey); |
| 105 | + xpub1 = await DashHd.toXPub(xkey1); |
| 106 | + if (xpub !== xpub1) { |
| 107 | + throw new Error("wallet xpub derivation mismatch"); |
| 108 | + } |
| 109 | + console.info(` HD XKey Path: m/44'/5'/${accountIndex}'/${use}`); |
| 110 | + console.info(` XPrv: `, xprv.slice(0, 56)); |
| 111 | + console.info(` `, xprv.slice(56)); |
| 112 | + console.info(` XPub: `, xpub.slice(0, 56)); |
| 113 | + console.info(` `, xpub.slice(56)); |
| 114 | + console.info(); |
| 115 | + |
| 116 | + addressIndex = 1; |
| 117 | + key = await xkey.deriveKey(addressIndex); |
| 118 | + key1 = await DashHd.derivePath(wallet, "m/44'/5'/1'/1/1"); |
| 119 | + |
| 120 | + wif = await DashHd.toWif(key.privateKey); |
| 121 | + if (wif !== wif111) { |
| 122 | + throw new Error("wallet wif derivation mismatch"); |
| 123 | + } |
| 124 | + wif1 = await DashHd.toWif(key1.privateKey); |
| 125 | + if (wif !== wif1) { |
| 126 | + throw new Error("wallet wif derivation mismatch"); |
| 127 | + } |
| 128 | + addr = await DashHd.toAddr(key.publicKey); |
| 129 | + if (addr !== addr111) { |
| 130 | + throw new Error("wallet addr derivation mismatch"); |
| 131 | + } |
| 132 | + addr1 = await DashHd.toAddr(key1.publicKey); |
| 133 | + if (addr !== addr1) { |
| 134 | + throw new Error("wallet addr derivation mismatch"); |
| 135 | + } |
| 136 | + xkey2 = await DashHd.fromXKey(xpub); |
| 137 | + key2 = await xkey2.deriveKey(addressIndex); |
| 138 | + addr2 = await DashHd.toAddr(key2.publicKey); |
| 139 | + if (addr !== addr2) { |
| 140 | + throw new Error("wallet xpub addr derivation mismatch"); |
| 141 | + } |
| 142 | + let xkey3 = await DashHd.fromXKey(xprv); |
| 143 | + let key3 = await xkey3.deriveKey(addressIndex); |
| 144 | + let wif3 = await DashHd.toWif(key3.privateKey); |
| 145 | + if (wif !== wif3) { |
| 146 | + throw new Error("wallet xprv wif derivation mismatch"); |
| 147 | + } |
| 148 | + let addr3 = await DashHd.toAddr(key3.publicKey); |
| 149 | + if (addr !== addr3) { |
| 150 | + throw new Error("wallet xprv addr derivation mismatch"); |
| 151 | + } |
| 152 | + |
| 153 | + console.info( |
| 154 | + ` HD Key Path: m/44'/5'/${accountIndex}'/${use}/${addressIndex}`, |
| 155 | + ); |
| 156 | + console.info(` WIF: ${wif}`); |
| 157 | + console.info(` Address: ${addr}`); |
| 158 | + console.info(); |
| 159 | +} |
| 160 | + |
| 161 | +async function main() { |
| 162 | + await getWalletKeys(); |
| 163 | +} |
| 164 | + |
| 165 | +main() |
| 166 | + .then(function () { |
| 167 | + process.exit(0); |
| 168 | + }) |
| 169 | + .catch(function (err) { |
| 170 | + console.error("Fail:"); |
| 171 | + console.error(err.stack || err); |
| 172 | + process.exit(1); |
| 173 | + }); |
0 commit comments