@@ -21,6 +21,7 @@ import (
2121 "strings"
2222
2323 "github.com/blinklabs-io/gouroboros/cbor"
24+ "github.com/blinklabs-io/plutigo/pkg/data"
2425 "github.com/btcsuite/btcd/btcutil/base58"
2526 "github.com/btcsuite/btcd/btcutil/bech32"
2627 "golang.org/x/crypto/sha3"
@@ -285,6 +286,69 @@ func (a *Address) MarshalCBOR() ([]byte, error) {
285286 return cbor .Encode (addrBytes )
286287}
287288
289+ func (a * Address ) ToPlutusData () data.PlutusData {
290+ if a .addressType == AddressTypeByron {
291+ // There is no PlutusData representation for Byron addresses
292+ return nil
293+ }
294+ // Build payment part
295+ var paymentPd data.PlutusData
296+ switch a .addressType {
297+ case AddressTypeKeyKey , AddressTypeKeyScript , AddressTypeKeyPointer , AddressTypeKeyNone :
298+ tmpCred := & Credential {
299+ CredType : CredentialTypeAddrKeyHash ,
300+ Credential : NewBlake2b224 (a .paymentAddress ),
301+ }
302+ paymentPd = data .NewConstr (
303+ 0 ,
304+ tmpCred .ToPlutusData (),
305+ )
306+ case AddressTypeScriptKey , AddressTypeScriptScript , AddressTypeScriptPointer , AddressTypeScriptNone :
307+ tmpCred := & Credential {
308+ CredType : CredentialTypeScriptHash ,
309+ Credential : NewBlake2b224 (a .paymentAddress ),
310+ }
311+ paymentPd = data .NewConstr (
312+ 1 ,
313+ tmpCred .ToPlutusData (),
314+ )
315+ default :
316+ return nil
317+ }
318+ // Build stake part
319+ var stakePd data.PlutusData
320+ switch a .addressType {
321+ case AddressTypeKeyKey , AddressTypeScriptKey , AddressTypeNoneKey :
322+ tmpCred := & Credential {
323+ CredType : CredentialTypeAddrKeyHash ,
324+ Credential : NewBlake2b224 (a .stakingAddress ),
325+ }
326+ stakePd = data .NewConstr (
327+ 0 ,
328+ tmpCred .ToPlutusData (),
329+ )
330+ case AddressTypeKeyScript , AddressTypeScriptScript , AddressTypeNoneScript :
331+ tmpCred := & Credential {
332+ CredType : CredentialTypeScriptHash ,
333+ Credential : NewBlake2b224 (a .stakingAddress ),
334+ }
335+ stakePd = data .NewConstr (
336+ 0 ,
337+ tmpCred .ToPlutusData (),
338+ )
339+ case AddressTypeKeyNone , AddressTypeScriptNone :
340+ stakePd = data .NewConstr (1 )
341+ // TODO: add support for pointer addresses once we add it to Address
342+ default :
343+ return nil
344+ }
345+ return data .NewConstr (
346+ 0 ,
347+ paymentPd ,
348+ stakePd ,
349+ )
350+ }
351+
288352func (a Address ) NetworkId () uint {
289353 if a .addressType == AddressTypeByron {
290354 // Use Shelley network ID convention
0 commit comments