@@ -13,8 +13,8 @@ import { toHex } from "./hex"
13
13
* @param {BinCodeable<T> } _type
14
14
* @param {T } value
15
15
*/
16
- export function encode ( _type , value ) {
17
- const bc = new BinCode ( new DataView ( new ArrayBuffer ( 16 ) ) ) ;
16
+ export function encode ( _type , value , options = { } ) {
17
+ const bc = new BinCode ( new DataView ( new ArrayBuffer ( 16 ) ) , 0 , options ) ;
18
18
_type . encode ( bc , value ) ;
19
19
return bc . slice ( ) ;
20
20
}
@@ -24,8 +24,8 @@ export function encode(_type, value) {
24
24
* @param {BinCodeable<T> } _type
25
25
* @param {ArrayBuffer } value
26
26
*/
27
- export function decode ( _type , value ) {
28
- const bc = new BinCode ( new DataView ( value ) ) ;
27
+ export function decode ( _type , value , options = { } ) {
28
+ const bc = new BinCode ( new DataView ( value ) , 0 , options ) ;
29
29
return _type . decode ( bc ) ;
30
30
}
31
31
@@ -35,10 +35,12 @@ export class BinCode {
35
35
*
36
36
* @param {DataView } dataview
37
37
* @param {number } idx
38
+ * @param {any } options
38
39
*/
39
- constructor ( dataview , idx = 0 ) {
40
+ constructor ( dataview , idx = 0 , options = { } ) {
40
41
this . dataview = dataview ;
41
42
this . idx = idx ;
43
+ this . options = options ;
42
44
}
43
45
44
46
/**
@@ -100,6 +102,7 @@ export function Vec(inner) {
100
102
} ,
101
103
decode ( bc ) {
102
104
let len = VarUint . decode ( bc ) ;
105
+ /** @type {any[] } */
103
106
let val = new Array ( len ) ;
104
107
for ( let i = 0 ; i < len ; i ++ ) {
105
108
val [ i ] = inner . decode ( bc ) ;
@@ -137,13 +140,23 @@ export function Struct(name, inner) {
137
140
}
138
141
}
139
142
}
140
-
141
- /**
143
+
144
+ /**
145
+ * @template T
146
+ * @typedef {{[k in keyof T]: T[k] extends BinCodeable<infer U>? U : never}[keyof T] } EnumType
147
+ */
148
+
149
+ /**
142
150
* @template T
151
+ * @typedef {T extends infer U? {[k in keyof U]: U[k]} : never } Expand
152
+ */
153
+
154
+ /**
155
+ * @template {{[k: number]: BinCodeable<any>}} T
143
156
* @param {string } name
144
- * @param {(val: T ) => number } toDiscriminant
145
- * @param {{[k: number]: BinCodeable<T>} } definitions
146
- * @returns {BinCodeable<T > }
157
+ * @param {(val: EnumType<T> ) => number } toDiscriminant
158
+ * @param {T } definitions
159
+ * @returns {BinCodeable<EnumType<T> > }
147
160
*/
148
161
export function Enum ( name , toDiscriminant , definitions ) {
149
162
return {
@@ -165,7 +178,6 @@ export function Enum(name, toDiscriminant, definitions) {
165
178
/**
166
179
* @template T
167
180
* @param {BinCodeable<T> } inner
168
- * @returns {BinCodeable<T | null> }
169
181
*/
170
182
export function Option ( inner ) {
171
183
return Enum ( 'Option<' + inner . name + '>' , x => x == null ? 0 : 1 , {
@@ -198,7 +210,7 @@ export function Lazy(makeBincodeable) {
198
210
}
199
211
}
200
212
201
- /** @type {BinCodeable<undefined > } */
213
+ /** @type {BinCodeable<never > } */
202
214
export const TODO = {
203
215
name : 'TODO' ,
204
216
encode ( bc , num ) {
@@ -216,7 +228,7 @@ export const Nothing = {
216
228
decode ( bc ) { }
217
229
}
218
230
219
- /** @type {BinCodeable<undefined > } */
231
+ /** @type {BinCodeable<null > } */
220
232
export const Null = {
221
233
name : 'Null' ,
222
234
encode ( bc , num ) { } ,
@@ -503,23 +515,24 @@ export function FixedBytes(length) {
503
515
}
504
516
}
505
517
506
- export const IdentityCreateTransitionSignable = Lazy ( ( ) =>
507
- Enum ( 'IdentityCreateTransitionSignable' , x => 0 , {
508
- 0 : Struct ( 'IdentityCreateTransitionV0Signable' , {
509
- $version : Constant ( '0' ) ,
510
- // // When signing, we don't sign the signatures for keys
511
- // #[platform_signable(into = "Vec<IdentityPublicKeyInCreationSignable>")]
512
- public_keys : Vec ( IdentityPublicKeyInCreationSignable ) ,
513
- asset_lock_proof : RawAssetLockProof ,
514
- user_fee_increase : UserFeeIncrease ,
515
- // #[platform_signable(exclude_from_sig_hash)]
516
- // signature: BinaryData,
517
- // #[cfg_attr(feature = "state-transition-serde-conversion", serde(skip))]
518
- // #[platform_signable(exclude_from_sig_hash)]
519
- // identity_id: Identifier,
520
- } )
521
- } )
522
- )
518
+ /**
519
+ * @template T
520
+ * @param {BinCodeable<T> } inner
521
+ * @returns {BinCodeable<T> }
522
+ */
523
+ export function NotSignable ( inner ) {
524
+ return {
525
+ name : 'NotSignable<' + inner . name + '>' ,
526
+ encode ( bc , value ) {
527
+ if ( ! bc . options . signable )
528
+ inner . encode ( bc , value ) ;
529
+ } ,
530
+ decode ( bc ) {
531
+ if ( ! bc . options . signable )
532
+ return inner . decode ( bc ) ;
533
+ }
534
+ }
535
+ }
523
536
524
537
export const IdentityCreateTransition = Lazy ( ( ) =>
525
538
Enum ( 'IdentityCreateTransition' , x => 0 , {
@@ -531,10 +544,10 @@ export const IdentityCreateTransition = Lazy(() =>
531
544
asset_lock_proof : RawAssetLockProof ,
532
545
user_fee_increase : UserFeeIncrease ,
533
546
// #[platform_signable(exclude_from_sig_hash)]
534
- signature : BinaryData ,
547
+ signature : NotSignable ( BinaryData ) ,
535
548
// #[cfg_attr(feature = "state-transition-serde-conversion", serde(skip))]
536
549
// #[platform_signable(exclude_from_sig_hash)]
537
- identity_id : Identifier ,
550
+ identity_id : NotSignable ( Identifier ) ,
538
551
} )
539
552
} )
540
553
)
@@ -584,23 +597,7 @@ export const ContractBounds = Lazy(() =>
584
597
} )
585
598
)
586
599
587
- export const IdentityPublicKeyInCreationSignable = Enum ( 'IdentityPublicKeyInCreationSignable' , x => x . $version , {
588
- 0 : Struct ( 'IdentityPublicKeyInCreationV0Signable' , {
589
- $version : Constant ( '0' ) ,
590
- id : KeyID ,
591
- type : KeyType ,
592
- purpose : Purpose ,
593
- security_level : SecurityLevel ,
594
- contract_bounds : Option ( ContractBounds ) ,
595
- read_only : Bool ,
596
- data : BinaryData ,
597
- // /// The signature is needed for ECDSA_SECP256K1 Key type and BLS12_381 Key type
598
- // #[platform_signable(exclude_from_sig_hash)]
599
- // signature: BinaryData,
600
- } ) ,
601
- } )
602
-
603
- export const IdentityPublicKeyInCreation = Enum ( 'IdentityPublicKeyInCreation' , x => x . $version , {
600
+ export const IdentityPublicKeyInCreation = Enum ( 'IdentityPublicKeyInCreation' , x => + x . $version , {
604
601
0 : Struct ( 'IdentityPublicKeyInCreationV0' , {
605
602
$version : Constant ( '0' ) ,
606
603
id : KeyID ,
@@ -612,7 +609,7 @@ export const IdentityPublicKeyInCreation = Enum('IdentityPublicKeyInCreation', x
612
609
data : BinaryData ,
613
610
// /// The signature is needed for ECDSA_SECP256K1 Key type and BLS12_381 Key type
614
611
// #[platform_signable(exclude_from_sig_hash)]
615
- signature : BinaryData ,
612
+ signature : NotSignable ( BinaryData ) ,
616
613
} )
617
614
} )
618
615
@@ -733,9 +730,7 @@ export function toJsonCamelCase(value) {
733
730
return JSON . stringify ( value , jsonCamelCaseReplacer )
734
731
}
735
732
736
-
737
-
738
- // TODO!
733
+ // TODO: Implement all the other transitions
739
734
export const StateTransition = Enum ( 'StateTransition' , x => 3 , {
740
735
// 0: DataContractCreateTransition, //DataContractCreate(DataContractCreateTransition),
741
736
// 1: DataContractUpdateTransition, //DataContractUpdate(DataContractUpdateTransition),
@@ -747,15 +742,3 @@ export const StateTransition = Enum('StateTransition', x => 3, {
747
742
// 7: IdentityCreditTransferTransition, //IdentityCreditTransfer(IdentityCreditTransferTransition),
748
743
// 8: MasternodeVoteTransition, //MasternodeVote(MasternodeVoteTransition),
749
744
} )
750
-
751
- export const StateTransitionSignable = Enum ( 'StateTransition' , x => 3 , {
752
- // 0: DataContractCreateTransition, //DataContractCreate(DataContractCreateTransition),
753
- // 1: DataContractUpdateTransition, //DataContractUpdate(DataContractUpdateTransition),
754
- // 2: DocumentsBatchTransition, //DocumentsBatch(DocumentsBatchTransition),
755
- 3 : IdentityCreateTransitionSignable , //IdentityCreate(IdentityCreateTransition),
756
- // 4: IdentityTopUpTransition, //IdentityTopUp(IdentityTopUpTransition),
757
- // 5: IdentityCreditWithdrawalTransition, //IdentityCreditWithdrawal(IdentityCreditWithdrawalTransition),
758
- // 6: IdentityUpdateTransition, //IdentityUpdate(IdentityUpdateTransition),
759
- // 7: IdentityCreditTransferTransition, //IdentityCreditTransfer(IdentityCreditTransferTransition),
760
- // 8: MasternodeVoteTransition, //MasternodeVote(MasternodeVoteTransition),
761
- } )
0 commit comments