@@ -13,8 +13,8 @@ import { toHex } from "./hex"
1313 * @param {BinCodeable<T> } _type
1414 * @param {T } value
1515 */
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 ) ;
1818 _type . encode ( bc , value ) ;
1919 return bc . slice ( ) ;
2020}
@@ -24,8 +24,8 @@ export function encode(_type, value) {
2424 * @param {BinCodeable<T> } _type
2525 * @param {ArrayBuffer } value
2626 */
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 ) ;
2929 return _type . decode ( bc ) ;
3030}
3131
@@ -35,10 +35,12 @@ export class BinCode {
3535 *
3636 * @param {DataView } dataview
3737 * @param {number } idx
38+ * @param {any } options
3839 */
39- constructor ( dataview , idx = 0 ) {
40+ constructor ( dataview , idx = 0 , options = { } ) {
4041 this . dataview = dataview ;
4142 this . idx = idx ;
43+ this . options = options ;
4244 }
4345
4446 /**
@@ -100,6 +102,7 @@ export function Vec(inner) {
100102 } ,
101103 decode ( bc ) {
102104 let len = VarUint . decode ( bc ) ;
105+ /** @type {any[] } */
103106 let val = new Array ( len ) ;
104107 for ( let i = 0 ; i < len ; i ++ ) {
105108 val [ i ] = inner . decode ( bc ) ;
@@ -137,13 +140,23 @@ export function Struct(name, inner) {
137140 }
138141 }
139142}
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+ /**
142150 * @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
143156 * @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> > }
147160 */
148161export function Enum ( name , toDiscriminant , definitions ) {
149162 return {
@@ -165,7 +178,6 @@ export function Enum(name, toDiscriminant, definitions) {
165178/**
166179 * @template T
167180 * @param {BinCodeable<T> } inner
168- * @returns {BinCodeable<T | null> }
169181 */
170182export function Option ( inner ) {
171183 return Enum ( 'Option<' + inner . name + '>' , x => x == null ? 0 : 1 , {
@@ -198,7 +210,7 @@ export function Lazy(makeBincodeable) {
198210 }
199211}
200212
201- /** @type {BinCodeable<undefined > } */
213+ /** @type {BinCodeable<never > } */
202214export const TODO = {
203215 name : 'TODO' ,
204216 encode ( bc , num ) {
@@ -216,7 +228,7 @@ export const Nothing = {
216228 decode ( bc ) { }
217229}
218230
219- /** @type {BinCodeable<undefined > } */
231+ /** @type {BinCodeable<null > } */
220232export const Null = {
221233 name : 'Null' ,
222234 encode ( bc , num ) { } ,
@@ -503,23 +515,24 @@ export function FixedBytes(length) {
503515 }
504516}
505517
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+ }
523536
524537export const IdentityCreateTransition = Lazy ( ( ) =>
525538 Enum ( 'IdentityCreateTransition' , x => 0 , {
@@ -531,10 +544,10 @@ export const IdentityCreateTransition = Lazy(() =>
531544 asset_lock_proof : RawAssetLockProof ,
532545 user_fee_increase : UserFeeIncrease ,
533546 // #[platform_signable(exclude_from_sig_hash)]
534- signature : BinaryData ,
547+ signature : NotSignable ( BinaryData ) ,
535548 // #[cfg_attr(feature = "state-transition-serde-conversion", serde(skip))]
536549 // #[platform_signable(exclude_from_sig_hash)]
537- identity_id : Identifier ,
550+ identity_id : NotSignable ( Identifier ) ,
538551 } )
539552 } )
540553)
@@ -584,23 +597,7 @@ export const ContractBounds = Lazy(() =>
584597 } )
585598)
586599
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 , {
604601 0 : Struct ( 'IdentityPublicKeyInCreationV0' , {
605602 $version : Constant ( '0' ) ,
606603 id : KeyID ,
@@ -612,7 +609,7 @@ export const IdentityPublicKeyInCreation = Enum('IdentityPublicKeyInCreation', x
612609 data : BinaryData ,
613610 // /// The signature is needed for ECDSA_SECP256K1 Key type and BLS12_381 Key type
614611 // #[platform_signable(exclude_from_sig_hash)]
615- signature : BinaryData ,
612+ signature : NotSignable ( BinaryData ) ,
616613 } )
617614} )
618615
@@ -733,9 +730,7 @@ export function toJsonCamelCase(value) {
733730 return JSON . stringify ( value , jsonCamelCaseReplacer )
734731}
735732
736-
737-
738- // TODO!
733+ // TODO: Implement all the other transitions
739734export const StateTransition = Enum ( 'StateTransition' , x => 3 , {
740735 // 0: DataContractCreateTransition, //DataContractCreate(DataContractCreateTransition),
741736 // 1: DataContractUpdateTransition, //DataContractUpdate(DataContractUpdateTransition),
@@ -747,15 +742,3 @@ export const StateTransition = Enum('StateTransition', x => 3, {
747742 // 7: IdentityCreditTransferTransition, //IdentityCreditTransfer(IdentityCreditTransferTransition),
748743 // 8: MasternodeVoteTransition, //MasternodeVote(MasternodeVoteTransition),
749744} )
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