Skip to content

Commit

Permalink
feat: papi evm
Browse files Browse the repository at this point in the history
  • Loading branch information
tuul-wq committed Oct 28, 2024
1 parent d75b96a commit dff88b2
Show file tree
Hide file tree
Showing 7 changed files with 319 additions and 223 deletions.
2 changes: 1 addition & 1 deletion .papi/descriptors/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "0.1.0-autogenerated.10903113426215382329",
"version": "0.1.0-autogenerated.2303156892404777937",
"name": "@polkadot-api/descriptors",
"files": [
"dist"
Expand Down
Binary file modified .papi/metadata/glmr.scale
Binary file not shown.
4 changes: 2 additions & 2 deletions app/models/balances/balances-model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ describe('models/balances/balances-model', () => {
});

test('should unsub all $subscriptions for chainId if assetId is absent', async () => {
const mockedSubscriptions = { '0x001': { 0: Promise.resolve(noop), 1: Promise.resolve(noop) } };
const mockedSubscriptions = { '0x001': { 0: noop, 1: noop } };
const fakeUnsubscribeFx = vi.fn().mockReturnValue({ '0x001': undefined });

const scope = fork({
Expand All @@ -94,7 +94,7 @@ describe('models/balances/balances-model', () => {

await allSettled(balancesModel._internal.subscribeChainsAssetsFx, {
scope,
params: { apis: {}, chains: [], assets: [], wallet: new Wallet('123') },
params: { clients: {}, chains: [], assets: [], wallet: new Wallet('123') },
});

expect(scope.getState(balancesModel._internal.$subscriptions)).toEqual({ '0x001': { 1: Promise.resolve(noop) } });
Expand Down
25 changes: 14 additions & 11 deletions app/shared/api/blockchain/transfer/native-transfer.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Enum, type HexString, type PolkadotClient } from 'polkadot-api';
import { type HexString, type PolkadotClient } from 'polkadot-api';

import { BN, BN_ZERO, stringToU8a } from '@polkadot/util';
import { BN, BN_ZERO } from '@polkadot/util';

import { type GenericApi } from '../types';

import { FAKE_ACCOUNT_ID } from '@/shared/helpers';
// import { FAKE_ACCOUNT_ID } from '@/shared/helpers';

import { type FeeParams, type ITransfer, type SendTransferParams } from './types';

import { dot } from '@polkadot-api/descriptors';
import { glmr } from '@polkadot-api/descriptors';

type ClientApi = GenericApi;
type ClientApi = GenericApi<typeof glmr>;

export class NativeTransferService implements ITransfer {
readonly #client: ClientApi;
Expand All @@ -20,7 +20,7 @@ export class NativeTransferService implements ITransfer {
}

#getTypedClientApi(client: PolkadotClient): ClientApi {
return { type: 'generic', api: client.getTypedApi(dot) };
return { type: 'generic', api: client.getTypedApi(glmr) };
}

sendTransfer({ amount, destination, signer, transferAll }: SendTransferParams): Promise<HexString> {
Expand All @@ -32,23 +32,26 @@ export class NativeTransferService implements ITransfer {
#getTransferKeepAliveTx(destination: Address, amount: BN) {
return this.#client.api.tx.Balances.transfer_keep_alive({
value: BigInt(amount.toString()),
dest: Enum('Id', destination),
dest: destination, // EVM only
// dest: Enum('Id', destination),
});
}

#getTransferAllTx(destination: Address) {
return this.#client.api.tx.Balances.transfer_all({
keep_alive: false,
dest: Enum('Id', destination),
dest: destination, // EVM only
// dest: Enum('Id', destination),
});
}

getTransferFee({ amount = BN_ZERO, transferAll }: FeeParams): Promise<BN> {
const tx = transferAll
? this.#getTransferAllTx(FAKE_ACCOUNT_ID)
: this.#getTransferKeepAliveTx(FAKE_ACCOUNT_ID, amount);
? this.#getTransferAllTx('0x431621580885a1d9cf257Aaf0628D26Df3e9c591')
: this.#getTransferKeepAliveTx('0x431621580885a1d9cf257Aaf0628D26Df3e9c591', amount);

return tx.getEstimatedFees(stringToU8a(FAKE_ACCOUNT_ID)).then(fee => new BN(fee.toString()));
return tx.getEstimatedFees('0x431621580885a1d9cf257Aaf0628D26Df3e9c591').then(fee => new BN(fee.toString()));
// return tx.getEstimatedFees(stringToU8a(FAKE_ACCOUNT_ID)).then(fee => new BN(fee.toString()));
}

async getGiftTransferFee({ amount = BN_ZERO, transferAll }: FeeParams): Promise<BN> {
Expand Down
19 changes: 15 additions & 4 deletions app/shared/api/crypto/keyring-api.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { secp256k1 } from '@noble/curves/secp256k1';
import { keccak_256 } from '@noble/hashes/sha3';
import { getPolkadotSigner } from '@polkadot-api/signer';
import { sr25519CreateDerive } from '@polkadot-labs/hdkd';
import { type Hex, type KeyPair, mnemonicToEntropy, mnemonicToMiniSecret } from '@polkadot-labs/hdkd-helpers';
import { HDKey } from '@scure/bip32';
import { mnemonicToSeedSync } from '@scure/bip39';
import { type PolkadotSigner } from 'polkadot-api';

import { stringToU8a, u8aToHex } from '@polkadot/util';
import { u8aToHex } from '@polkadot/util';

import { isEvmChain } from '@/shared/helpers';
import { type Chain } from '@/types/substrate';
Expand Down Expand Up @@ -50,11 +52,20 @@ function getSubstrateKeyPair(mnemonic: Mnemonic): KeyPair {
function getEvmKeyPair(mnemonic: Mnemonic): KeyPair {
const seed = mnemonicToSeedSync(mnemonic);
const keyPair = HDKey.fromMasterSeed(seed).derive("m/44'/60'/0'/0/0");
const publicKey = keccak_256(secp256k1.getPublicKey(keyPair.privateKey!, false).slice(1)).slice(-20);

return {
publicKey: keyPair.publicKey!,
sign: (message: Hex) => (typeof message === 'string' ? keyPair.sign(stringToU8a(message)) : keyPair.sign(message)),
const sign = (data: Hex): Uint8Array => {
const signature = secp256k1.sign(keccak_256(data), keyPair.privateKey!);
const signedBytes = signature.toCompactRawBytes();
const len = signedBytes.length;
const result = new Uint8Array(len + 1);
result.set(signedBytes);
result[len] = signature.recovery;

return result;
};

return { publicKey, sign };
}

// Signer
Expand Down
Loading

0 comments on commit dff88b2

Please sign in to comment.