@@ -5,12 +5,14 @@ import {
55 PostConditionMode ,
66 PostConditionPrincipalId ,
77 PostConditionType ,
8+ PoxConditionCode ,
89} from './constants' ;
910import {
1011 FungibleComparator ,
1112 NonFungibleComparator ,
1213 PostCondition ,
1314 PostConditionModeName ,
15+ PoxComparator ,
1416} from './postcondition-types' ;
1517import { AssetString } from './types' ;
1618import {
@@ -50,6 +52,13 @@ enum PostConditionCodeWireType {
5052 'maybe-sent' = NonFungibleConditionCode . MaybeSent ,
5153}
5254
55+ /** @internal */
56+ enum PoxConditionCodeWireType {
57+ 'will-not-perform' = PoxConditionCode . WillNotPerform ,
58+ 'may-perform' = PoxConditionCode . MayPerform ,
59+ 'will-perform' = PoxConditionCode . WillPerform ,
60+ }
61+
5362export function postConditionToWire ( postcondition : PostCondition ) : PostConditionWire {
5463 switch ( postcondition . type ) {
5564 case 'stx-postcondition' :
@@ -87,6 +96,27 @@ export function postConditionToWire(postcondition: PostCondition): PostCondition
8796 asset : parseAssetString ( postcondition . asset ) ,
8897 assetName : postcondition . assetId ,
8998 } ;
99+ case 'staking-postcondition' :
100+ return {
101+ type : StacksWireType . PostCondition ,
102+ conditionType : PostConditionType . Staking ,
103+ principal :
104+ postcondition . address === 'origin'
105+ ? { type : StacksWireType . Principal , prefix : PostConditionPrincipalId . Origin }
106+ : parsePrincipalString ( postcondition . address ) ,
107+ conditionCode : conditionTypeToByte ( postcondition . condition ) as FungibleConditionCode ,
108+ amount : parsePostConditionAmount ( postcondition . amount ) ,
109+ } ;
110+ case 'pox-postcondition' :
111+ return {
112+ type : StacksWireType . PostCondition ,
113+ conditionType : PostConditionType . PoX ,
114+ principal :
115+ postcondition . address === 'origin'
116+ ? { type : StacksWireType . Principal , prefix : PostConditionPrincipalId . Origin }
117+ : parsePrincipalString ( postcondition . address ) ,
118+ conditionCode : poxConditionTypeToByte ( postcondition . condition ) ,
119+ } ;
90120 default :
91121 throw new Error ( 'Invalid post condition type' ) ;
92122 }
@@ -117,6 +147,19 @@ export function wireToPostCondition(wire: PostConditionWire): PostCondition {
117147 asset : assetWireToString ( wire . asset ) ,
118148 assetId : wire . assetName ,
119149 } ;
150+ case PostConditionType . Staking :
151+ return {
152+ type : 'staking-postcondition' ,
153+ address : principalWireToString ( wire . principal ) ,
154+ condition : conditionByteToType ( wire . conditionCode ) ,
155+ amount : wire . amount . toString ( ) ,
156+ } ;
157+ case PostConditionType . PoX :
158+ return {
159+ type : 'pox-postcondition' ,
160+ address : principalWireToString ( wire . principal ) ,
161+ condition : poxConditionByteToType ( wire . conditionCode ) ,
162+ } ;
120163 default : {
121164 const _exhaustiveCheck : never = wire ;
122165 throw new Error ( `Invalid post condition type: ${ _exhaustiveCheck } ` ) ;
@@ -149,6 +192,19 @@ export function conditionByteToType<T extends FungibleConditionCode | NonFungibl
149192 ) [ wireType ] ;
150193}
151194
195+ /** @internal */
196+ export function poxConditionTypeToByte ( condition : PoxComparator ) : PoxConditionCode {
197+ return ( PoxConditionCodeWireType as unknown as Record < PoxComparator , PoxConditionCode > ) [
198+ condition
199+ ] ;
200+ }
201+
202+ /** @internal */
203+ export function poxConditionByteToType ( wireType : PoxConditionCode ) : PoxComparator {
204+ // numerical enums are bidirectional in TypeScript
205+ return ( PoxConditionCodeWireType as unknown as Record < PoxConditionCode , PoxComparator > ) [ wireType ] ;
206+ }
207+
152208/**
153209 * Convert a post condition to a hex string
154210 * @param postcondition - The post condition object to convert
0 commit comments