1

I have a pallet which ensures a particular custom origin of H160(20 bytes). Actually, this pallet is to ensure that the Origin is that of an ethereum address(20 bytes).

I am therefore writing Integration tests using PolkadotJS, how can i send and sign a transaction by using a 20 bytes address origin?

1
  • When i try Bruno's suggestion, I always get: Error: createType(AccountId):: Invalid AccountId provided, expected 32 bytes, found 20
    – dharjeezy
    2 days ago

1 Answer 1

0

Looks like Ethereum is supported in @polkadot/keyring:

const PairFromSeed = {
  ecdsa: (seed: Uint8Array): Keypair => secp256k1FromSeed(seed),
  ed25519: (seed: Uint8Array): Keypair => ed25519FromSeed(seed),
  ethereum: (seed: Uint8Array): Keypair => secp256k1FromSeed(seed),
  sr25519: (seed: Uint8Array): Keypair => sr25519FromSeed(seed)
};

So you could do something like this:

// Import the keyring as required
import Keyring from '@polkadot/keyring';

// Create a keyring instance
const keyring = new Keyring({ type: 'ethereum' });

Moonbeam has excellent docs on this. As well as an example script:

import { ApiPromise, WsProvider } from '@polkadot/api';
import { Keyring } from '@polkadot/api';

const wsProvider = new WsProvider('WSS-API-ENDPOINT-HERE');
const api = await ApiPromise.create({ provider: wsProvider });

const keyring = new Keyring({ type: 'ethereum' });

const alice = keyring.addFromUri('ALICE-ACCOUNT-PRIVATE-KEY-HERE');
const bob = 'BOB-ACCOUNT-PUBLIC-KEY-HERE';

const tx = await api.tx.balances
  .transfer(bob, 12345n)

const encodedCallData = tx.method.toHex()
console.log(encodedCallData)

const txHash = await tx
  .signAndSend(alice);

console.log(`Submitted with hash ${txHash}`);

// Disconnect the API
api.disconnect();
3
  • Thanks for the response. So, I actually tried as you suggested before posting the question, but I always get this error: Error: createType(AccountId):: Invalid AccountId provided, expected 32 bytes, found 20
    – dharjeezy
    May 24 at 13:07
  • Okay. Can you open a new question with a reproducible example (actual code + repo(s)), so we can replicate the issue and solve it.
    – Bruno
    May 24 at 13:10
  • The repo is actually private, but it is similar to moonbeam's. i will open another question with code examples if that's ok
    – dharjeezy
    May 24 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.