@@ -2,7 +2,6 @@ import { Address, Bytes, Hash, Hex } from 'ox'
22import {
33 isRawConfig ,
44 isRawNestedLeaf ,
5- isRawNode ,
65 isRawSignerLeaf ,
76 isSignedSapientSignerLeaf ,
87 isSignedSignerLeaf ,
@@ -55,46 +54,89 @@ export type Leaf = SignerLeaf | SapientSignerLeaf | SubdigestLeaf | AnyAddressSu
5554
5655export type Topology = Node | Leaf
5756
57+ /** Encoded topology types for JSON serialization */
58+ type EncodedSignerLeaf = {
59+ type : 'signer'
60+ address : Address . Address
61+ weight : string
62+ }
63+
64+ type EncodedSapientSignerLeaf = {
65+ type : 'sapient-signer'
66+ address : Address . Address
67+ weight : string
68+ imageHash : Hex . Hex
69+ }
70+
71+ type EncodedSubdigestLeaf = {
72+ type : 'subdigest'
73+ digest : Hex . Hex
74+ }
75+
76+ type EncodedAnyAddressSubdigestLeaf = {
77+ type : 'any-address-subdigest'
78+ digest : Hex . Hex
79+ }
80+
81+ type EncodedNestedLeaf = {
82+ type : 'nested'
83+ tree : EncodedTopology
84+ weight : string
85+ threshold : string
86+ }
87+
88+ type EncodedNodeLeaf = Hex . Hex
89+
90+ export type EncodedLeaf =
91+ | EncodedSignerLeaf
92+ | EncodedSapientSignerLeaf
93+ | EncodedSubdigestLeaf
94+ | EncodedAnyAddressSubdigestLeaf
95+ | EncodedNestedLeaf
96+ | EncodedNodeLeaf
97+ export type EncodedNode = [ EncodedTopology , EncodedTopology ]
98+ export type EncodedTopology = EncodedNode | EncodedLeaf
99+
58100export type Config = {
59101 threshold : bigint
60102 checkpoint : bigint
61103 topology : Topology
62104 checkpointer ?: Address . Address
63105}
64106
65- export function isSignerLeaf ( cand : any ) : cand is SignerLeaf {
66- return typeof cand === 'object' && cand !== null && cand . type === 'signer'
107+ export function isSignerLeaf ( cand : unknown ) : cand is SignerLeaf {
108+ return typeof cand === 'object' && cand !== null && 'type' in cand && cand . type === 'signer'
67109}
68110
69- export function isSapientSignerLeaf ( cand : any ) : cand is SapientSignerLeaf {
70- return typeof cand === 'object' && cand !== null && cand . type === 'sapient-signer'
111+ export function isSapientSignerLeaf ( cand : unknown ) : cand is SapientSignerLeaf {
112+ return typeof cand === 'object' && cand !== null && 'type' in cand && cand . type === 'sapient-signer'
71113}
72114
73- export function isSubdigestLeaf ( cand : any ) : cand is SubdigestLeaf {
74- return typeof cand === 'object' && cand !== null && cand . type === 'subdigest'
115+ export function isSubdigestLeaf ( cand : unknown ) : cand is SubdigestLeaf {
116+ return typeof cand === 'object' && cand !== null && 'type' in cand && cand . type === 'subdigest'
75117}
76118
77- export function isAnyAddressSubdigestLeaf ( cand : any ) : cand is AnyAddressSubdigestLeaf {
78- return typeof cand === 'object' && cand !== null && cand . type === 'any-address-subdigest'
119+ export function isAnyAddressSubdigestLeaf ( cand : unknown ) : cand is AnyAddressSubdigestLeaf {
120+ return typeof cand === 'object' && cand !== null && 'type' in cand && cand . type === 'any-address-subdigest'
79121}
80122
81- export function isNodeLeaf ( cand : any ) : cand is NodeLeaf {
82- return Hex . validate ( cand ) && cand . length === 66
123+ export function isNodeLeaf ( cand : unknown ) : cand is NodeLeaf {
124+ return typeof cand === 'string' && Hex . validate ( cand ) && cand . length === 66
83125}
84126
85- export function isNestedLeaf ( cand : any ) : cand is NestedLeaf {
86- return typeof cand === 'object' && cand !== null && cand . type === 'nested'
127+ export function isNestedLeaf ( cand : unknown ) : cand is NestedLeaf {
128+ return typeof cand === 'object' && cand !== null && 'type' in cand && cand . type === 'nested'
87129}
88130
89- export function isNode ( cand : any ) : cand is Node {
131+ export function isNode ( cand : unknown ) : cand is Node {
90132 return Array . isArray ( cand ) && cand . length === 2 && isTopology ( cand [ 0 ] ) && isTopology ( cand [ 1 ] )
91133}
92134
93- export function isConfig ( cand : any ) : cand is Config {
94- return typeof cand === 'object' && 'threshold' in cand && 'checkpoint' in cand && 'topology' in cand
135+ export function isConfig ( cand : unknown ) : cand is Config {
136+ return typeof cand === 'object' && cand !== null && 'threshold' in cand && 'checkpoint' in cand && 'topology' in cand
95137}
96138
97- export function isLeaf ( cand : Topology ) : cand is Leaf {
139+ export function isLeaf ( cand : unknown ) : cand is Leaf {
98140 return (
99141 isSignerLeaf ( cand ) ||
100142 isSapientSignerLeaf ( cand ) ||
@@ -105,7 +147,7 @@ export function isLeaf(cand: Topology): cand is Leaf {
105147 )
106148}
107149
108- export function isTopology ( cand : any ) : cand is Topology {
150+ export function isTopology ( cand : unknown ) : cand is Topology {
109151 return isNode ( cand ) || isLeaf ( cand )
110152}
111153
@@ -310,7 +352,7 @@ export function configFromJson(json: string): Config {
310352 }
311353}
312354
313- function encodeTopology ( top : Topology ) : any {
355+ function encodeTopology ( top : Topology ) : EncodedTopology {
314356 if ( isNode ( top ) ) {
315357 return [ encodeTopology ( top [ 0 ] ) , encodeTopology ( top [ 1 ] ) ]
316358 } else if ( isSignerLeaf ( top ) ) {
@@ -350,7 +392,7 @@ function encodeTopology(top: Topology): any {
350392 throw new Error ( 'Invalid topology' )
351393}
352394
353- function decodeTopology ( obj : any ) : Topology {
395+ function decodeTopology ( obj : EncodedTopology ) : Topology {
354396 if ( Array . isArray ( obj ) ) {
355397 if ( obj . length !== 2 ) {
356398 throw new Error ( 'Invalid node structure in JSON' )
0 commit comments