Skip to content

Commit

Permalink
resolve comment + rename to fit BIP44 specs
Browse files Browse the repository at this point in the history
  • Loading branch information
kevzzsk committed Jul 18, 2023
1 parent b65ae2f commit f05ed15
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 32 deletions.
30 changes: 20 additions & 10 deletions packages/sdk/src/addresses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,37 +120,41 @@ export function getAccountDataFromHdNode({
hdNode,
format = "legacy",
network = "testnet",
accountIndex = 0,
index = 0
account = 0,
addressIndex = 0
}: GetAccountDataFromHdNodeOptions) {
if (!hdNode) {
throw new Error("Invalid options provided.");
}

const addressType = addressNameToType[format];

const fullDerivationPath = getDerivationPath(format, accountIndex, index);
const fullDerivationPath = getDerivationPath(format, account, addressIndex);
const child = hdNode.derivePath(fullDerivationPath);

const pubKey = format === "taproot" ? toXOnly(child.publicKey) : child.publicKey;
const paymentObj = createTransaction(pubKey, addressType, network);

const address = paymentObj.address!;

const account: Account = {
const accountData: Account = {
address,
pub: child.publicKey.toString("hex"),
priv: child.privateKey!.toString("hex"),
format,
type: addressType,
derivationPath: fullDerivationPath
derivationPath: {
account,
addressIndex,
path: fullDerivationPath
}
};

if (format === "taproot") {
account.xkey = toXOnly(child.publicKey).toString("hex");
accountData.xkey = toXOnly(child.publicKey).toString("hex");
}

return account;
return accountData;
}

export function getAllAccountsFromHdNode({ hdNode, network = "testnet" }: GetAllAccountsFromHDNodeOptions) {
Expand All @@ -177,10 +181,16 @@ export type Address = {
pub: string;
};

export type Derivation = {
account: number;
addressIndex: number;
path: string
}

export type Account = Address & {
priv: string;
type: AddressTypes;
derivationPath: string;
derivationPath: Derivation;
};

type GetAddressesOptions = {
Expand All @@ -196,8 +206,8 @@ type GetAccountDataFromHdNodeOptions = {
hdNode: BIP32Interface;
format?: AddressFormats;
network?: Network;
accountIndex?: number;
index?: number;
account?: number;
addressIndex?: number;
};

type GetAllAccountsFromHDNodeOptions = Omit<GetAccountDataFromHdNodeOptions, "format">;
Expand Down
27 changes: 8 additions & 19 deletions packages/sdk/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,23 @@ export function createTransaction(
return bitcoin.payments[type]({ pubkey: key, network: networkObj });
}

export function getDerivationPath(formatType: AddressFormats, accountIndex = 0, index = 0) {
export function getDerivationPath(formatType: AddressFormats, account = 0, addressIndex = 0) {
const pathFormat = {
legacy: `m/44'/0'/${accountIndex}'/0/${index}`,
segwit: `m/49'/0'/${accountIndex}'/0/${index}`,
bech32: `m/84'/0'/${accountIndex}'/0/${index}`,
taproot: `m/86'/0'/${accountIndex}'/0/${index}`
legacy: `m/44'/0'/${account}'/0/${addressIndex}`,
segwit: `m/49'/0'/${account}'/0/${addressIndex}`,
bech32: `m/84'/0'/${account}'/0/${addressIndex}`,
taproot: `m/86'/0'/${account}'/0/${addressIndex}`
};
return pathFormat[formatType];
}

export function getPathLevels(fullDerivationPath: string) {
const [, purpose, coinType, account, change, addressIndex] = fullDerivationPath.split("/");
return {
purpose: purpose.replace("'", ""),
coinType: coinType.replace("'", ""),
account: account.replace("'", ""),
change,
addressIndex
};
}

export function hdNodeToChild(
node: BIP32Interface,
formatType: AddressFormats = "legacy",
index = 0,
accountIndex = 0
addressIndex = 0,
account = 0
) {
const fullDerivationPath = getDerivationPath(formatType, accountIndex, index);
const fullDerivationPath = getDerivationPath(formatType, account, addressIndex);

return node.derivePath(fullDerivationPath);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/sdk/src/wallet/Ordit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ export class Ordit {
}
}

getAddress(type: AddressFormats, accountIndex: number, addressIndex: number) {
generateAddress(type: AddressFormats, account: number, addressIndex: number) {
if (!this.#hdNode) throw new Error("No HD node found. Please reinitialize with BIP39 words or seed.");

return getAccountDataFromHdNode({
hdNode: this.#hdNode,
format: type,
network: this.#network,
accountIndex,
index: addressIndex
account,
addressIndex
});
}

Expand Down

0 comments on commit f05ed15

Please sign in to comment.