Skip to content

Commit

Permalink
Merge branch '112-fix-sign-transaction-in-evm' into 'dev'
Browse files Browse the repository at this point in the history
Resolve "remove '0x' from signing message in EvmChain"

Closes #112

See merge request ergo/rosen-bridge/rosen-chains!132
  • Loading branch information
zargarzadehm committed Aug 12, 2024
2 parents 0fc8876 + 5853f15 commit a870083
Show file tree
Hide file tree
Showing 10 changed files with 65 additions and 35 deletions.
File renamed without changes.
10 changes: 5 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/chains/ethereum/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @rosen-chains/ethereum

## 0.1.2

### Patch Changes

- Updated dependencies
- @rosen-chains/evm@2.0.1

## 0.1.1

### Patch Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/chains/ethereum/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/ethereum",
"version": "0.1.1",
"version": "0.1.2",
"description": "this project contains ethereum chain for Rosen-bridge",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down Expand Up @@ -29,7 +29,7 @@
},
"peerDependencies": {
"@rosen-chains/abstract-chain": "^9.0.0",
"@rosen-chains/evm": "^2.0.0"
"@rosen-chains/evm": "^2.0.1"
},
"dependencies": {
"@rosen-bridge/tokens": "^1.2.1"
Expand Down
6 changes: 6 additions & 0 deletions packages/chains/evm/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @rosen-chains/evm

## 2.0.1

### Patch Changes

- fix signTransaction

## 2.0.0

### Major Changes
Expand Down
48 changes: 25 additions & 23 deletions packages/chains/evm/lib/EvmChain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -510,30 +510,32 @@ abstract class EvmChain extends AbstractChain<Transaction> {
requiredSign: number
): Promise<PaymentTransaction> => {
const tx = Serializer.deserialize(transaction.txBytes);
return this.signFunction(Buffer.from(tx.unsignedHash, 'hex')).then(
(res) => {
const r = '0x' + res.signature.slice(0, 64);
const s = '0x' + res.signature.slice(64, 128);
const yParity = Number(res.signatureRecovery);
if (yParity !== 0 && yParity !== 1)
throw new ImpossibleBehavior(
`non-binary signature recovery: ${res.signatureRecovery}`
);
const signature = Signature.from({
r,
s,
yParity: yParity,
});
tx.signature = signature;
return new PaymentTransaction(
transaction.network,
transaction.txId,
transaction.eventId,
Serializer.signedSerialize(tx),
transaction.txType
const hash =
tx.unsignedHash.slice(0, 2) === '0x'
? tx.unsignedHash.slice(2)
: tx.unsignedHash;
return this.signFunction(Buffer.from(hash, 'hex')).then((res) => {
const r = '0x' + res.signature.slice(0, 64);
const s = '0x' + res.signature.slice(64, 128);
const yParity = Number(res.signatureRecovery);
if (yParity !== 0 && yParity !== 1)
throw new ImpossibleBehavior(
`non-binary signature recovery: ${res.signatureRecovery}`
);
}
);
const signature = Signature.from({
r,
s,
yParity: yParity,
});
tx.signature = signature;
return new PaymentTransaction(
transaction.network,
transaction.txId,
transaction.eventId,
Serializer.signedSerialize(tx),
transaction.txType
);
});
};

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/chains/evm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/evm",
"version": "2.0.0",
"version": "2.0.1",
"description": "this project contains evm chains for Rosen-bridge",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down
12 changes: 10 additions & 2 deletions packages/chains/evm/tests/EvmChain.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1968,19 +1968,22 @@ describe('EvmChain', () => {
* - mock PaymentTransaction of unsigned transaction
* - call the function
* - check returned value
* - check if function got called
* @expected
* - it should return PaymentTransaction of signed transaction (all fields
* are same as input object, except txBytes which is signed transaction)
* - signed tx bytes and hash should be as expected
* - `signFunction` should have been called with unsigned hash without '0x'
*/
it('should return PaymentTransaction of the signed transaction', async () => {
// mock a sign function to return signature
const signFunction = async (txHash: Uint8Array) => {
const signFunction = vi.fn();
signFunction.mockImplementation(async (txHash: Uint8Array) => {
return {
signature: TestData.transaction2Signature,
signatureRecovery: TestData.transaction2SignatureRecovery,
};
};
});
const evmChain = testUtils.generateChainObject(network, signFunction);

// mock PaymentTransaction of unsigned transaction
Expand All @@ -2007,6 +2010,11 @@ describe('EvmChain', () => {
const signedTx = Serializer.deserialize(result.txBytes);
expect(signedTx.serialized).toEqual(TestData.transaction2SignedTx);
expect(signedTx.hash).toEqual(TestData.transaction2TxId);

// `signFunction` should have been called with unsigned hash without '0x'
expect(signFunction).toHaveBeenCalledWith(
Buffer.from(tx.unsignedHash.slice(2), 'hex')
);
});

/**
Expand Down
7 changes: 7 additions & 0 deletions packages/networks/evm-rpc/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# @rosen-chains/evm-rpc

## 2.0.1

### Patch Changes

- Updated dependencies
- @rosen-chains/evm@2.0.1

## 2.0.0

### Major Changes
Expand Down
4 changes: 2 additions & 2 deletions packages/networks/evm-rpc/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rosen-chains/evm-rpc",
"version": "2.0.0",
"version": "2.0.1",
"description": "A package to be used as network api provider for @rosen-chains/evm package",
"repository": "https://github.com/rosen-bridge/rosen-chains",
"license": "GPL-3.0",
Expand Down Expand Up @@ -39,6 +39,6 @@
},
"peerDependencies": {
"@rosen-chains/abstract-chain": "^9.0.0",
"@rosen-chains/evm": "^2.0.0"
"@rosen-chains/evm": "^2.0.1"
}
}

0 comments on commit a870083

Please sign in to comment.