From 786c4e7ff73a7824dbde513cf60c94d0988fc468 Mon Sep 17 00:00:00 2001 From: HFazelinia Date: Wed, 3 Apr 2024 14:23:55 +0100 Subject: [PATCH] update tss sign functions --- .changeset/tough-masks-pull.md | 6 ++++ packages/chains/bitcoin/lib/BitcoinChain.ts | 25 +++++++++-------- packages/chains/bitcoin/lib/types.ts | 5 ++++ .../chains/bitcoin/tests/BitcoinChain.spec.ts | 28 ++++++++++++++----- packages/chains/evm/lib/EvmChain.ts | 6 ++-- packages/chains/evm/lib/types.ts | 9 ++++-- packages/chains/evm/tests/EvmChain.spec.ts | 3 +- packages/chains/evm/tests/TestChain.ts | 4 +-- packages/chains/evm/tests/TestUtils.ts | 6 +++- 9 files changed, 64 insertions(+), 28 deletions(-) create mode 100644 .changeset/tough-masks-pull.md diff --git a/.changeset/tough-masks-pull.md b/.changeset/tough-masks-pull.md new file mode 100644 index 0000000..9459e2c --- /dev/null +++ b/.changeset/tough-masks-pull.md @@ -0,0 +1,6 @@ +--- +'@rosen-chains/bitcoin': major +'@rosen-chains/evm': major +--- + +add signatureRecovery to return data of required signFunction diff --git a/packages/chains/bitcoin/lib/BitcoinChain.ts b/packages/chains/bitcoin/lib/BitcoinChain.ts index 27699f4..6ca749d 100644 --- a/packages/chains/bitcoin/lib/BitcoinChain.ts +++ b/packages/chains/bitcoin/lib/BitcoinChain.ts @@ -18,7 +18,12 @@ import { } from '@rosen-chains/abstract-chain'; import AbstractBitcoinNetwork from './network/AbstractBitcoinNetwork'; import BitcoinTransaction from './BitcoinTransaction'; -import { BitcoinConfigs, BitcoinTx, BitcoinUtxo } from './types'; +import { + BitcoinConfigs, + BitcoinTx, + BitcoinUtxo, + TssSignFunction, +} from './types'; import Serializer from './Serializer'; import { Psbt, Transaction, address, payments, script } from 'bitcoinjs-lib'; import JsonBigInt from '@rosen-bridge/json-bigint'; @@ -33,7 +38,7 @@ class BitcoinChain extends AbstractUtxoChain { declare configs: BitcoinConfigs; CHAIN = BITCOIN_CHAIN; extractor: BitcoinRosenExtractor; - protected signFunction: (txHash: Uint8Array) => Promise; + protected signFunction: TssSignFunction; protected lockScript: string; protected signingScript: Buffer; @@ -42,7 +47,7 @@ class BitcoinChain extends AbstractUtxoChain { configs: BitcoinConfigs, feeRatioDivisor: bigint, tokens: RosenTokens, - signFunction: (txHash: Uint8Array) => Promise, + signFunction: TssSignFunction, logger?: AbstractLogger ) { super(network, configs, feeRatioDivisor, logger); @@ -398,14 +403,12 @@ class BitcoinChain extends AbstractUtxoChain { Transaction.SIGHASH_ALL ); - const signatureHex = this.signFunction(signMessage).then( - (signatureHex: string) => { - this.logger.debug( - `Input [${i}] of tx [${bitcoinTx.txId}] is signed. signature: ${signatureHex}` - ); - return signatureHex; - } - ); + const signatureHex = this.signFunction(signMessage).then((response) => { + this.logger.debug( + `Input [${i}] of tx [${bitcoinTx.txId}] is signed. signature: ${response.signature}` + ); + return response.signature; + }); signaturePromises.push(signatureHex); } diff --git a/packages/chains/bitcoin/lib/types.ts b/packages/chains/bitcoin/lib/types.ts index 0860c1f..7c74d86 100644 --- a/packages/chains/bitcoin/lib/types.ts +++ b/packages/chains/bitcoin/lib/types.ts @@ -35,3 +35,8 @@ export interface BitcoinTx { inputs: BitcoinTxInput[]; outputs: BitcoinTxOutput[]; } + +export type TssSignFunction = (txHash: Uint8Array) => Promise<{ + signature: string; + signatureRecovery: string; +}>; diff --git a/packages/chains/bitcoin/tests/BitcoinChain.spec.ts b/packages/chains/bitcoin/tests/BitcoinChain.spec.ts index c380cb9..d18850a 100644 --- a/packages/chains/bitcoin/tests/BitcoinChain.spec.ts +++ b/packages/chains/bitcoin/tests/BitcoinChain.spec.ts @@ -12,6 +12,7 @@ import { BitcoinTransaction, BitcoinUtxo, SEGWIT_INPUT_WEIGHT_UNIT, + TssSignFunction, } from '../lib'; import TestBitcoinNetwork from './network/TestBitcoinNetwork'; import * as testData from './testData'; @@ -43,10 +44,14 @@ describe('BitcoinChain', () => { aggregatedPublicKey: testData.lockAddressPublicKey, txFeeSlippage: 10, }; - const mockedSignFn = () => Promise.resolve(''); + const mockedSignFn = () => + Promise.resolve({ + signature: '', + signatureRecovery: '', + }); const generateChainObject = ( network: TestBitcoinNetwork, - signFn: (txHash: Uint8Array) => Promise = mockedSignFn + signFn: TssSignFunction = mockedSignFn ) => { return new BitcoinChain( network, @@ -491,12 +496,18 @@ describe('BitcoinChain', () => { */ it('should return PaymentTransaction of the signed transaction', async () => { // mock a sign function to return signature - const signFunction = async (hash: Uint8Array): Promise => { + const signFunction: TssSignFunction = async (hash: Uint8Array) => { const hashHex = Buffer.from(hash).toString('hex'); if (hashHex === testData.transaction2HashMessage0) - return testData.transaction2Signature0; + return { + signature: testData.transaction2Signature0, + signatureRecovery: '', + }; else if (hashHex === testData.transaction2HashMessage1) - return testData.transaction2Signature1; + return { + signature: testData.transaction2Signature1, + signatureRecovery: '', + }; else throw Error( `TestError: sign function is called with wrong message [${hashHex}]` @@ -534,12 +545,15 @@ describe('BitcoinChain', () => { */ it('should throw error when at least signing of one message is failed', async () => { // mock a sign function to throw error - const signFunction = async (hash: Uint8Array): Promise => { + const signFunction: TssSignFunction = async (hash: Uint8Array) => { if ( Buffer.from(hash).toString('hex') === testData.transaction2HashMessage0 ) - return testData.transaction2Signature0; + return { + signature: testData.transaction2Signature0, + signatureRecovery: '', + }; else throw Error(`TestError: sign failed`); }; diff --git a/packages/chains/evm/lib/EvmChain.ts b/packages/chains/evm/lib/EvmChain.ts index 6a10c3c..6d609f8 100644 --- a/packages/chains/evm/lib/EvmChain.ts +++ b/packages/chains/evm/lib/EvmChain.ts @@ -20,7 +20,7 @@ import { } from '@rosen-chains/abstract-chain'; import { EvmRosenExtractor } from '@rosen-bridge/rosen-extractor'; import AbstractEvmNetwork from './network/AbstractEvmNetwork'; -import { EvmConfigs } from './types'; +import { EvmConfigs, TssSignFunction } from './types'; import { Transaction } from 'ethers'; import Serializer from './Serializer'; import * as EvmUtils from './EvmUtils'; @@ -34,7 +34,7 @@ abstract class EvmChain extends AbstractChain { feeRatioDivisor: bigint; supportedTokens: Array; - protected signFunction: (txHash: Uint8Array) => Promise; + protected signFunction: TssSignFunction; constructor( network: AbstractEvmNetwork, @@ -43,7 +43,7 @@ abstract class EvmChain extends AbstractChain { tokens: RosenTokens, nativeToken: string, supportedTokens: Array, - signFunction: (txHash: Uint8Array) => Promise, + signFunction: TssSignFunction, logger?: any ) { super(network, configs, logger); diff --git a/packages/chains/evm/lib/types.ts b/packages/chains/evm/lib/types.ts index a85b2d4..9b405dd 100644 --- a/packages/chains/evm/lib/types.ts +++ b/packages/chains/evm/lib/types.ts @@ -1,6 +1,6 @@ import { ChainConfigs } from '@rosen-chains/abstract-chain'; -interface BlockHeader { +export interface BlockHeader { hash: string; number: number; gasLimit: bigint; @@ -8,11 +8,14 @@ interface BlockHeader { baseFeePerGas: bigint; } -interface EvmConfigs extends ChainConfigs { +export interface EvmConfigs extends ChainConfigs { maxParallelTx: number; gasPriceSlippage: bigint; gasLimitSlippage: bigint; gasLimitMultiplier: bigint; } -export { BlockHeader, EvmConfigs }; +export type TssSignFunction = (txHash: Uint8Array) => Promise<{ + signature: string; + signatureRecovery: string; +}>; diff --git a/packages/chains/evm/tests/EvmChain.spec.ts b/packages/chains/evm/tests/EvmChain.spec.ts index e6e873e..a5a086c 100644 --- a/packages/chains/evm/tests/EvmChain.spec.ts +++ b/packages/chains/evm/tests/EvmChain.spec.ts @@ -16,6 +16,7 @@ import * as testUtils from './TestUtils'; import TestChain from './TestChain'; import Serializer from '../lib/Serializer'; import { Transaction, TransactionLike } from 'ethers'; +import { TssSignFunction } from '../lib'; describe('EvmChain', () => { const network = new TestEvmNetwork(); @@ -23,7 +24,7 @@ describe('EvmChain', () => { const generateChainObject = ( network: TestEvmNetwork, - signFn: (txHash: Uint8Array) => Promise = testUtils.mockedSignFn + signFn: TssSignFunction = testUtils.mockedSignFn ) => { return new TestChain( network, diff --git a/packages/chains/evm/tests/TestChain.ts b/packages/chains/evm/tests/TestChain.ts index c5dc392..ebd161d 100644 --- a/packages/chains/evm/tests/TestChain.ts +++ b/packages/chains/evm/tests/TestChain.ts @@ -1,5 +1,5 @@ import { RosenTokens } from '@rosen-bridge/tokens'; -import { AbstractEvmNetwork, EvmChain } from '../lib'; +import { AbstractEvmNetwork, EvmChain, TssSignFunction } from '../lib'; import { EvmRosenExtractor } from '@rosen-bridge/rosen-extractor'; class TestChain extends EvmChain { @@ -14,7 +14,7 @@ class TestChain extends EvmChain { tokens: RosenTokens, nativeToken: string, supportedTokens: Array, - signFunction: (txHash: Uint8Array) => Promise, + signFunction: TssSignFunction, logger?: any ) { super( diff --git a/packages/chains/evm/tests/TestUtils.ts b/packages/chains/evm/tests/TestUtils.ts index 5fd97c1..b447929 100644 --- a/packages/chains/evm/tests/TestUtils.ts +++ b/packages/chains/evm/tests/TestUtils.ts @@ -32,7 +32,11 @@ export const configs: EvmConfigs = { gasLimitMultiplier: 3n, }; -export const mockedSignFn = () => Promise.resolve(''); +export const mockedSignFn = () => + Promise.resolve({ + signature: '', + signatureRecovery: '', + }); export const mockHasLockAddressEnoughAssets = ( chain: EvmChain,