Skip to content

Commit

Permalink
extended keys working
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlincecum committed Jun 6, 2024
1 parent 40633fc commit 67d00b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
Empty file added src.ts/wallet/quai-hdwallet.ts
Empty file.
28 changes: 18 additions & 10 deletions src/wallet/hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,16 +224,24 @@ export abstract class HDWallet extends BaseWallet implements HDNodeLike<HDWallet
operation: 'extendedKey',
});

return encodeBase58Check(
const myKey = encodeBase58Check(
concat([
'0x0488ADE4',
zpad(this.depth, 1),
'0x' + zpad(this.depth, 2),
this.accountFingerprint ?? '',
zpad(this.index, 4),
'0x' + zpad(this.index, 4),
this.chainCode,
concat(['0x00', this.privateKey]),
]),
);
return myKey;
}

printBytes(data: BytesLike): void {
const hex = hexlify(data).substring(2);
console.log(hex.length / 2, hex);
const bytes = toBeArray(decodeBase58(encodeBase58Check(data)));
console.log(bytes.length - 4, bytes.slice(0, bytes.length - 4));
}

/**
Expand Down Expand Up @@ -357,17 +365,17 @@ export abstract class HDWallet extends BaseWallet implements HDNodeLike<HDWallet
const bytes = toBeArray(decodeBase58(extendedKey)); // @TODO: redact

assertArgument(
bytes.length === 82 || encodeBase58Check(bytes.slice(0, 78)) === extendedKey,
bytes.length === 80 || encodeBase58Check(bytes.slice(0, 76)) === extendedKey,
'invalid extended key',
'extendedKey',
'[ REDACTED ]',
);

const depth = bytes[4];
const accountFingerprint = hexlify(bytes.slice(5, 9));
const index = parseInt(hexlify(bytes.slice(9, 13)).substring(2), 16);
const chainCode = hexlify(bytes.slice(13, 45));
const key = bytes.slice(45, 78);
const index = parseInt(hexlify(bytes.slice(9, 11)).substring(2), 16);
const chainCode = hexlify(bytes.slice(11, 43));
const key = bytes.slice(43, 76);

switch (hexlify(bytes.slice(0, 4))) {
// Public Key
Expand All @@ -389,7 +397,7 @@ export abstract class HDWallet extends BaseWallet implements HDNodeLike<HDWallet

// Private Key
case '0x0488ade4':
case '0x04358394 ':
case '0x04358394':
if (key[0] !== 0) {
break;
}
Expand Down Expand Up @@ -652,9 +660,9 @@ export class HDNodeVoidWallet extends VoidSigner {
return encodeBase58Check(
concat([
'0x0488B21E',
zpad(this.depth, 1),
'0x' + zpad(this.depth, 2),
this.accountFingerprint ?? '',
zpad(this.index, 4),
'0x' + zpad(this.index, 4),
this.chainCode,
this._publicKey,
]),
Expand Down

0 comments on commit 67d00b3

Please sign in to comment.