Raw TxCreator
PAPI provides a helper to build a TxCreator directly from a raw signing function, provided by a cryptographic library, a hardware wallet, etc. It deals with the chain transaction side and leaves the cryptographic signature part to the consumer.
getTxCreator
It is a function to get a TxCreator.
Returns
Parameters
publicKey
Type: Uint8Array
Public key or account identifier used to identify the account.
signingType
Type: "Ecdsa" | "Ed25519" | "Sr25519"
Signature schema used to sign.
sign
Type: (input: Uint8Array) => Promise<Uint8Array> | Uint8Array
Cryptographic signature function, signing with the signingType previously declared.
Examples
In these examples we use several libraries:
@noble/curves2.2.0@noble/hashes2.2.0@polkadot-labs/hdkd-helpers0.0.31@polkadot-labs/hdkd0.0.29@scure/sr255192.2.0polkadot-api3.x
Some snippets might work in other versions.
Ed25519
import { } from "polkadot-api/tx-creator"
import { } from "@noble/curves/ed25519.js"
const = new () // get your key
const = (
.(),
"Ed25519",
() => .(, ),
)Sr25519
From private key
import { } from "polkadot-api/tx-creator"
import * as from "@scure/sr25519"
const = new () // get your key
const = (
.(),
"Sr25519",
() => .(, ),
)From mnemonic phrase
This snippet helps to create a TxCreator for Alice, Bob, Charlie, etc.
You can replace by your own mnemonic phrase.
import { } from "@polkadot-labs/hdkd"
import {
,
,
,
} from "@polkadot-labs/hdkd-helpers"
import { } from "polkadot-api/tx-creator"
const = (())
const = ()
const = ("//Alice") // or `//Bob`, `//Charlie`, etc
const = (
.,
"Sr25519",
.,
)Ecdsa
Ecdsa is an umbrella term for two types of TxCreators using ECDSA signatures over Secpk1. In a glance, these are the two types:
- EVM-like chains (Moonbeam, Mythos, Darwinia, Crab, etc.) expect the signer to sign payloads using a Keccak256 hash and use AccountId20 addresses (Ethereum-like addresses).
- Polkadot-like chains (e.g., Polkadot, Kusama) expect the signer to sign payloads using Blake2_256 and use AccountId32 addresses (Polkadot-like addresses).
With that distinction in mind, here's how you can create Ecdsa TxCreators for these different chain types:
Polkadot-like chains
import { } from "@noble/curves/secp256k1.js"
import { } from "@noble/hashes/blake2.js"
import { } from "polkadot-api/tx-creator"
const = new () // get your key
const = (
(.(), { : 32 }),
"Ecdsa",
() => {
const = .((, { : 32 }), , {
: false,
: "recovered",
})
return .([....(1), [0]])
},
)Ethereum-like chains
import { } from "@noble/curves/secp256k1.js"
import { } from "@noble/hashes/sha3.js"
import { } from "polkadot-api/tx-creator"
const = new () // get your key
const = (
(.(, false).(1)).(-20),
"Ecdsa",
() => {
const = .((), , {
: false,
: "recovered",
})
return .([....(1), [0]])
},
)