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

Commit

Permalink
fix extrinsic validator
Browse files Browse the repository at this point in the history
  • Loading branch information
tjjfvi committed Nov 12, 2022
1 parent bc802fc commit 5c8384a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 61 deletions.
15 changes: 4 additions & 11 deletions effects/extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,13 @@ import * as scale from "./scale.ts"

const k0_ = Symbol()

export interface CallData {
palletName: string
methodName: string
args: Record<string, unknown>
}

export interface ExtrinsicProps extends CallData {
export interface ExtrinsicProps {
sender: M.MultiAddress
checkpoint?: U.HexHash
mortality?: [period: bigint, phase: bigint]
nonce?: string
tip?: bigint
call: unknown
}

export function extrinsic<Client extends Z.$<rpc.Client>>(client: Client) {
Expand Down Expand Up @@ -101,14 +96,12 @@ export class SignedExtrinsic<
? M.era.mortal(mortality[0], mortality[1])
: M.era.immortal
})
const extra = Z.ls(mortality, nonce, this.props.tip || 0)
const extra = Z.ls(mortality, nonce, this.props.tip || 0n)
const additional = Z.ls(specVersion, transactionVersion, checkpointHash, genesisHash)
const signature = Z.rec({ address: this.props.sender, extra, additional })
const $extrinsicProps = Z.rec({
protocolVersion: 4,
palletName: this.props.palletName,
methodName: this.props.methodName,
args: this.props.args,
call: this.props.call,
signature,
})
this.extrinsicBytes = scale.scaleEncoded($extrinsic_, $extrinsicProps, true)
Expand Down
52 changes: 23 additions & 29 deletions examples/multisig_transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import * as C from "../mod.ts"
import * as T from "../test_util/mod.ts"
import * as U from "../util/mod.ts"

import { Balances, Multisig } from "../codegen/_output/polkadot/pallets/mod.ts"

// FIXME: remove this check once the Zones .bind(env) fix is merged
const hostname = Deno.env.get("TEST_CTX_HOSTNAME")
const portRaw = Deno.env.get("TEST_CTX_PORT")
Expand All @@ -24,14 +26,10 @@ const multisigPublicKey = createKeyMulti(signatories, THRESHOLD)
// Transfer initial balance (existential deposit) to multisig address
const existentialDeposit = extrinsic({
sender: C.compat.multiAddressFromKeypair(T.alice),
call: {
type: "Balances",
value: {
type: "transfer",
value: 2_000_000_000_000n,
dest: C.MultiAddress.Id(multisigPublicKey),
},
},
call: Balances.transfer({
value: 2_000_000_000_000n,
dest: C.MultiAddress.Id(multisigPublicKey),
}),
})
.signed(C.compat.signerFromKeypair(T.alice))
.watch(function(status) {
Expand Down Expand Up @@ -80,28 +78,24 @@ function createOrApproveMultisigProposal<
) {
return extrinsic({
sender: C.compat.multiAddressFromKeypair(pair),
call: {
type: "Multisig",
value: C.Z.rec({
type: "as_multi",
threshold: THRESHOLD,
call: {
type: "Balances",
value: {
type: "transfer_keep_alive",
dest: C.compat.multiAddressFromKeypair(T.dave),
value: 1_230_000_000_000n,
},
},
other_signatories: signatories.filter((value) => value !== pair.publicKey),
store_call: false,
max_weight: {
ref_time: 500_000_000n,
proof_size: 0n,
call: C.Z.call.fac(Multisig.as_multi, null!)(C.Z.rec({
threshold: THRESHOLD,
call: {
type: "Balances",
value: {
type: "transfer_keep_alive",
dest: C.compat.multiAddressFromKeypair(T.dave),
value: 1_230_000_000_000n,
},
maybe_timepoint: maybeTimepoint as Rest[0],
}),
},
},
other_signatories: signatories.filter((value) => value !== pair.publicKey),
store_call: false,
max_weight: {
ref_time: 500_000_000n,
proof_size: 0n,
},
maybe_timepoint: maybeTimepoint as Rest[0],
})),
})
.signed(C.compat.signerFromKeypair(pair))
.watch(function(status) {
Expand Down
29 changes: 8 additions & 21 deletions frame_metadata/Extrinsic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec<Extrinsic> {
$multiAddress._encode(buffer, signature.address)
if ("additional" in signature) {
const toSignBuffer = new $.EncodeBuffer(buffer.stealAlloc(toSignSize))
console.log($call._metadata[0]!.args![1][14][1][1], call)
$call._encode(toSignBuffer, call)
const callEnd = toSignBuffer.finishedSize + toSignBuffer.index
if ("signPayload" in props.sign) {
Expand Down Expand Up @@ -150,29 +149,17 @@ export function $extrinsic(props: ExtrinsicCodecProps): $.Codec<Extrinsic> {
},
_assert(assert) {
assert.typeof(this, "object")
assert
.key(this, "protocolVersion")
.equals($.u8, 4)
assert.key(this, "protocolVersion").equals($.u8, 4)
const value_ = assert.value as any
// TODO: use `assert.key(this, "call")` upon merging https://github.com/paritytech/capi/pull/368
$call._assert(
new $.AssertState({
type: value_.palletName,
value: {
type: value_.methodName,
...value_.args,
},
}),
)
$call._assert(assert.key(this, "call"))
if (value_.signature) {
const signatureAssertState = assert.key(this, "signature")
signatureAssertState.key($multiAddress, "address")
signatureAssertState.key($extra, "extra")
if ("additional" in signatureAssertState) {
signatureAssertState.key($additional, "additional")
}
if ("sig" in signatureAssertState) {
signatureAssertState.key($multiSignature, "sig")
$multiAddress._assert(signatureAssertState.key(this, "address"))
$extra._assert(signatureAssertState.key(this, "extra"))
if ("additional" in value_.signature) {
$additional._assert(signatureAssertState.key(this, "additional"))
} else {
$multiSignature._assert(signatureAssertState.key(this, "sig"))
}
}
},
Expand Down

0 comments on commit 5c8384a

Please sign in to comment.