|
1 | 1 | import * as assert from "assert"; |
| 2 | +import * as crypto from "crypto"; |
2 | 3 | import { bip32 as utxolibBip32 } from "@bitgo/utxo-lib"; |
3 | 4 | import { BIP32 } from "../js/bip32.js"; |
4 | 5 |
|
@@ -138,6 +139,25 @@ describe("WasmBIP32", () => { |
138 | 139 | assert.strictEqual(key.isNeutered(), false); |
139 | 140 | assert.ok(key.toBase58().startsWith("tprv")); |
140 | 141 | }); |
| 142 | + |
| 143 | + it("should create from seed string using SHA256", () => { |
| 144 | + const seedString = "test"; |
| 145 | + const key = bip32.BIP32.fromSeedSha256(seedString); |
| 146 | + assert.strictEqual(key.depth, 0); |
| 147 | + assert.strictEqual(key.isNeutered(), false); |
| 148 | + assert.ok(key.privateKey instanceof Uint8Array); |
| 149 | + // Should be deterministic |
| 150 | + const key2 = bip32.BIP32.fromSeedSha256(seedString); |
| 151 | + assert.strictEqual(key.toBase58(), key2.toBase58()); |
| 152 | + }); |
| 153 | + |
| 154 | + it("should create from seed string with network", () => { |
| 155 | + const seedString = "test"; |
| 156 | + const key = bip32.BIP32.fromSeedSha256(seedString, "BitcoinTestnet3"); |
| 157 | + assert.strictEqual(key.depth, 0); |
| 158 | + assert.strictEqual(key.isNeutered(), false); |
| 159 | + assert.ok(key.toBase58().startsWith("tprv")); |
| 160 | + }); |
141 | 161 | }); |
142 | 162 |
|
143 | 163 | describe("WasmBIP32 parity with utxolib", () => { |
@@ -336,4 +356,26 @@ describe("WasmBIP32 parity with utxolib", () => { |
336 | 356 | const wasmParentFp = new DataView(wasmKey.fingerprint.buffer).getUint32(0, false); |
337 | 357 | assert.strictEqual(wasmChild.parentFingerprint, wasmParentFp); |
338 | 358 | }); |
| 359 | + |
| 360 | + it("should match utxolib when using fromSeedSha256", () => { |
| 361 | + // Test various seed strings to ensure parity with manual SHA256 + fromSeed |
| 362 | + const seedStrings = ["test", "user", "backup", "bitgo", "default.0", "default.1", "default.2"]; |
| 363 | + |
| 364 | + for (const seedString of seedStrings) { |
| 365 | + // Manual approach: hash with SHA256, then create from seed |
| 366 | + const hash = crypto.createHash("sha256").update(seedString).digest(); |
| 367 | + const utxolibKey = utxolibBip32.fromSeed(hash); |
| 368 | + |
| 369 | + // WASM approach: fromSeedSha256 does hashing internally |
| 370 | + const wasmKey = bip32.BIP32.fromSeedSha256(seedString); |
| 371 | + |
| 372 | + assert.strictEqual( |
| 373 | + wasmKey.toBase58(), |
| 374 | + utxolibKey.toBase58(), |
| 375 | + `Failed for seed string: ${seedString}`, |
| 376 | + ); |
| 377 | + assert.ok(bufferEqual(wasmKey.publicKey, utxolibKey.publicKey)); |
| 378 | + assert.ok(bufferEqual(wasmKey.chainCode, utxolibKey.chainCode)); |
| 379 | + } |
| 380 | + }); |
339 | 381 | }); |
0 commit comments