Skip to content

Commit

Permalink
test with given seedhash. When a seedhash is given as parameters, the…
Browse files Browse the repository at this point in the history
… seed/seedhash are not returned in CreateHtlcResult. Set the properties as optional accordingly
  • Loading branch information
Jouzo committed Aug 2, 2021
1 parent 3b543a6 commit fb8f306
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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")
})
Expand Down
10 changes: 5 additions & 5 deletions packages/jellyfish-api-core/src/category/spv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<CreateHtlcResult>}
*/
async createHtlc (receiverPubKey: string, ownerPubKey: string, options: CreateHtlcOptions): Promise<CreateHtlcResult> {
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')
}
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit fb8f306

Please sign in to comment.