Skip to content

Commit

Permalink
Feat/encode eth signature (#471)
Browse files Browse the repository at this point in the history
* Update eth wallet sign to use encoded payload
  • Loading branch information
KarishmaBothara authored May 15, 2022
1 parent fbf61c4 commit a16e493
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ jobs:
- name: start latest test node and list all docker container
run: |
docker network create my-bridge-net
docker run --pull=always --network=my-bridge-net --name testnet_node_alice -p 9944:9944 -d cennznet/cennznet:2.1.0 --dev --tmp --unsafe-ws-external --unsafe-rpc-external --eth-http=http://ethNode:8545 --no-mdns
docker run --pull=always --network=my-bridge-net --name testnet_node_alice -p 9944:9944 -d cennznet/cennznet:latest --dev --tmp --unsafe-ws-external --unsafe-rpc-external --eth-http=http://ethNode:8545 --no-mdns
docker network connect my-bridge-net ethNode
docker exec testnet_node_alice curl ethNode:8545
- name: test
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
# dev node with runtime v40 and client 1.4.0
testnet_node_alice:
container_name: testnet_node_alice
image: cennznet/cennznet:2.1.0
image: cennznet/cennznet:latest
command:
- --dev
- --tmp
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/staticMetadata.ts

Large diffs are not rendered by default.

17 changes: 14 additions & 3 deletions packages/api/src/util/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { KeyringPair } from '@polkadot/keyring/types';
import { EthereumSignature } from '@polkadot/types/interfaces';
import { H256 } from '@polkadot/types/interfaces/runtime';
import { Api } from '@cennznet/api';
import { encodeAddress } from '@polkadot/util-crypto';

// Splits the given Ethereum signatures into r,s,v format
export function extractEthereumSignature(signatures: EthereumSignature[]): { r: string[]; s: string[]; v: number[] } {
Expand Down Expand Up @@ -39,6 +40,7 @@ export async function awaitDepositClaim(
claim: { tokenAddress: string; amount: string; beneficiary: string },
signer: KeyringPair
): Promise<ClaimDeposited> {
const beneficiaryAddress = encodeAddress(claim.beneficiary, 42); // convert public key to address
return new Promise((resolve, reject) => {
api.tx.erc20Peg
.depositClaim(depositTxHash, claim)
Expand All @@ -48,11 +50,20 @@ export async function awaitDepositClaim(
for (const {
event: { method, section, data },
} of events) {
const [, claimer] = data;
if (section === 'erc20Peg' && method === 'Erc20Claim' && claimer && claimer.toString() === signer.address) {
const [, beneficiary] = data;
if (
section === 'erc20Peg' &&
method === 'Erc20Claim' &&
beneficiary &&
beneficiary.toString() === beneficiaryAddress
) {
eventClaimId = data[0];
break;
} else if (section === 'system' && method === 'ExtrinsicFailed') {
} else if (
(section === 'system' && method === 'ExtrinsicFailed') ||
section === 'erc20Peg' ||
method === 'DelayedErc20DepositFailed'
) {
reject('Claim already notarized');
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/api/test/e2e/ethBridge.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe('Eth bridge test', () => {
if (section === 'erc20Peg' && method == 'Erc20Claim') {
const [claimId, claimer] = data;
expect((claimId as EventClaimId).toNumber()).toBeGreaterThanOrEqual(0);
expect(claimer.toString()).toEqual(alice.address);
expect(claimer.toString()).toEqual(bob.address);
done();
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/api/test/e2e/getMetadata.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ describe('getMetadata()', () => {
const slimMetadata = await getMetadata('ws://localhost:9944');
const metadataKey = Object.entries(slimMetadata)[0][0].split('-');
const metadataValue = Object.entries(slimMetadata)[0][1];
expect(metadataKey[1]).toEqual("53");
expect(metadataKey[1]).toEqual("58");
const registry = new TypeRegistry();
const mVersionedSlim = new MetadataVersioned(registry, metadataValue);
const modules = mVersionedSlim.asLatest.pallets.toArray();
Expand All @@ -37,7 +37,7 @@ describe('getMetadata()', () => {
const slimMetadata = await getMetadata('ws://localhost:9944', keepMetaFor);
const metadataKey = Object.entries(slimMetadata)[0][0].split('-');
const metadataValue = Object.entries(slimMetadata)[0][1];
expect(metadataKey[1]).toEqual("53");
expect(metadataKey[1]).toEqual("58");
const registry = new TypeRegistry();
const mVersionedSlim = new MetadataVersioned(registry, metadataValue);
const modules = mVersionedSlim.asLatest.pallets.toArray();
Expand Down
4 changes: 2 additions & 2 deletions packages/api/test/e2e/tx.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('e2e transactions', () => {
});

it('Uses eth wallet to sign', async done => {
const ethAddress = '0x806B4697a5FCEBb66c16A613FB71955358c99A7C';
const ethAddress = '0x5d5586341ca72146791c33c26c0c10ed971c9b53';
// Find the equivalent CENNZnet address for Ethereum address and send some CPAY to spend on txs
const cennznetAddress = cvmToAddress(ethAddress);
const amount = 100000;
Expand All @@ -128,7 +128,7 @@ describe('e2e transactions', () => {
accounts: {return: accounts},
signature: {
params: [accounts[0], 'sign'],
return: "0x474613e40d21220a531d0abd0e56e8a01d60f6db019b859116726c5b6a5a1bbd070c9cac70fca3a43caeb4cb9ee036ce72fe2cf9b40fb1dbc1374e1a85dcfe281c"
return: "0xc8ee1390bc05479bb4e13eb36b46714af19821eb590142e3f8fb7d972f6f31fb070717be960e41e4ec33baf85776c43e5be588916e7c41ee20ad99c6695fa7fa1b"
}
});

Expand Down
4 changes: 3 additions & 1 deletion packages/types/src/interfaces/extrinsic/v1/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { Address, Call } from '@polkadot/types/interfaces/runtime';
import { ExtrinsicValueV4 } from '@polkadot/types/extrinsic/v4/Extrinsic';
import { Api } from '@cennznet/api';
import { EstimateFeeParams, PaymentOptions } from '@cennznet/api/derives/types';
import {base64Encode} from "@polkadot/util-crypto";
import {Constructor, HexString} from '@polkadot/util/types';

export default class CENNZnetExtrinsic extends GenericExtrinsic{
Expand Down Expand Up @@ -95,8 +96,9 @@ export default class CENNZnetExtrinsic extends GenericExtrinsic{
const cennznetAddress = cvmToAddress(ethAddress);
const nonce = await api.rpc.system.accountNextIndex(cennznetAddress);
const payload = this.registry.createType('EthWalletCall', { call: this, nonce }).toHex();
const encodedPayload = `data:application/octet-stream;base64,${base64Encode(payload)}`;
// Request signature from ethereum wallet
const signature = await ethereum.request({ method: 'personal_sign', params: [payload, ethAddress] });
const signature = await ethereum.request({ method: 'personal_sign', params: [encodedPayload, ethAddress] });
// Broadcast the tx to CENNZnet
await api.tx.ethWallet.call(this, ethAddress, signature).send(optionalStatusCb);
return this;
Expand Down

0 comments on commit a16e493

Please sign in to comment.