Skip to content

Commit

Permalink
update tss sign functions
Browse files Browse the repository at this point in the history
  • Loading branch information
RaaCT0R committed Apr 3, 2024
1 parent 57c41dd commit 786c4e7
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 28 deletions.
6 changes: 6 additions & 0 deletions .changeset/tough-masks-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rosen-chains/bitcoin': major
'@rosen-chains/evm': major
---

add signatureRecovery to return data of required signFunction
25 changes: 14 additions & 11 deletions packages/chains/bitcoin/lib/BitcoinChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -33,7 +38,7 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
declare configs: BitcoinConfigs;
CHAIN = BITCOIN_CHAIN;
extractor: BitcoinRosenExtractor;
protected signFunction: (txHash: Uint8Array) => Promise<string>;
protected signFunction: TssSignFunction;
protected lockScript: string;
protected signingScript: Buffer;

Expand All @@ -42,7 +47,7 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
configs: BitcoinConfigs,
feeRatioDivisor: bigint,
tokens: RosenTokens,
signFunction: (txHash: Uint8Array) => Promise<string>,
signFunction: TssSignFunction,
logger?: AbstractLogger
) {
super(network, configs, feeRatioDivisor, logger);
Expand Down Expand Up @@ -398,14 +403,12 @@ class BitcoinChain extends AbstractUtxoChain<BitcoinTx, BitcoinUtxo> {
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);
}

Expand Down
5 changes: 5 additions & 0 deletions packages/chains/bitcoin/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ export interface BitcoinTx {
inputs: BitcoinTxInput[];
outputs: BitcoinTxOutput[];
}

export type TssSignFunction = (txHash: Uint8Array) => Promise<{
signature: string;
signatureRecovery: string;
}>;
28 changes: 21 additions & 7 deletions packages/chains/bitcoin/tests/BitcoinChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
BitcoinTransaction,
BitcoinUtxo,
SEGWIT_INPUT_WEIGHT_UNIT,
TssSignFunction,
} from '../lib';
import TestBitcoinNetwork from './network/TestBitcoinNetwork';
import * as testData from './testData';
Expand Down Expand Up @@ -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<string> = mockedSignFn
signFn: TssSignFunction = mockedSignFn
) => {
return new BitcoinChain(
network,
Expand Down Expand Up @@ -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<string> => {
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}]`
Expand Down Expand Up @@ -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<string> => {
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`);
};

Expand Down
6 changes: 3 additions & 3 deletions packages/chains/evm/lib/EvmChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -34,7 +34,7 @@ abstract class EvmChain extends AbstractChain<Transaction> {

feeRatioDivisor: bigint;
supportedTokens: Array<string>;
protected signFunction: (txHash: Uint8Array) => Promise<string>;
protected signFunction: TssSignFunction;

constructor(
network: AbstractEvmNetwork,
Expand All @@ -43,7 +43,7 @@ abstract class EvmChain extends AbstractChain<Transaction> {
tokens: RosenTokens,
nativeToken: string,
supportedTokens: Array<string>,
signFunction: (txHash: Uint8Array) => Promise<string>,
signFunction: TssSignFunction,
logger?: any
) {
super(network, configs, logger);
Expand Down
9 changes: 6 additions & 3 deletions packages/chains/evm/lib/types.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
import { ChainConfigs } from '@rosen-chains/abstract-chain';

interface BlockHeader {
export interface BlockHeader {
hash: string;
number: number;
gasLimit: bigint;
gasUsed: bigint;
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;
}>;
3 changes: 2 additions & 1 deletion packages/chains/evm/tests/EvmChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@ 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();
const feeRatioDivisor = 1n;

const generateChainObject = (
network: TestEvmNetwork,
signFn: (txHash: Uint8Array) => Promise<string> = testUtils.mockedSignFn
signFn: TssSignFunction = testUtils.mockedSignFn
) => {
return new TestChain(
network,
Expand Down
4 changes: 2 additions & 2 deletions packages/chains/evm/tests/TestChain.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -14,7 +14,7 @@ class TestChain extends EvmChain {
tokens: RosenTokens,
nativeToken: string,
supportedTokens: Array<string>,
signFunction: (txHash: Uint8Array) => Promise<string>,
signFunction: TssSignFunction,
logger?: any
) {
super(
Expand Down
6 changes: 5 additions & 1 deletion packages/chains/evm/tests/TestUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 786c4e7

Please sign in to comment.