Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support for parsing TX output datum #311

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions cbor/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,20 @@ func (v *Value) UnmarshalCBOR(data []byte) error {
func (v Value) Cbor() []byte {
return []byte(v.cborData)
}

type LazyValue struct {
*Value
}

func (l *LazyValue) UnmarshalCBOR(data []byte) error {
if l.Value == nil {
l.Value = &Value{}
}
l.cborData = string(data[:])
return nil
}

func (l *LazyValue) Decode() (*Value, error) {
err := l.Value.UnmarshalCBOR([]byte(l.cborData))
return l.Value, err
}
8 changes: 8 additions & 0 deletions cmd/block-fetch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ func main() {
}
}
}
if output.Datum() != nil {
datumValue, err := output.Datum().Decode()
if err != nil {
fmt.Printf(" Datum (hex): %x\n", output.Datum().Cbor())
} else {
fmt.Printf(" Datum: %#v\n", datumValue.Value)
}
}
fmt.Println("")
}
}
Expand Down
14 changes: 11 additions & 3 deletions ledger/alonzo.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,9 +114,9 @@ func (b *AlonzoTransactionBody) Outputs() []TransactionOutput {
type AlonzoTransactionOutput struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
OutputAddress Address
OutputAmount MaryTransactionOutputValue
DatumHash Blake2b256
OutputAddress Address
OutputAmount MaryTransactionOutputValue
TxOutputDatumHash *Blake2b256
}

func (o *AlonzoTransactionOutput) UnmarshalCBOR(cborData []byte) error {
Expand Down Expand Up @@ -157,6 +157,14 @@ func (o AlonzoTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
return o.OutputAmount.Assets
}

func (o AlonzoTransactionOutput) DatumHash() *Blake2b256 {
return o.TxOutputDatumHash
}

func (o AlonzoTransactionOutput) Datum() *cbor.LazyValue {
return nil
}

type AlonzoTransactionWitnessSet struct {
ShelleyTransactionWitnessSet
PlutusScripts []cbor.RawMessage `cbor:"3,keyasint,omitempty"`
Expand Down
80 changes: 76 additions & 4 deletions ledger/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,69 @@ func (b *BabbageTransactionBody) Outputs() []TransactionOutput {
return ret
}

const (
DatumOptionTypeHash = 0
DatumOptionTypeData = 1
)

type BabbageTransactionOutputDatumOption struct {
hash *Blake2b256
data *cbor.LazyValue
}

func (d *BabbageTransactionOutputDatumOption) UnmarshalCBOR(data []byte) error {
datumOptionType, err := cbor.DecodeIdFromList(data)
if err != nil {
return err
}
switch datumOptionType {
case DatumOptionTypeHash:
var tmpDatumHash struct {
cbor.StructAsArray
Type int
Hash Blake2b256
}
if _, err := cbor.Decode(data, &tmpDatumHash); err != nil {
return err
}
d.hash = &(tmpDatumHash.Hash)
case DatumOptionTypeData:
var tmpDatumData struct {
cbor.StructAsArray
Type int
DataCbor []byte
}
if _, err := cbor.Decode(data, &tmpDatumData); err != nil {
return err
}
var datumValue cbor.LazyValue
if _, err := cbor.Decode(tmpDatumData.DataCbor, &datumValue); err != nil {
return err
}
d.data = &(datumValue)
default:
return fmt.Errorf("unsupported datum option type: %d", datumOptionType)
}
return nil
}

func (d *BabbageTransactionOutputDatumOption) MarshalCBOR() ([]byte, error) {
var tmpObj []interface{}
if d.hash != nil {
tmpObj = []interface{}{DatumOptionTypeHash, d.hash}
} else if d.data != nil {
tmpObj = []interface{}{DatumOptionTypeData, cbor.Tag{Number: 24, Content: d.data.Cbor()}}
} else {
return nil, fmt.Errorf("unknown datum option type")
}
return cbor.Encode(&tmpObj)
}

type BabbageTransactionOutput struct {
OutputAddress Address `cbor:"0,keyasint,omitempty"`
OutputAmount MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"`
DatumOption []cbor.RawMessage `cbor:"2,keyasint,omitempty"`
ScriptRef cbor.Tag `cbor:"3,keyasint,omitempty"`
OutputAddress Address `cbor:"0,keyasint,omitempty"`
OutputAmount MaryTransactionOutputValue `cbor:"1,keyasint,omitempty"`
DatumOption *BabbageTransactionOutputDatumOption `cbor:"2,keyasint,omitempty"`
ScriptRef *cbor.Tag `cbor:"3,keyasint,omitempty"`
legacyOutput bool
}

Expand Down Expand Up @@ -202,6 +260,20 @@ func (o BabbageTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
return o.OutputAmount.Assets
}

func (o BabbageTransactionOutput) DatumHash() *Blake2b256 {
if o.DatumOption != nil {
return o.DatumOption.hash
}
return nil
}

func (o BabbageTransactionOutput) Datum() *cbor.LazyValue {
if o.DatumOption != nil {
return o.DatumOption.data
}
return nil
}

type BabbageTransactionWitnessSet struct {
AlonzoTransactionWitnessSet
PlutusV2Scripts []cbor.RawMessage `cbor:"6,keyasint,omitempty"`
Expand Down
8 changes: 8 additions & 0 deletions ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ func (o MaryTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
return o.OutputAmount.Assets
}

func (o MaryTransactionOutput) DatumHash() *Blake2b256 {
return nil
}

func (o MaryTransactionOutput) Datum() *cbor.LazyValue {
return nil
}

type MaryTransactionOutputValue struct {
cbor.StructAsArray
Amount uint64
Expand Down
8 changes: 8 additions & 0 deletions ledger/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,14 @@ func (o ShelleyTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
return nil
}

func (o ShelleyTransactionOutput) DatumHash() *Blake2b256 {
return nil
}

func (o ShelleyTransactionOutput) Datum() *cbor.LazyValue {
return nil
}

type ShelleyTransactionWitnessSet struct {
VkeyWitnesses []interface{} `cbor:"0,keyasint,omitempty"`
MultisigScripts []interface{} `cbor:"1,keyasint,omitempty"`
Expand Down
2 changes: 2 additions & 0 deletions ledger/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ type TransactionOutput interface {
Address() Address
Amount() uint64
Assets() *MultiAsset[MultiAssetTypeOutput]
Datum() *cbor.LazyValue
DatumHash() *Blake2b256
}

func NewTransactionFromCbor(txType uint, data []byte) (interface{}, error) {
Expand Down