Skip to content

Commit

Permalink
Feat/call back for eth wallet signing (#468)
Browse files Browse the repository at this point in the history
* support callback when signing via ethereum wallet
  • Loading branch information
KarishmaBothara authored May 4, 2022
1 parent 1bf31a2 commit c0f754f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
14 changes: 7 additions & 7 deletions packages/api/test/e2e/tx.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,14 +139,14 @@ describe('e2e transactions', () => {
.signViaEthWallet(
ethAddress,
api,
(global as any).ethereum
(global as any).ethereum, async ({ events, status }: SubmittableResult) => {
if (status.isInBlock) {
expect(events[0].event.method).toEqual('Transferred');
expect(events[0].event.section).toEqual('genericAsset');
done();
}
}
);

await api.query.genericAsset.freeBalance(stakingAssetId, cennznetAddress, (cennzBal) => {
console.log('cennzBal::', cennzBal.toString());
console.log('cennzBal.toNumber() == transferAmt:', cennzBal.toString() === (amount - transferAmt).toString());
cennzBal.toString() === (amount - transferAmt).toString() ? done() : null
});
});
});
});
Expand Down
18 changes: 12 additions & 6 deletions packages/types/src/interfaces/extrinsic/v1/Extrinsic.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import {cvmToAddress} from "@cennznet/types/utils";
import { GenericExtrinsic, UInt } from '@polkadot/types';
import { SignatureOptions } from '@cennznet/types/interfaces/extrinsic/types';
import { AnyU8a, ExtrinsicPayloadValue, IKeyringPair, Registry } from '@polkadot/types/types';
import {
AnyU8a,
Callback,
ExtrinsicPayloadValue,
IKeyringPair,
ISubmittableResult,
Registry
} from '@polkadot/types/types';
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 {Constructor, HexString} from '@polkadot/util/types';
import { ApiTypes, SubmittableResultResult } from "@polkadot/api/types";

export default class CENNZnetExtrinsic extends GenericExtrinsic{
private signaturePayloadOptions: SignatureOptions | ExtrinsicPayloadValue;
Expand Down Expand Up @@ -83,16 +89,16 @@ export default class CENNZnetExtrinsic extends GenericExtrinsic{
}

/**
* @description sign the extrinsic via metamask
* @description sign the extrinsic via any ethereum wallet
*/
async signViaEthWallet(ethAddress: string, api: Api, ethereum: any): Promise<SubmittableResultResult<ApiTypes>> {
async signViaEthWallet(ethAddress: string, api: Api, ethereum: any, optionalStatusCb?: Callback<ISubmittableResult>): Promise<this> {
const cennznetAddress = cvmToAddress(ethAddress);
const nonce = await api.rpc.system.accountNextIndex(cennznetAddress);
const payload = this.registry.createType('EthWalletCall', { call: this, nonce }).toHex();
// Request signature from ethereum wallet
const signature = await ethereum.request({ method: 'personal_sign', params: [payload, ethAddress] });
// Broadcast the tx to CENNZnet
const txHash = await api.tx.ethWallet.call(this, ethAddress, signature).send();
return txHash;
await api.tx.ethWallet.call(this, ethAddress, signature).send(optionalStatusCb);
return this;
}
}

0 comments on commit c0f754f

Please sign in to comment.