diff --git a/packages/jellyfish-api-core/__tests__/category/spv/createHtlc.test.ts b/packages/jellyfish-api-core/__tests__/category/spv/createHtlc.test.ts index 030466d2a5..4813079b7b 100644 --- a/packages/jellyfish-api-core/__tests__/category/spv/createHtlc.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/spv/createHtlc.test.ts @@ -26,9 +26,9 @@ describe('Spv', () => { expect(htlc.address.length).toStrictEqual(35) expect(typeof htlc.redeemScript).toStrictEqual('string') expect(typeof htlc.seed).toStrictEqual('string') - expect(htlc.seed.length).toStrictEqual(64) + expect(htlc.seed?.length).toStrictEqual(64) expect(typeof htlc.seedhash).toStrictEqual('string') - expect(htlc.seedhash.length).toStrictEqual(64) + expect(htlc.seedhash?.length).toStrictEqual(64) const decodedScript = await container.call('spv_decodehtlcscript', [htlc.redeemScript]) expect(decodedScript.sellerkey).toStrictEqual(pubKeyA) @@ -71,20 +71,40 @@ describe('Spv', () => { await expect(promise).rejects.toThrow("RpcApiError: 'Invalid block denominated relative timeout', code: -3, method: spv_createhtlc") }) - it('should not createHtlc with seed length != 32', async () => { + it('should createHtlc with given seedhash', 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 timeout = 10 + const seedhash = '071adfe943d7e61e8f25161aa543042e6ca378262c18452f770a88c9f1d10a3a' + + const htlc = await client.spv.createHtlc(pubKeyA, pubKeyB, { timeout: `${timeout}`, seedhash }) + expect(typeof htlc.address).toStrictEqual('string') + expect(htlc.address.length).toStrictEqual(35) + expect(typeof htlc.redeemScript).toStrictEqual('string') + expect(htlc.seed).toBeUndefined() + expect(htlc.seedhash).toBeUndefined() + + const decodedScript = await container.call('spv_decodehtlcscript', [htlc.redeemScript]) + expect(decodedScript.sellerkey).toStrictEqual(pubKeyA) + expect(decodedScript.buyerkey).toStrictEqual(pubKeyB) + expect(decodedScript.blocks).toStrictEqual(timeout) + expect(decodedScript.hash).toStrictEqual(seedhash) + }) + + it('should not createHtlc with seedhash length != 32', 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 promise = client.spv.createHtlc(pubKeyA, pubKeyB, { timeout: '10', seed: '00' }) + const promise = client.spv.createHtlc(pubKeyA, pubKeyB, { timeout: '10', seedhash: '00' }) await expect(promise).rejects.toThrow(RpcApiError) await expect(promise).rejects.toThrow("RpcApiError: 'Invalid hash image length, 32 (SHA256) accepted', code: -3, method: spv_createhtlc") }) - it('should not createHtlc with invalid seed', async () => { + it('should not createHtlc with invalid seedhash', 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 promise = client.spv.createHtlc(pubKeyA, pubKeyB, { timeout: '8', seed: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' }) // 32 char seed with non-hex char + const promise = client.spv.createHtlc(pubKeyA, pubKeyB, { timeout: '8', seedhash: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' }) // 32 char seed with non-hex char await expect(promise).rejects.toThrow(RpcApiError) await expect(promise).rejects.toThrow("RpcApiError: 'Invalid hash image', code: -3, method: spv_createhtlc") }) diff --git a/packages/jellyfish-api-core/src/category/spv.ts b/packages/jellyfish-api-core/src/category/spv.ts index a2b79837cb..8d9332cfc8 100644 --- a/packages/jellyfish-api-core/src/category/spv.ts +++ b/packages/jellyfish-api-core/src/category/spv.ts @@ -65,11 +65,11 @@ export class Spv { * @param {string} ownerPubKey The public key of the recipient of the refund * @param {CreateHtlcOptions} options * @param {string} options.timeout Timeout of the contract (denominated in blocks) relative to its placement in the blockchain. Minimum 9. See HTLC_MINIMUM_BLOCK_COUNT - * @param {string} [options.seed] SHA256 hash of the seed. If none provided one will be generated + * @param {string} [options.seedhash] SHA256 hash of the seed. If none provided one will be generated * @return {Promise} */ async createHtlc (receiverPubKey: string, ownerPubKey: string, options: CreateHtlcOptions): Promise { - return await this.client.call('spv_createhtlc', [receiverPubKey, ownerPubKey, options.timeout, options.seed], 'number') + return await this.client.call('spv_createhtlc', [receiverPubKey, ownerPubKey, options.timeout, options.seedhash], 'number') } } @@ -99,7 +99,7 @@ export interface CreateHtlcOptions { /** Timeout of the contract (denominated in blocks) relative to its placement in the blockchain. Minimum 9. See HTLC_MINIMUM_BLOCK_COUNT */ timeout: string /** SHA256 hash of the seed. If none provided one will be generated */ - seed?: string + seedhash?: string } export interface CreateHtlcResult { @@ -108,7 +108,7 @@ export interface CreateHtlcResult { /** Hex-encoded redemption script */ redeemScript: string /** Hex-encoded seed */ - seed: string + seed?: string /** Hex-encoded seed hash */ - seedhash: string + seedhash?: string }