Skip to content

Commit

Permalink
US-1875 Added change index to the bitcoin library (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
Freshenext authored Nov 28, 2023
1 parent 4d725ef commit 546622b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/bitcoin/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rsksmart/rif-wallet-bitcoin",
"version": "1.2.1",
"version": "1.2.2",
"description": "Bitcoin Library to send bitcoin, receive bitcoin by generating an address, and much more.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
13 changes: 7 additions & 6 deletions packages/bitcoin/src/core/BIP.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ export class BIP {
this.AddressFactory = new AddressFactory(this.bipNumber, this.networkInfo)
}

getAddress (index = 0): string {
getAddress (index = 0, changeIndex = 0): string {
const bip32Instance = this.bip32Root.derivePath(
this.PathDerivator.getAddressDerivation(index)
this.PathDerivator.getAddressDerivation(index, changeIndex)
)
return this.AddressFactory.getAddress(bip32Instance.publicKey)
}
Expand All @@ -113,12 +113,13 @@ export class BIP {
)
}

async fetchExternalAvailableAddress () {
async fetchExternalAvailableAddress ({ changeIndex = 0 }) {
const { index } =
await this.fetcher.fetchXpubNextUnusedIndex(
this.accountPublicKey
this.accountPublicKey,
changeIndex,
)
return this.getAddress(index)
return this.getAddress(index, changeIndex)
}

async fetchExternalAvailableAddresses ({ changeIndex = 0, knownLastUsedIndex = 0, maxIndexesToFetch = 5 }) {
Expand All @@ -129,7 +130,7 @@ export class BIP {
maxIndexesToFetch,
)
return availableIndexes.reduce<string[]>((prev, curr) => {
prev.push(this.getAddress(curr))
prev.push(this.getAddress(curr, changeIndex))
return prev
}, [])
}
Expand Down
4 changes: 2 additions & 2 deletions packages/bitcoin/src/core/PathDerivator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export class PathDerivator {
return `m/${this.purpose}'/${this.coinTypeNumber}'/${this.account}'/0`
}

getAddressDerivation (index: number | string) {
return `m/${this.purpose}'/${this.coinTypeNumber}'/${this.account}'/${this.changeIndex}/${index}`
getAddressDerivation (index: number | string, changeIndex?: number | string) {
return `m/${this.purpose}'/${this.coinTypeNumber}'/${this.account}'/${changeIndex ?? this.changeIndex}/${index}`
}
}

0 comments on commit 546622b

Please sign in to comment.