Skip to content

Commit 17f8547

Browse files
feat(sdk-core): add static deriveKeyWithSeed implementation
Usable without having to instantiate a coin object. Issue: BTC-1731
1 parent 77f0bd2 commit 17f8547

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

modules/sdk-core/src/bitgo/baseCoin/baseCoin.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import {
4242
BuildNftTransferDataOptions,
4343
BaseBroadcastTransactionOptions,
4444
BaseBroadcastTransactionResult,
45+
DeriveKeyWithSeedOptions,
4546
} from './iBaseCoin';
4647
import { IInscriptionBuilder } from '../inscriptionBuilder';
4748
import { Hash } from 'crypto';
@@ -418,7 +419,13 @@ export abstract class BaseCoin implements IBaseCoin {
418419
* @param seed
419420
* @returns {{key: string, derivationPath: string}}
420421
*/
421-
deriveKeyWithSeed({ key, seed }: { key: string; seed: string }): { key: string; derivationPath: string } {
422+
static deriveKeyWithSeedBip32(
423+
key: utxolib.BIP32Interface,
424+
seed: string
425+
): {
426+
key: utxolib.BIP32Interface;
427+
derivationPath: string;
428+
} {
422429
function sha256(input) {
423430
return crypto.createHash('sha256').update(input).digest();
424431
}
@@ -428,11 +435,21 @@ export abstract class BaseCoin implements IBaseCoin {
428435
parseInt(derivationPathInput.slice(7, 14), 16),
429436
];
430437
const derivationPath = 'm/999999/' + derivationPathParts.join('/');
431-
const keyNode = bip32.fromBase58(key);
432-
const derivedKeyNode = keyNode.derivePath(derivationPath);
433438
return {
434-
key: derivedKeyNode.toBase58(),
435-
derivationPath: derivationPath,
439+
key: key.derivePath(derivationPath),
440+
derivationPath,
441+
};
442+
}
443+
444+
/** {@see deriveKeyWithSeedBip32} */
445+
deriveKeyWithSeed(params: DeriveKeyWithSeedOptions): {
446+
key: string;
447+
derivationPath: string;
448+
} {
449+
const { key, derivationPath } = BaseCoin.deriveKeyWithSeedBip32(bip32.fromBase58(params.key), params.seed);
450+
return {
451+
key: key.toBase58(),
452+
derivationPath,
436453
};
437454
}
438455

0 commit comments

Comments
 (0)