@@ -253,9 +253,25 @@ func isPubKey(script []byte) (bool, []byte) {
253253 return false , nil
254254}
255255
256+ // isCashTokensScript returns whether or not the script is a
257+ // cashtokens script along with the script itself if it is.
258+ // We can do better compression on this.
259+ func isCashTokensScript (script []byte ) (bool , []byte ) {
260+ if len (script ) > 0 && script [0 ] == txscript .SPECIAL_TOKEN_PREFIX {
261+ return true , script
262+ }
263+ return false , nil
264+ }
265+
256266// compressedScriptSize returns the number of bytes the passed script would take
257267// when encoded with the domain specific compression algorithm described above.
258268func compressedScriptSize (pkScript []byte ) int {
269+
270+ if valid , _ := isCashTokensScript (pkScript ); valid {
271+ return serializeSizeVLQ (uint64 (len (pkScript )+ numSpecialScripts )) +
272+ len (pkScript )
273+ }
274+
259275 // Pay-to-pubkey-hash script.
260276 if valid , _ := isPubKeyHash (pkScript ); valid {
261277 return 21
@@ -311,34 +327,38 @@ func decodeCompressedScriptSize(serialized []byte) int {
311327// handle the number of bytes returned by the compressedScriptSize function or
312328// it will panic.
313329func putCompressedScript (target , pkScript []byte ) int {
314- // Pay-to-pubkey-hash script.
315- if valid , hash := isPubKeyHash (pkScript ); valid {
316- target [0 ] = cstPayToPubKeyHash
317- copy (target [1 :21 ], hash )
318- return 21
319- }
320330
321- // Pay-to-script-hash script.
322- if valid , hash := isScriptHash (pkScript ); valid {
323- target [0 ] = cstPayToScriptHash
324- copy (target [1 :21 ], hash )
325- return 21
326- }
331+ if valid , _ := isCashTokensScript (pkScript ); ! valid {
327332
328- // Pay-to-pubkey (compressed or uncompressed) script.
329- if valid , serializedPubKey := isPubKey (pkScript ); valid {
330- pubKeyFormat := serializedPubKey [0 ]
331- switch pubKeyFormat {
332- case 0x02 , 0x03 :
333- target [0 ] = pubKeyFormat
334- copy (target [1 :33 ], serializedPubKey [1 :33 ])
335- return 33
336- case 0x04 :
337- // Encode the oddness of the serialized pubkey into the
338- // compressed script type.
339- target [0 ] = pubKeyFormat | (serializedPubKey [64 ] & 0x01 )
340- copy (target [1 :33 ], serializedPubKey [1 :33 ])
341- return 33
333+ // Pay-to-pubkey-hash script.
334+ if valid , hash := isPubKeyHash (pkScript ); valid {
335+ target [0 ] = cstPayToPubKeyHash
336+ copy (target [1 :21 ], hash )
337+ return 21
338+ }
339+
340+ // Pay-to-script-hash script.
341+ if valid , hash := isScriptHash (pkScript ); valid {
342+ target [0 ] = cstPayToScriptHash
343+ copy (target [1 :21 ], hash )
344+ return 21
345+ }
346+
347+ // Pay-to-pubkey (compressed or uncompressed) script.
348+ if valid , serializedPubKey := isPubKey (pkScript ); valid {
349+ pubKeyFormat := serializedPubKey [0 ]
350+ switch pubKeyFormat {
351+ case 0x02 , 0x03 :
352+ target [0 ] = pubKeyFormat
353+ copy (target [1 :33 ], serializedPubKey [1 :33 ])
354+ return 33
355+ case 0x04 :
356+ // Encode the oddness of the serialized pubkey into the
357+ // compressed script type.
358+ target [0 ] = pubKeyFormat | (serializedPubKey [64 ] & 0x01 )
359+ copy (target [1 :33 ], serializedPubKey [1 :33 ])
360+ return 33
361+ }
342362 }
343363 }
344364
0 commit comments