Repository navigation
Expand file tree
/
Copy pathregistry.nim
More file actions
174 lines (169 loc) · 9.66 KB
/
Copy pathregistry.nim
File metadata and controls
174 lines (169 loc) · 9.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
## Driver registry — select a driver by kind + config, so the host is not
## hardcoded to one driver. Adding a driver is: implement the contract (a new
## module), pass conformance, and add one `case` arm here. Nothing else in the
## module changes (invariant 6's promise, made turnkey).
##
## Config is plain JSON so a host can carry it in persistence/metadata rather than
## in code — `newDriver("safe", %*{"chainId": 31337, "safe": "0x…", "owners":
## ["0x…"], "threshold": 2})`.
import std/[json, strutils]
import ./driver
import ./safe
import ./threshold
import ./xmr_msig
import ./frost
import ./invoke
import ./eip191
import ./btc_multisig
import ./lez_multisig # the LEZ multisig program (exo-6cbe)
import ./lez_msig # lez-msig: approvals verified in-program (exo-0d3)
import ../lez/msig as msigbytes
import ./btc_frost # FROST: the aggregate locus (Phase D)
import ./lez_frost # a FROST group acting on LEZ (exo-55e)
import ./split # a split: each pays their own share (exo-a90.3)
import ./charter # a room's charter: its electorate named per proposal (exo-267.1)
import ../bitcoin/tx # hexToBytes
import ../crypto/secp256k1 # Address
import ../crypto/curve25519 # Ed25519Pub (the roster)
type RegistryError* = object of CatchableError
proc hexToAddr(s: string): Address =
var h = s
if h.len >= 2 and h[0] == '0' and (h[1] == 'x' or h[1] == 'X'): h = h[2 .. ^1]
for i in 0 ..< min(20, h.len div 2):
try: result[i] = byte(parseHexInt(h[2*i .. 2*i+1]))
except CatchableError: discard
proc parseRoster(config: JsonNode): seq[Ed25519Pub] =
## The Ed25519 roster the threshold / frost / invoke drivers share — each entry a
## 32-byte hex key. Malformed entries are skipped rather than raising, so a bad key
## drops out of the roster instead of breaking construction.
if config.hasKey("roster") and config["roster"].kind == JArray:
for pkHex in config["roster"]:
var h = pkHex.getStr()
if h.len >= 2 and h[0] == '0' and (h[1] == 'x' or h[1] == 'X'): h = h[2 .. ^1]
var pk: Ed25519Pub
for i in 0 ..< min(32, h.len div 2):
try: pk[i] = byte(parseHexInt(h[2*i .. 2*i+1]))
except CatchableError: discard
result.add pk
proc newDriver*(kind: string, config: JsonNode): Driver =
## The one place driver selection lives. Returns the generic `Driver` — the
## coordination fold and the core take it as-is; a driver-specific host path can
## downcast to the concrete type it asked for.
case kind
of "safe":
var owners: seq[Address]
if config.hasKey("owners") and config["owners"].kind == JArray:
for o in config["owners"]: owners.add hexToAddr(o.getStr())
newSafeDriver(chainId = uint64(config{"chainId"}.getInt(31337)),
safe = hexToAddr(config{"safe"}.getStr()),
owners = owners,
threshold = config{"threshold"}.getInt(2))
of "btc-p2wsh", "btc-tapscript":
# A Bitcoin multisig account (exo-a50.2.3): {network, k, keys: [33-byte hex]}.
var keys: seq[seq[byte]]
if config.hasKey("keys") and config["keys"].kind == JArray:
for k in config["keys"]: keys.add hexToBytes(k.getStr())
let family = (if kind == "btc-p2wsh": P2wshFamily else: TapscriptFamily)
newBtcMultisigDriver(btcAccount(family, config{"network"}.getStr("regtest"), config{"k"}.getInt(2), keys))
of "lez-multisig":
# A LEZ multisig account (exo-6cbe): {chain, pda, program, createKey, members, threshold}.
let cfg = $(%*{"program": config{"program"}.getStr(), "createKey": config{"createKey"}.getStr(),
"pda": config{"pda"}.getStr("lee-v0.2")})
var members: seq[string]
for m in config{"members"}.getElems(): members.add m.getStr()
let (ok, acct, detail) = lezMultisigAccountFromParts(config{"chain"}.getStr("lez:testnet"), "", cfg,
members, config{"threshold"}.getInt(1))
# the registry builds from the config alone: the address is the one it derives
if not ok and not detail.startsWith("the address"): raise newException(RegistryError, detail)
newLezMultisigDriver(acct)
of "lez-msig":
# A lez-msig account (exo-0d3): {chain, config: its disclosure config (configJsonOf)}.
# The registry builds a public one from the config alone: the address is the PDA it
# derives. A private one's address takes the group's keys, so it comes as given
# ({address}, exo-efa).
let cfgJson = config{"config"}.getStr()
var address = config{"address"}.getStr()
try:
let c = parseJson(cfgJson)
if c{"placement"}.getStr("public") != "private":
address = msigbytes.hexOf(msigbytes.multisigPublic(hexToBytes(c{"program"}.getStr()), hexToBytes(c{"createKey"}.getStr())))
except CatchableError as e: raise newException(RegistryError, "not a lez-msig config: " & e.msg)
let (ok, acct, detail) = lezMsigAccountOf(config{"chain"}.getStr("lez:local"), address, cfgJson)
if not ok: raise newException(RegistryError, detail)
newLezMsigDriver(acct)
of "xmr-msig":
# A Monero room treasury (ADR-020): {chain, address, k, members: [room identity hex]}.
var members: seq[string]
for m in config{"members"}.getElems(): members.add m.getStr()
newXmrMsigDriver(XmrMsigAccount(chain: config{"chain"}.getStr(), address: config{"address"}.getStr(),
k: config{"k"}.getInt(2), members: members))
of "btc-frost":
# A FROST account (Phase D): {network, recovery: the ceremony's recovery data, hex}.
newBtcFrostDriver(frostAccount(config{"network"}.getStr("regtest"), hexToBytes(config{"recovery"}.getStr())))
of "lez-frost":
# A LEZ FROST account (exo-55e): {chain, recovery: the ceremony's recovery data, hex}.
newLezFrostDriver(lezFrostAccount(config{"chain"}.getStr("lez:testnet"), hexToBytes(config{"recovery"}.getStr())))
of "evm-split":
# A split settling on an EVM chain (exo-a90.3): {chain: CAIP-2, members: [room identity hex]}.
# No account — the parties are named in each split's effect; members only gate proposing.
var members: seq[string]
for m in config{"members"}.getElems(): members.add m.getStr()
newSplitDriver(EvmSplitFamily, config{"chain"}.getStr("eip155:31337"), members)
of "lez-split":
# The private split on a LEZ zone (exo-a90.9): {chain: CAIP-2, members: [room identity hex]}.
var members: seq[string]
for m in config{"members"}.getElems(): members.add m.getStr()
newSplitDriver(LezSplitFamily, config{"chain"}.getStr("lez:testnet"), members)
of "btc-split":
# A split paid in BTC (exo-d17): {chain: CAIP-2 bip122:…, members: [room identity hex]}.
var members: seq[string]
for m in config{"members"}.getElems(): members.add m.getStr()
newSplitDriver(BtcSplitFamily, config{"chain"}.getStr("bip122:0f9188f13cb7b2c71f2a335e3a4fc328"), members)
of "monero-split":
# A split paid in XMR (exo-dcc.5, ADR-018): {chain: CAIP-2 monero:…, members: [room identity hex]}.
# Stagenet unless named: mainnet waits for exo-dcc.8.
var members: seq[string]
for m in config{"members"}.getElems(): members.add m.getStr()
newSplitDriver(MoneroSplitFamily, config{"chain"}.getStr("monero:76ee3cc98646292206cd3e86f74d88b4"), members)
of "stub":
newStubDriver(rounds = config{"rounds"}.getInt(1),
threshold = config{"threshold"}.getInt(2),
verifyResult = config{"verifyResult"}.getBool(true))
of "threshold":
newThresholdDriver(parseRoster(config), config{"k"}.getInt(2))
of "unanimous":
# The k = n threshold over the same roster (driver-as-proposal: admitted into a room
# only by an approved add-driver, see kinds.nim).
let roster = parseRoster(config)
newThresholdDriver(roster, max(1, roster.len))
of "charter":
# A room's charter (exo-267.1): its electorate and k are named in each charter's
# effect; the roster is the room's members, for the sign-path check only.
var members: seq[string]
for m in config{"roster"}.getElems(): members.add m.getStr().toLowerAscii().replace("0x", "")
newCharterDriver(members)
of "frost":
# 2-round Schnorr-threshold structure over an Ed25519 roster. Same roster/k
# config shape as "threshold"; the difference is describe().rounds = 2, so the
# core runs two collection passes. See frost.nim for the scaffold boundary.
newFrostDriver(parseRoster(config), config{"k"}.getInt(2))
of "invoke":
# The generic module-action driver (P-D1): coordinate "the room agrees to call
# module.method(args)" as a k-of-n Ed25519 endorsement over the room roster. The
# action (module/method/args) lives in the EFFECT, not the driver, so this is a
# generic roster driver; the EXECUTION path (P-D2) reads the module/method from
# the effect and gates on the allowlist + the target's capability policy.
newInvokeDriver(parseRoster(config), config{"k"}.getInt(1))
of "eip191":
# A Tier-1 (module-native) driver beyond Safe (P-D6): the room's signers each
# EIP-191 personal-sign the effect; a contribution counts only if its secp256k1
# signature recovers to a configured signer. Settles nothing on-chain — a signed
# group attestation. `signers` are 20-byte hex addresses, like a Safe's owners.
var signers: seq[Address]
if config.hasKey("signers") and config["signers"].kind == JArray:
for s in config["signers"]: signers.add hexToAddr(s.getStr())
newPersonalSignDriver(signers = signers, threshold = config{"threshold"}.getInt(2))
else:
# Never a fallback to another family (kinds.nim): a kind this client does not have
# is refused here, and resolveKind turns it into an UnsupportedDriver for the fold.
raise newException(RegistryError, "unknown driver kind: " & kind)