Skip to content

Commit 684f7ec

Browse files
committed
WIP: loop over addresses
1 parent 7c0b484 commit 684f7ec

File tree

1 file changed

+53
-25
lines changed

1 file changed

+53
-25
lines changed

demo.js

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ let DashHd = require("dashhd");
55
let DashKeys = require("dashkeys");
66
let DashTx = require("dashtx");
77
let DashPlatform = require("./dashplatform.js");
8-
let CBOR = require("CBOR");
8+
let CBOR = require("cbor");
99

1010
let KeyUtils = require("./key-utils.js");
1111

@@ -54,53 +54,80 @@ let KEY_TYPES = {
5454
ECDSA_SECP256K1: 0,
5555
};
5656

57+
let network = "testnet";
58+
let coinType = 5; // DASH
59+
if (network === "testnet") {
60+
coinType = 1; // testnet
61+
}
62+
//coinType = 1;
63+
5764
let identityEcdsaPath = "";
5865
{
5966
// m/purpose'/coin_type'/feature'/subfeature'/keytype'/identityindex'/keyindex'
6067
// ex: m/9'/5'/5'/0'/0'/<id>/<key>
6168
let purposeDip13 = 9;
62-
let coinDash = 5;
6369
let featureId = 5;
6470
let subfeatureKey = 0;
6571
let keyType = KEY_TYPES.ECDSA_SECP256K1;
66-
identityEcdsaPath = `m/${purposeDip13}'/${coinDash}'/${featureId}'/${subfeatureKey}'/${keyType}'`;
72+
identityEcdsaPath = `m/${purposeDip13}'/${coinType}'/${featureId}'/${subfeatureKey}'/${keyType}'`;
6773
}
6874

6975
async function main() {
7076
void (await WasmDpp.default());
7177

72-
let network = "testnet";
73-
7478
let dashTx = DashTx.create(KeyUtils);
7579

7680
// let phrase = await DashPhrase.generate();
7781
let phrase =
78-
"casino reveal crop open ordinary garment spy pizza clown exercise poem enjoy";
82+
"wool panel expand embrace try lab rescue reason drop fog stand kangaroo";
83+
console.log(`Phrase:`);
84+
console.log(phrase);
7985
let salt = "";
8086
let seedBytes = await DashPhrase.toSeed(phrase, salt);
81-
let walletKey = await DashHd.fromSeed(seedBytes);
87+
console.log("Seed:");
88+
console.log(DashTx.utils.bytesToHex(seedBytes));
89+
let walletKey = await DashHd.fromSeed(seedBytes, { coinType });
90+
let walletId = await DashHd.toId(walletKey);
91+
console.log(`Wallet ID:`);
92+
console.log(walletId);
8293

8394
let accountIndex = 0; // pick the desired account for paying the fee
84-
let addressIndex = 8; // pick an address with funds
85-
let accountKey = await walletKey.deriveAccount(accountIndex);
86-
let use = DashHd.RECEIVE;
87-
let xprvKey = await accountKey.deriveXKey(use);
88-
let addressKey = await xprvKey.deriveAddress(addressIndex);
89-
if (!addressKey.privateKey) {
90-
throw new Error("not an error, just a lint hack");
95+
let addressIndex = 0; // pick an address with funds
96+
let accountKey;
97+
for (let a = 0; a <= accountIndex; a += 1) {
98+
accountKey = await walletKey.deriveAccount(a);
99+
100+
for (let usage of [DashHd.RECEIVE, DashHd.CHANGE]) {
101+
let xprvKey = await accountKey.deriveXKey(usage);
102+
103+
let addressKey;
104+
let addr;
105+
let pkh;
106+
let wif;
107+
for (let i = 0; i <= addressIndex; i += 1) {
108+
addressKey = await xprvKey.deriveAddress(i);
109+
if (!addressKey.privateKey) {
110+
throw new Error("not an error, just a lint hack");
111+
}
112+
113+
addr = await DashHd.toAddr(addressKey.publicKey, { version: network });
114+
let pkhBytes = await DashKeys.addrToPkh(addr, {
115+
//@ts-ignore
116+
version: network,
117+
});
118+
pkh = DashKeys.utils.bytesToHex(pkhBytes);
119+
wif = await DashHd.toWif(addressKey.privateKey, { version: network });
120+
console.log();
121+
console.log(
122+
`[m/44'/${coinType}'/${a}'/${usage}/${i}] Address: ${addr}`,
123+
);
124+
// TODO is _this_ the assetLockPrivateKey??
125+
console.log(`[m/44'/${coinType}'/${a}/${usage}/${i}] WIF: ${wif}`);
126+
}
127+
}
91128
}
92129

93-
let addr = await DashHd.toAddr(addressKey.publicKey, { version: network });
94-
let pkhBytes = await DashKeys.addrToPkh(addr, {
95-
//@ts-ignore
96-
version: network,
97-
});
98-
let pkh = DashKeys.utils.bytesToHex(pkhBytes);
99-
let wif = await DashHd.toWif(addressKey.privateKey, { version: network });
100-
console.log();
101-
console.log(`Address: ${addr}`);
102-
// TODO is _this_ the assetLockPrivateKey??
103-
console.log(`WIF: ${wif}`);
130+
process.exit(1);
104131

105132
KeyUtils.set(addr, {
106133
address: addr,
@@ -226,6 +253,7 @@ async function main() {
226253
let cbor = CBOR.encodeCanonical(stateTransition);
227254
console.log(`cbor:`);
228255
console.log(DashTx.utils.bytesToHex(cbor));
256+
console.log(bytesToBase64(cbor));
229257

230258
let sigBytes = await KeyUtils.sign(addressKey.privateKey, cbor);
231259
let sigHex = DashTx.utils.bytesToHex(sigBytes);

0 commit comments

Comments
 (0)