Skip to content

Commit

Permalink
Merge pull request #6 from kevzzsk/kevin/get-address
Browse files Browse the repository at this point in the history
refactor!: update getAddress to generate new address based on provided path levels
  • Loading branch information
iamcrazycoder authored Jul 18, 2023
2 parents 77e838a + f05ed15 commit 128889e
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
35 changes: 27 additions & 8 deletions packages/sdk/src/addresses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import BIP32Factory, { BIP32Interface } from "bip32";

import { Network } from "../config/types";
import { getWalletKeys } from "../keys";
import { createTransaction, getNetwork, hdNodeToChild, toXOnly } from "../utils";
import { createTransaction, getDerivationPath, getNetwork, toXOnly } from "../utils";
import { AddressFormats, addressFormats, addressNameToType, AddressTypes, addressTypeToName } from "./formats";

export function getAddressFormat(address: string, network: Network) {
Expand Down Expand Up @@ -119,32 +119,42 @@ export async function getAddresses({
export function getAccountDataFromHdNode({
hdNode,
format = "legacy",
network = "testnet"
network = "testnet",
account = 0,
addressIndex = 0
}: GetAccountDataFromHdNodeOptions) {
if (!hdNode) {
throw new Error("Invalid options provided.");
}

const addressType = addressNameToType[format];
//
const child = hdNodeToChild(hdNode, format, 0);

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
type: addressType,
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 @@ -171,9 +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: Derivation;
};

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

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

export function hdNodeToChild(node: BIP32Interface, formatType: AddressFormats = "legacy", index = 0) {
const fullDerivationPath = DERIVATION_PATHS_WITHOUT_INDEX[formatType] + index;
export function getDerivationPath(formatType: AddressFormats, account = 0, addressIndex = 0) {
const pathFormat = {
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 hdNodeToChild(
node: BIP32Interface,
formatType: AddressFormats = "legacy",
addressIndex = 0,
account = 0
) {
const fullDerivationPath = getDerivationPath(formatType, account, addressIndex);

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

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

const accounts: Account[] = [];
for (let i = 0; i < count; i++) {
const account = getAccountDataFromHdNode({ hdNode: this.#hdNode, format: type, network: this.#network });

accounts.push(account);
}

this.allAddresses.push(...accounts);

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

signPsbt(value: string, { finalized = true, instantBuy = false }: SignPSBTOptions) {
Expand Down

0 comments on commit 128889e

Please sign in to comment.