Raw signer
PAPI provides a helper to build a PolkadotSigner instance directly from a raw signing function, provided by a cryptographic library, a hardware wallet, etc. It just deals with the chain logic side and leaves to the consumer the cryptographic signature part.
getPolkadotSigner
It is a function to get a signer.
Returns
Parameters
publicKey
Type: Uint8Array
publicKey used to identify the signer.
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.0.1@noble/hashes2.0.1@polkadot-labs/hdkd-helpers0.0.26@polkadot-labs/hdkd0.0.25@scure/sr255190.3.0.polkadot-api1.20.0
Some snippets might work in other versions.
Ed25519
import { } from "polkadot-api/signer"
import { } from "@noble/curves/ed25519.js"
const = new () // get your key
const = (
.(),
"Ed25519",
() => .(, ),
)Sr25519
From private key
import { } from "polkadot-api/signer"
import * as from "@scure/sr25519"
const = new () // get your key
const = (
.(),
"Sr25519",
() => .(, ),
)From mnemonic phrase
This snippet helps to create a signer 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/signer"
const = (())
const = ()
const = ("//Alice") // or `//Bob`, `//Charlie`, etc
const = (
.,
"Sr25519",
.,
)Ecdsa
Ecdsa signer is an umbrella term for two types of signers 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 PolkadotSigners for these different chain types:
Polkadot-like chains
import { } from "@noble/curves/secp256k1.js"
import { } from "@noble/hashes/blake2.js"
import { } from "polkadot-api/signer"
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/signer"
const = new () // get your key
const = (
(.(, false).(1)).(-20),
"Ecdsa",
() => {
const = .((), , {
: false,
: "recovered",
})
return .([....(1), [0]])
},
)