-
Notifications
You must be signed in to change notification settings - Fork 37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(jellyfish-api-core): add deriveAddresses function #1974
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
87193e4
feat: add deriveAddresses function
877d121
Update packages/jellyfish-api-core/__tests__/category/misc/deriveAddr…
DocteurPing 174b368
Update packages/jellyfish-api-core/__tests__/category/misc/deriveAddr…
DocteurPing f94d943
make error check more specific for deriveAddresses test
4a28087
add test deriveAddresses for pkh and sh descriptor
549dce9
Update packages/jellyfish-api-core/__tests__/category/misc/deriveAddr…
DocteurPing 213be03
move the await outside the expect for deriveAddresses tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
131 changes: 131 additions & 0 deletions
131
packages/jellyfish-api-core/__tests__/category/misc/deriveAddresses.test.ts
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,131 @@ | ||
import { RegTestContainer } from '@defichain/testcontainers/dist/index' | ||
import { ContainerAdapterClient } from '../../container_adapter_client' | ||
import { RpcApiError } from '../../../src' | ||
|
||
describe('derive addresses', () => { | ||
const container = new RegTestContainer() | ||
const client = new ContainerAdapterClient(container) | ||
|
||
beforeAll(async () => { | ||
await container.start() | ||
}) | ||
|
||
afterAll(async () => { | ||
await container.stop() | ||
}) | ||
|
||
it('should derive an address without range', async () => { | ||
const descriptor = 'wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/0)#t6wfjs64' | ||
const address = ['bcrt1qjqmxmkpmxt80xz4y3746zgt0q3u3ferr34acd5'] | ||
expect(await client.misc.deriveAddresses(descriptor)).toStrictEqual(address) | ||
}) | ||
|
||
it('should derive an address with range', async () => { | ||
const descriptor = 'wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/*)#kft60nuy' | ||
const address = ['bcrt1qhku5rq7jz8ulufe2y6fkcpnlvpsta7rq4442dy', 'bcrt1qpgptk2gvshyl0s9lqshsmx932l9ccsv265tvaq'] | ||
expect(await client.misc.deriveAddresses(descriptor, [1, 2])).toStrictEqual(address) | ||
DocteurPing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
it('should derive an address without range pkh descriptor', async () => { | ||
const descriptor = 'pkh([d6043800/0\'/0\'/18\']03efdee34c0009fd175f3b20b5e5a5517fd5d16746f2e635b44617adafeaebc388)#4ahsl9pk' | ||
const address = ['ms7ruzvL4atCu77n47dStMb3of6iScS8kZ'] | ||
expect(await client.misc.deriveAddresses(descriptor)).toStrictEqual(address) | ||
}) | ||
|
||
it('should derive an address without range sh descriptor', async () => { | ||
const descriptor = 'sh(wpkh(tpubDCJtdt5dgJpdhW4MtaVYDhG4T4tF6jcLR1PxL43q9pq1mxvXgMS9Mzw1HnXG15vxUGQJMMSqCQHMTy3F1eW5VkgVroWzchsPD5BUojrcWs8/0/*))#e8nc36sh' | ||
const address = ['2NA1PWXse3JjGGMcyjMETTCQnTpsLtQETQW', '2MzzJMkCmixHarCD47sFavseb3uTrPnxKav'] | ||
expect(await client.misc.deriveAddresses(descriptor, [0, 1])).toStrictEqual(address) | ||
}) | ||
|
||
it('should raise an error if the range is specified for un-ranged descriptor', async () => { | ||
const descriptor = 'wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/0)#t6wfjs64' | ||
const promise = client.misc.deriveAddresses(descriptor, [1, 3]) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -8, | ||
message: 'Range should not be specified for an un-ranged descriptor', | ||
method: 'deriveaddresses' | ||
} | ||
}) | ||
}) | ||
|
||
it('should raise an error if there is no range for a range descriptor', async () => { | ||
const descriptor = 'wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/*)#kft60nuy' | ||
const promise = client.misc.deriveAddresses(descriptor) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -8, | ||
message: 'Range must be specified for a ranged descriptor', | ||
method: 'deriveaddresses' | ||
} | ||
}) | ||
}) | ||
|
||
it('should raise an error if end of range is too high', async () => { | ||
const descriptor = 'wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/*)' | ||
const promise = client.misc.deriveAddresses(descriptor, [100000]) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -8, | ||
message: 'Range must be specified as end or as [begin,end]', | ||
method: 'deriveaddresses' | ||
} | ||
}) | ||
}) | ||
|
||
it('should raise an error if range is too large', async () => { | ||
const descriptor = 'wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/*)' | ||
const promise = client.misc.deriveAddresses(descriptor, [1, 1000000000]) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -8, | ||
message: 'Range is too large', | ||
method: 'deriveaddresses' | ||
} | ||
}) | ||
}) | ||
|
||
it('should raise an error if range is less than 0', async () => { | ||
const descriptor = 'wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/*)' | ||
const promise = client.misc.deriveAddresses(descriptor, [-1, 2]) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -8, | ||
message: 'Range should be greater or equal than 0', | ||
method: 'deriveaddresses' | ||
} | ||
}) | ||
}) | ||
|
||
it('should raise an error if end of range is smaller than the start', async () => { | ||
const descriptor = 'wpkh(tprv8ZgxMBicQKsPd7Uf69XL1XwhmjHopUGep8GuEiJDZmbQz6o58LninorQAfcKZWARbtRtfnLcJ5MQ2AtHcQJCCRUcMRvmDUjyEmNUWwx8UbK/1/1/*)' | ||
const promise = client.misc.deriveAddresses(descriptor, [2, 0]) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -8, | ||
message: 'Range specified as [begin,end] must not have begin after end', | ||
method: 'deriveaddresses' | ||
} | ||
}) | ||
}) | ||
|
||
it('should raise an error if there is wrong drescriptor', async () => { | ||
const descriptor = 'descriptor' | ||
const promise = client.misc.deriveAddresses(descriptor, [0, 2]) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toMatchObject({ | ||
payload: { | ||
code: -5, | ||
message: 'Missing checksum', | ||
method: 'deriveaddresses' | ||
} | ||
}) | ||
}) | ||
}) |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should also check for the exact error thrown.