Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure hardened account derivation #321

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/wallet/hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { Zone } from '../constants/index.js';
import { TransactionRequest, Provider } from '../providers/index.js';
import { AllowedCoinType } from '../constants/index.js';

export const HARDENED_OFFSET = 2 ** 31;

/**
* Interface representing information about a neutered address.
*/
Expand Down Expand Up @@ -123,7 +125,7 @@ export abstract class AbstractHDWallet {
isChange: boolean = false,
): HDNodeWallet {
const changeIndex = isChange ? 1 : 0;
const changeNode = this._root.deriveChild(account).deriveChild(changeIndex);
const changeNode = this._root.deriveChild(account + HARDENED_OFFSET).deriveChild(changeIndex);

let addrIndex = startingIndex;
let addressNode: HDNodeWallet;
Expand Down Expand Up @@ -177,7 +179,10 @@ export abstract class AbstractHDWallet {

// derive the address node and validate the zone
const changeIndex = isChange ? 1 : 0;
const addressNode = this._root.deriveChild(account).deriveChild(changeIndex).deriveChild(addressIndex);
const addressNode = this._root
.deriveChild(account + HARDENED_OFFSET)
.deriveChild(changeIndex)
.deriveChild(addressIndex);
const zone = getZoneForAddress(addressNode.address);
if (!zone) {
throw new Error(`Failed to derive a valid address zone for the index ${addressIndex}`);
Expand Down Expand Up @@ -413,7 +418,10 @@ export abstract class AbstractHDWallet {
}

const changeIndex = addressInfo.change ? 1 : 0;
return this._root.deriveChild(addressInfo.account).deriveChild(changeIndex).deriveChild(addressInfo.index);
return this._root
.deriveChild(addressInfo.account + HARDENED_OFFSET)
.deriveChild(changeIndex)
.deriveChild(addressInfo.index);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/wallet/qi-hdwallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
SerializedHDWallet,
_guard,
MAX_ADDRESS_DERIVATION_ATTEMPTS,
HARDENED_OFFSET,
} from './hdwallet.js';
import { HDNodeWallet } from './hdnodewallet.js';
import { QiTransactionRequest, Provider, TransactionResponse } from '../providers/index.js';
Expand Down Expand Up @@ -705,7 +706,7 @@ export class QiHDWallet extends AbstractHDWallet {
// NeuteredAddressInfo (BIP44 addresses)
const changeIndex = addressInfo.change ? 1 : 0;
const addressNode = this._root
.deriveChild(addressInfo.account)
.deriveChild(addressInfo.account + HARDENED_OFFSET)
.deriveChild(changeIndex)
.deriveChild(addressInfo.index);
return addressNode.privateKey;
Expand Down Expand Up @@ -1297,7 +1298,7 @@ export class QiHDWallet extends AbstractHDWallet {
private _getPaymentCodePrivate(account: number): PaymentCodePrivate {
const bip32 = this._getBIP32API();

const accountNode = this._root.deriveChild(account);
const accountNode = this._root.deriveChild(account + HARDENED_OFFSET);

// payment code array
const pc = new Uint8Array(80);
Expand Down
Loading