Skip to content

Commit

Permalink
chore(wallet): allow to specify custom account and index
Browse files Browse the repository at this point in the history
  • Loading branch information
thedoublejay committed Jun 20, 2024
1 parent c5b54f8 commit 0c60312
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 5 additions & 4 deletions packages/sdk/src/addresses/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,18 +143,19 @@ export function getAccountDataFromHdNode({
return accountData
}

export function getAllAccountsFromHdNode({ hdNode, network = "testnet" }: GetAllAccountsFromHDNodeOptions) {
export function getAllAccountsFromHdNode({ hdNode, network = "testnet", account = 0, addressIndex = 0 }: GetAllAccountsFromHDNodeOptions) {
const accounts: Account[] = []
const addressTypesList = Object.values(addressTypeToName) as AddressFormats[]

addressTypesList.forEach((addrType) => {
const account = getAccountDataFromHdNode({
const walletAccount = getAccountDataFromHdNode({
hdNode,
format: addrType,
network
network,
account, addressIndex
})

accounts.push(account)
accounts.push(walletAccount)
})

return accounts
Expand Down
8 changes: 5 additions & 3 deletions packages/sdk/src/wallet/Ordit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class Ordit {
selectedAddressType: AddressFormats | undefined
selectedAddress: string | undefined

constructor({ wif, seed, privateKey, bip39, network = "testnet", type = "legacy" }: WalletOptions) {
constructor({ wif, seed, privateKey, bip39, network = "testnet", type = "legacy", account = 0, addressIndex = 0 }: WalletOptions) {
this.#network = network
const networkObj = getNetwork(network)
const format = addressNameToType[type]
Expand Down Expand Up @@ -65,7 +65,7 @@ export class Ordit {

this.#hdNode = hdNode

const accounts = getAllAccountsFromHdNode({ hdNode, network })
const accounts = getAllAccountsFromHdNode({ hdNode, network, account, addressIndex })

const pkBuf = Buffer.from(accounts[0].priv, "hex")
this.#keyPair = ECPair.fromPrivateKey(pkBuf, { network: networkObj })
Expand All @@ -79,7 +79,7 @@ export class Ordit {

this.#hdNode = hdNode

const accounts = getAllAccountsFromHdNode({ hdNode, network })
const accounts = getAllAccountsFromHdNode({ hdNode, network, account, addressIndex })
this.#keyPair = accounts[0].child

this.publicKey = this.#keyPair.publicKey.toString("hex")
Expand Down Expand Up @@ -266,6 +266,8 @@ export type WalletOptions = {
bip39?: string
network?: Network
type?: AddressFormats
account?: number;
addressIndex?: number;
}

export type Address = ReturnType<typeof getAddressesFromPublicKey>[0]
Expand Down

0 comments on commit 0c60312

Please sign in to comment.