-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
135 changes: 135 additions & 0 deletions
135
packages/jellyfish-api-core/__tests__/category/spv/claimHtlc.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,135 @@ | ||
import { MasterNodeRegTestContainer } from '@defichain/testcontainers' | ||
import { RpcApiError } from '@defichain/jellyfish-api-core' | ||
import { ContainerAdapterClient } from '../../container_adapter_client' | ||
|
||
describe('Spv', () => { | ||
const container = new MasterNodeRegTestContainer() | ||
const client = new ContainerAdapterClient(container) | ||
|
||
beforeAll(async () => { | ||
await container.start() | ||
await container.waitForReady() | ||
|
||
await container.call('spv_fundaddress', [await container.call('spv_getnewaddress')]) // Funds 1 BTC | ||
}) | ||
|
||
afterAll(async () => { | ||
await container.stop() | ||
}) | ||
|
||
it('should claimHtlc', async () => { | ||
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10']) | ||
|
||
await container.call('spv_sendtoaddress', [htlc.address, 0.1]) // Funds HTLC address | ||
const claimedHtlc = await client.spv.claimHtlc( | ||
htlc.address, | ||
await container.call('spv_getnewaddress'), | ||
{ seed: htlc.seed } | ||
) | ||
expect(typeof claimedHtlc.txid).toStrictEqual('string') | ||
expect(claimedHtlc.sendmessage).toStrictEqual('Success') | ||
}) | ||
|
||
it('should not claimHtlc when no unspent HTLC outputs found', async () => { | ||
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10']) | ||
|
||
const promise = client.spv.claimHtlc( | ||
htlc.address, | ||
await container.call('spv_getnewaddress'), | ||
{ seed: htlc.seed } | ||
) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toThrow("RpcApiError: 'No unspent HTLC outputs found', code: -4, method: spv_claimhtlc") | ||
}) | ||
|
||
it('should not claimHtlc when provided seed is not in hex form', async () => { | ||
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10']) | ||
|
||
const promise = client.spv.claimHtlc( | ||
htlc.address, | ||
await container.call('spv_getnewaddress'), | ||
{ seed: 'XXXX' } | ||
) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toThrow("RpcApiError: 'Provided seed is not in hex form', code: -5, method: spv_claimhtlc") | ||
}) | ||
|
||
it('should not claimHtlc when seed provided does not match seed hash in contract', async () => { | ||
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10']) | ||
|
||
const promise = client.spv.claimHtlc( | ||
htlc.address, | ||
await container.call('spv_getnewaddress'), | ||
{ seed: '00' } | ||
) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toThrow("RpcApiError: 'Seed provided does not match seed hash in contract', code: -5, method: spv_claimhtlc") | ||
}) | ||
|
||
it('should not claimHtlc with invalid address', async () => { | ||
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10']) | ||
|
||
const promise = client.spv.claimHtlc( | ||
'XXXX', | ||
await container.call('spv_getnewaddress'), | ||
{ seed: htlc.seed } | ||
) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toThrow("RpcApiError: 'Invalid address', code: -5, method: spv_claimhtlc") | ||
}) | ||
|
||
it('should not claimHtlc when redeem script not found in wallet', async () => { | ||
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10']) | ||
const randomAddress = '2Mu4edSkC5gKVwYayfDq2fTFwT6YD4mujSX' | ||
|
||
const promise = client.spv.claimHtlc( | ||
randomAddress, | ||
await container.call('spv_getnewaddress'), | ||
{ seed: htlc.seed } | ||
) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toThrow("RpcApiError: 'Redeem script not found in wallet', code: -4, method: spv_claimhtlc") | ||
}) | ||
|
||
it('should not claimHtlc with invalid destination address', async () => { | ||
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10']) | ||
|
||
const promise = client.spv.claimHtlc( | ||
htlc.address, | ||
'XXXX', | ||
{ seed: htlc.seed } | ||
) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toThrow("RpcApiError: 'Invalid destination address', code: -5, method: spv_claimhtlc") | ||
}) | ||
|
||
it('should not claimHtlc with not enough funds to cover fee', async () => { | ||
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')]) | ||
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10']) | ||
|
||
await container.call('spv_sendtoaddress', [htlc.address, 0.00000546]) // Funds HTLC address with dust | ||
|
||
const promise = client.spv.claimHtlc( | ||
htlc.address, | ||
await container.call('spv_getnewaddress'), | ||
{ seed: htlc.seed } | ||
) | ||
await expect(promise).rejects.toThrow(RpcApiError) | ||
await expect(promise).rejects.toThrow("RpcApiError: 'Not enough funds to cover fee', code: -1, method: spv_claimhtlc") | ||
}) | ||
}) |
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