Statement SDK
The Statement Store primitive is an off-chain data store for signed messages (known as statements) accessible via RPC.
Getting Started
Install the Statement SDK using your package manager:
pnpm i @polkadot-api/sdk-statementThe function createStatementSdk takes a simple request-response function, which is expected to implement the Statement JSON-RPC API.
The actual interface of the function is
type RequestFn = (method: string, params: Array<any>) => Promise<any>The property _request of a client can be used for this matter.
import { createClient } from "polkadot-api"
import { getWsProvider } from "polkadot-api/ws-provider"
const client = createClient(
getWsProvider("wss://paseo-people-next-rpc.polkadot.io"),
)
import { createStatementSdk } from "@polkadot-api/sdk-statement"
const statementSdk = createStatementSdk(client._request) Get Statements
Get Complete Store (dump)
The function dump will get all statements from the provider's store. Note that this endpoint might be rate-limited in some cases.
// all currently available statements of that node.
const statements = await statementSdk.dump()Get Filtered Statements (getStatements)
This function will query for filtered statements by topic and/or dest key (find meaning in Statement Store docs.
Parameters
dest:Binaryfor specific dest key.nullfor no dest key.undefinedto disable filtering bydesttopics: Array of up to 4 topics to filter by.
import { stringToTopic } from "@polkadot-api/sdk-statement"
// statements with specific topics and specific decryptionkey.
const statements = await statementSdk.getStatements({
topics: [stringToTopic("pop"), stringToTopic("chat"), stringToTopic("v1")],
dest: Binary.fromHex(
"0xf0673d30606ee26672707e4fd2bc8b58d3becb7aba2d5f60add64abb5fea4710",
),
})Submit Statements
Create Statement Signer
In order to sign a Statement, we need to create a signer. This can be done with the helper getStatementSigner, taking:
publicKey: Pubkey of the signertype:ed25519,sr25519,ecdsasignature type.signFn:(payload: Uint8Array) => Uint8Array | Promise<Uint8Array>Cryptographic signing function.
As one can notice, this is very similar to the PolkadotSigner interface. Check those docs to learn more!
import { getStatementSigner } from "@polkadot-api/sdk-statement"
import { ed25519 } from "@noble/curves/ed25519.js"
const SECRET_KEY = new Uint8Array() // get your key
const signer = getStatementSigner(
ed25519.getPublicKey(SECRET_KEY),
"ed25519",
(i) => ed25519.sign(i, SECRET_KEY),
)Create Statement
In order to create a statement, it is as simple as creating an UnsignedStatement object.
import { Binary } from "polkadot-api"
import type { UnsignedStatement } from "@polkadot-api/sdk-statement"
const statement: UnsignedStatement = {
data: Binary.fromHex("0xDEADBEEF"),
priority: 1,
}Submit Statement
Once we have the signer and the statement, we can go to sign and submit it to the store!
const signer = getStatementSigner(
ed25519.getPublicKey(SECRET_KEY),
"ed25519",
(i) => ed25519.sign(i, SECRET_KEY),
)
const statement: UnsignedStatement = {
data: Binary.fromHex("0xDEADBEEF"),
priority: 1,
}
const signed = await signer.sign(statement)
await statementSdk.submit(signed)