Skip to content

Commit

Permalink
Ensure use of hardened bit at account level
Browse files Browse the repository at this point in the history
  • Loading branch information
rileystephens28 committed Oct 17, 2024
1 parent 03861af commit 7288035
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
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

0 comments on commit 7288035

Please sign in to comment.