From a84295347f2db1364218fbb76611100d2ea22c9f Mon Sep 17 00:00:00 2001 From: Vladimir Borovik Date: Mon, 12 Aug 2024 13:24:48 +0700 Subject: [PATCH] feat: helpers for multiple fills --- .../hash-lock/hash-lock.spec.ts | 15 ++++++++++ src/cross-chain-order/hash-lock/hash-lock.ts | 6 +++- src/escrow-factory/escrow-factory.ts | 28 ++++++++++++++++--- 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/cross-chain-order/hash-lock/hash-lock.spec.ts b/src/cross-chain-order/hash-lock/hash-lock.spec.ts index 7287f30..42fca3f 100644 --- a/src/cross-chain-order/hash-lock/hash-lock.spec.ts +++ b/src/cross-chain-order/hash-lock/hash-lock.spec.ts @@ -38,4 +38,19 @@ describe('HashLock', () => { getBytesCount(HashLock.forMultipleFills(leaves).toString()) ).toEqual(32n) }) + + it('should return proof', () => { + const secrets = [ + '0x6466643931343237333333313437633162386632316365646666323931643738', + '0x3131353932633266343034343466363562333230313837353438356463616130', + '0x6634376135663837653765303462346261616566383430303662303336386635' + ] + + const leaves = HashLock.getMerkleLeaves(secrets) + + expect(HashLock.getProof(leaves, 0)).toEqual([ + '0x3009d711fa0454e4a4f0d553fcfa67f20e0d67571d7e06f2105814c6b123ba55', + '0x70e897a17a55b03df83541f6420507b3482da14f2489ef7ea4d9a94cf30d1c06' + ]) + }) }) diff --git a/src/cross-chain-order/hash-lock/hash-lock.ts b/src/cross-chain-order/hash-lock/hash-lock.ts index 0d86a7e..3192acf 100644 --- a/src/cross-chain-order/hash-lock/hash-lock.ts +++ b/src/cross-chain-order/hash-lock/hash-lock.ts @@ -31,6 +31,10 @@ export class HashLock { ) } + public static getProof(leaves: MerkleLeaf[], idx: number): MerkleLeaf[] { + return SimpleMerkleTree.of(leaves).getProof(idx) as MerkleLeaf[] + } + public static fromString(value: string): HashLock { assert( isHexBytes(value) && getBytesCount(value) === 32n, @@ -70,4 +74,4 @@ export class HashLock { } } -type MerkleLeaf = string & {_tag: 'MerkleLeaf'} +export type MerkleLeaf = string & {_tag: 'MerkleLeaf'} diff --git a/src/escrow-factory/escrow-factory.ts b/src/escrow-factory/escrow-factory.ts index 111fe25..7c4656a 100644 --- a/src/escrow-factory/escrow-factory.ts +++ b/src/escrow-factory/escrow-factory.ts @@ -1,9 +1,9 @@ -import {Address} from '@1inch/fusion-sdk' -import {getCreate2Address, keccak256} from 'ethers' +import {Address, Interaction} from '@1inch/fusion-sdk' +import {AbiCoder, getCreate2Address, keccak256} from 'ethers' import {getBytesCount, isHexBytes, trim0x} from '@1inch/byte-utils' import assert from 'assert' -import {Immutables} from '../immutables' -import {DstImmutablesComplement} from '../immutables/dst-immutables-complement' +import {Immutables, DstImmutablesComplement} from '../immutables' +import {MerkleLeaf} from '../cross-chain-order/hash-lock/hash-lock' export class EscrowFactory { constructor(public readonly address: Address) {} @@ -103,4 +103,24 @@ export class EscrowFactory { implementationAddress ) } + + public getMultipleFillInteraction( + proof: MerkleLeaf[], + idx: number, + secretHash: string + ): Interaction { + return new Interaction( + this.address, + AbiCoder.defaultAbiCoder().encode( + [ + `( + bytes32[] proof, + uint256 idx, + bytes32 secretHash, + )` + ], + [{proof, idx, secretHash}] + ) + ) + } }