Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

Commit

Permalink
feat: add SignedExtrinsicRune from and fromHex methods (#853)
Browse files Browse the repository at this point in the history
  • Loading branch information
harrysolovay authored Apr 6, 2023
1 parent 66d9f71 commit 56743de
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
43 changes: 43 additions & 0 deletions examples/sign/offline.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* @title Offline Signing
* @stability nearing
* @description Create and sign an extrinsic, then serialize it into a hex for later use.
* Finally, rehydrate the extrinsic and submit it.
*/

import { $, SignedExtrinsicRune } from "capi"
import { signature } from "capi/patterns/signature/polkadot.ts"
import { Balances, chain, createUsers } from "westend_dev/mod.js"

const { alexa, billy } = await createUsers()

// Create and sign the extrinsic. Extract the hex.
const hex = await Balances
.transfer({
value: 12345n,
dest: billy.address,
})
.signed(signature({ sender: alexa }))
.hex()
.run()

// Save `hex` however you'd like (potentially sending to a relayer service,
// writing to disk, etc.).
save(hex)

// Hydrate the signed extrinsic, submit it and await finalization.
const hash = await SignedExtrinsicRune
.fromHex(chain, hex)
.sent()
.dbgStatus("Tx status:")
.finalized()
.run()

// Ensure the extrinsic has been finalized.
$.assert($.str, hash)

// egdoc-ignore-start
// The following noop is solely for explanation. Swap this out with your
// own signed-hex-representation-consuming code.
function save(_hex: string) {}
// egdoc-ignore-end
18 changes: 16 additions & 2 deletions fluent/SignedExtrinsicRune.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { hex } from "../crypto/mod.ts"
import { ValueRune } from "../rune/mod.ts"
import { Chain } from "./ChainRune.ts"
import { Rune, RunicArgs, ValueRune } from "../rune/mod.ts"
import { Chain, ChainRune } from "./ChainRune.ts"
import { ExtrinsicStatusRune } from "./ExtrinsicStatusRune.ts"
import { PatternRune } from "./PatternRune.ts"

export class SignedExtrinsicRune<out C extends Chain, out U> extends PatternRune<Uint8Array, C, U> {
static from<C extends Chain, U, X>(
chain: ChainRune<C, U>,
...[value]: RunicArgs<X, [value: Uint8Array]>
) {
return Rune.resolve(value).into(SignedExtrinsicRune, chain)
}

static fromHex<C extends Chain, U, X>(
chain: ChainRune<C, U>,
...[value]: RunicArgs<X, [value: string]>
) {
return this.from(chain, Rune.resolve(value).map(hex.decode))
}

hex() {
return this.into(ValueRune).map(hex.encode)
}
Expand Down
1 change: 1 addition & 0 deletions words.txt
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ dispatchable
dprint
edgeware
efinity
egdoc
esbenp
esque
extrinsics
Expand Down

0 comments on commit 56743de

Please sign in to comment.