-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor address derivation, discovery and crypto providers
- Loading branch information
Showing
21 changed files
with
457 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const range = require('./helpers/range') | ||
const {toBip32StringPath} = require('./helpers/bip32') | ||
const {packAddress} = require('cardano-crypto.js') | ||
|
||
const AddressManager = ({ | ||
accountIndex, | ||
addressLimit, | ||
cryptoProvider, | ||
derivationScheme, | ||
disableCaching, // good for tests | ||
isChange, | ||
}) => { | ||
const state = { | ||
deriveAddressMemo: {}, | ||
} | ||
|
||
function discoverAddresses() { | ||
const childIndexBegin = derivationScheme.startAddressIndex | ||
const childIndexEnd = childIndexBegin + addressLimit | ||
const absDerivationPaths = range(childIndexBegin, childIndexEnd) | ||
.map((i) => [accountIndex, isChange ? 1 : 0, i]) | ||
.map(derivationScheme.toAbsoluteDerivationPath) | ||
|
||
return deriveAddresses(absDerivationPaths) | ||
} | ||
|
||
async function discoverAddressesWithMeta() { | ||
return (await discoverAddresses()).map((address) => { | ||
return { | ||
address, | ||
bip32StringPath: toBip32StringPath(getAddressToAbsPathMapping()[address]), | ||
} | ||
}) | ||
} | ||
|
||
function deriveAddresses(absDerivationPaths) { | ||
return Promise.all(absDerivationPaths.map(deriveAddress)) | ||
} | ||
|
||
async function deriveAddress(absDerivationPath) { | ||
// in derivation scheme 1, the middle part of the derivation path is skipped | ||
const memoKey = JSON.stringify(absDerivationPath) | ||
|
||
if (!state.deriveAddressMemo[memoKey] || disableCaching) { | ||
const xpub = await cryptoProvider.deriveXpub(absDerivationPath) | ||
const hdPassphrase = | ||
derivationScheme.type === 'v1' ? await cryptoProvider.getHdPassphrase() : undefined | ||
|
||
state.deriveAddressMemo[memoKey] = packAddress( | ||
absDerivationPath, | ||
xpub, | ||
hdPassphrase, | ||
derivationScheme.number | ||
) | ||
} | ||
|
||
return state.deriveAddressMemo[memoKey] | ||
} | ||
|
||
function getAddressToAbsPathMapping() { | ||
const result = {} | ||
Object.keys(state.deriveAddressMemo).map((key) => { | ||
result[state.deriveAddressMemo[key]] = JSON.parse(key) | ||
}) | ||
|
||
return result | ||
} | ||
|
||
return { | ||
discoverAddresses, | ||
discoverAddressesWithMeta, | ||
getAddressToAbsPathMapping, | ||
_deriveAddress: deriveAddress, | ||
_deriveAddresses: deriveAddresses, | ||
} | ||
} | ||
|
||
module.exports = AddressManager |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.