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

feat: add fromHex to MultisigRune #991

Merged
merged 6 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions fluent/CodecRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import * as $ from "../deps/scale.ts"
import { Rune, RunicArgs } from "../rune/mod.ts"

export class CodecRune<in out T, out U> extends Rune<$.Codec<T>, U> {
static from<X, T>(...[codec]: RunicArgs<X, [codec: $.Codec<T>]>) {
return Rune.resolve(codec).into(CodecRune)
}

// TODO: eventually, utilize `V` to toggle runtime validation
encoded<X>(...[value]: RunicArgs<X, [value: T]>) {
return Rune.tuple([this, value]).map(async ([codec, value]) => {
Expand Down
28 changes: 27 additions & 1 deletion patterns/multisig/MultisigRune.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { MultiAddress } from "@capi/polkadot"
import * as bytes from "../../deps/std/bytes.ts"
import { $, Chain, ChainRune, PatternRune, Rune, RunicArgs, ValueRune } from "../../mod.ts"
import {
$,
Chain,
ChainRune,
CodecRune,
hex,
PatternRune,
Rune,
RunicArgs,
ValueRune,
} from "../../mod.ts"
import { multisigAccountId } from "./multisigAccountId.ts"

export interface Multisig {
signatories: Uint8Array[]
threshold?: number
}

export const $multisig: $.Codec<Multisig> = $.object(
$.field("signatories", $.array($.sizedUint8Array(32))),
$.optionalField("threshold", $.u8),
)

// TODO: swap out `Chain` constraints upon subset gen issue resolution... same for other patterns
export class MultisigRune<out C extends Chain, out U> extends PatternRune<Multisig, C, U> {
static from<C extends Chain, U, X>(
Expand All @@ -22,6 +37,8 @@ export class MultisigRune<out C extends Chain, out U> extends PatternRune<Multis
threshold = this.value.map(({ threshold, signatories }) => threshold ?? signatories.length - 1)
accountId = Rune.fn(multisigAccountId).call(this.value.access("signatories"), this.threshold)
address = MultiAddress.Id(this.accountId)
encoded = CodecRune.from($multisig).encoded(this.as(MultisigRune))
hex = this.encoded.map(hex.encode)

otherSignatories<X>(...[sender]: RunicArgs<X, [sender: MultiAddress]>) {
return Rune
Expand Down Expand Up @@ -150,6 +167,15 @@ export class MultisigRune<out C extends Chain, out U> extends PatternRune<Multis
)
.map((entry) => entry !== null)
}

static fromHex<C extends Chain, U, X>(
chain: ChainRune<C, U>,
...[state]: RunicArgs<X, [state: string]>
) {
return CodecRune.from($multisig)
.decoded(Rune.resolve(state).into(ValueRune).map(hex.decode))
.into(MultisigRune, chain)
}
}

export class NoProposalError extends Error {
Expand Down
8 changes: 4 additions & 4 deletions patterns/multisig/VirtualMultisigRune.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
$,
Chain,
ChainRune,
CodecRune,
hex,
MetaRune,
PatternRune,
Expand Down Expand Up @@ -38,7 +39,7 @@ export class VirtualMultisigRune<out C extends Chain, out U>
signatories: this.value.access("members").map((arr) => arr.map((a) => a[1])),
threshold: this.value.access("threshold"),
}).into(MultisigRune, this.chain)
encoded = this.value.map((m) => $virtualMultisig.encode(m))
encoded = CodecRune.from($virtualMultisig).encoded(this.as(VirtualMultisigRune))
hex = this.encoded.map(hex.encode)

senderProxyId<X>(...[senderAccountId]: RunicArgs<X, [senderAccountId: Uint8Array]>) {
Expand Down Expand Up @@ -107,9 +108,8 @@ export class VirtualMultisigRune<out C extends Chain, out U>
chain: ChainRune<C, U>,
...[state]: RunicArgs<X, [state: string]>
) {
return Rune
.resolve(state)
.map((s) => $virtualMultisig.decode(hex.decode(s)))
return CodecRune.from($virtualMultisig)
.decoded(Rune.resolve(state).into(ValueRune).map(hex.decode))
.into(VirtualMultisigRune, chain)
}

Expand Down