@@ -17,12 +17,21 @@ var (
1717
1818 // ErrSigningClaimFieldNotFound is returned when the 'Channel' & 'Amount' fields are both required, but were not found.
1919 ErrSigningClaimFieldNotFound = errors .New ("'Channel' & 'Amount' fields are both required, but were not found" )
20+ // ErrBatchFlagsFieldNotFound is returned when the 'flags' field is missing.
21+ ErrBatchFlagsFieldNotFound = errors .New ("no field `flags`" )
22+ // ErrBatchTxIDsFieldNotFound is returned when the 'txIDs' field is missing.
23+ ErrBatchTxIDsFieldNotFound = errors .New ("no field `txIDs`" )
24+ // ErrBatchTxIDsNotArray is returned when the 'txIDs' field is not an array.
25+ ErrBatchTxIDsNotArray = errors .New ("txIDs field must be an array" )
26+ // ErrBatchTxIDNotString is returned when a txID is not a string.
27+ ErrBatchTxIDNotString = errors .New ("each txID must be a string" )
2028)
2129
2230const (
2331 txMultiSigPrefix = "534D5400"
2432 paymentChannelClaimPrefix = "434C4D00"
2533 txSigPrefix = "53545800"
34+ batchPrefix = "42434800"
2635)
2736
2837// Encode converts a JSON transaction object to a hex string in the canonical binary format.
@@ -116,6 +125,55 @@ func EncodeForSigningClaim(json map[string]any) (string, error) {
116125 return strings .ToUpper (paymentChannelClaimPrefix + hex .EncodeToString (channel ) + hex .EncodeToString (amount )), nil
117126}
118127
128+ // EncodeForSigningBatch encodes a batch transaction into binary format in preparation for signing.
129+ func EncodeForSigningBatch (json map [string ]any ) (string , error ) {
130+ if json ["flags" ] == nil {
131+ return "" , ErrBatchFlagsFieldNotFound
132+ }
133+ if json ["txIDs" ] == nil {
134+ return "" , ErrBatchTxIDsFieldNotFound
135+ }
136+
137+ // Extract and validate txIDs
138+ txIDsInterface , ok := json ["txIDs" ].([]any )
139+ if ! ok {
140+ return "" , ErrBatchTxIDsNotArray
141+ }
142+
143+ // Create UInt32 for flags
144+ flagsType := & types.UInt32 {}
145+ flagsBytes , err := flagsType .FromJSON (json ["flags" ])
146+ if err != nil {
147+ return "" , err
148+ }
149+
150+ // Create UInt32 for txIDs length
151+ txIDsLengthType := & types.UInt32 {}
152+ txIDsLengthBytes , err := txIDsLengthType .FromJSON (uint32 (len (txIDsInterface )))
153+ if err != nil {
154+ return "" , err
155+ }
156+
157+ // Build the result string
158+ result := batchPrefix + hex .EncodeToString (flagsBytes ) + hex .EncodeToString (txIDsLengthBytes )
159+
160+ // Add each transaction ID
161+ for _ , txID := range txIDsInterface {
162+ txIDStr , ok := txID .(string )
163+ if ! ok {
164+ return "" , ErrBatchTxIDNotString
165+ }
166+ hash256 := types .NewHash256 ()
167+ txIDBytes , err := hash256 .FromJSON (txIDStr )
168+ if err != nil {
169+ return "" , err
170+ }
171+ result += hex .EncodeToString (txIDBytes )
172+ }
173+
174+ return strings .ToUpper (result ), nil
175+ }
176+
119177// removeNonSigningFields removes the fields from a JSON transaction object that should not be signed.
120178func removeNonSigningFields (json map [string ]any ) map [string ]any {
121179 for k := range json {
0 commit comments