Skip to content

Commit 67ef861

Browse files
authored
Merge pull request #507 from blinklabs-io/feat/ledger-utxorpc
feat: ledger functions for utxorpc support
2 parents 3eeb1dd + b33e309 commit 67ef861

File tree

11 files changed

+258
-3
lines changed

11 files changed

+258
-3
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@ go 1.20
55
require (
66
github.com/fxamacker/cbor/v2 v2.6.0
77
github.com/jinzhu/copier v0.4.0
8+
github.com/utxorpc/go-codegen v0.4.0
89
golang.org/x/crypto v0.19.0
910
)
1011

1112
require (
1213
github.com/x448/float16 v0.8.4 // indirect
1314
golang.org/x/sys v0.17.0 // indirect
15+
google.golang.org/protobuf v1.32.0 // indirect
1416
)

go.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
github.com/fxamacker/cbor/v2 v2.6.0 h1:sU6J2usfADwWlYDAFhZBQ6TnLFBHxgesMrQfQgk1tWA=
22
github.com/fxamacker/cbor/v2 v2.6.0/go.mod h1:pxXPTn3joSm21Gbwsv0w9OSA2y1HFR9qXEeXQVeNoDQ=
3+
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
34
github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8=
45
github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg=
6+
github.com/utxorpc/go-codegen v0.4.0 h1:9YzCURt5Jrnwii8VmQYeKH6K/wYXMQx+DmdjzieSZpM=
7+
github.com/utxorpc/go-codegen v0.4.0/go.mod h1:rFk8LI55uFqnwftPYTTklTKB8Lhwh4K+ltb2BtrFe3g=
58
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
69
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
710
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
811
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
912
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
1013
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
14+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
15+
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
16+
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=

ledger/allegra.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package ledger
1616

1717
import (
18+
"encoding/hex"
1819
"fmt"
1920

21+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
22+
2023
"github.com/blinklabs-io/gouroboros/cbor"
2124
)
2225

@@ -80,6 +83,25 @@ func (b *AllegraBlock) Transactions() []Transaction {
8083
return ret
8184
}
8285

86+
func (b *AllegraBlock) Utxorpc() *utxorpc.Block {
87+
var block *utxorpc.Block
88+
var body *utxorpc.BlockBody
89+
var header *utxorpc.BlockHeader
90+
var txs []*utxorpc.Tx
91+
header.Slot = b.SlotNumber()
92+
tmpHash, _ := hex.DecodeString(b.Hash())
93+
header.Hash = tmpHash
94+
header.Height = b.BlockNumber()
95+
for _, t := range b.Transactions() {
96+
tx := t.Utxorpc()
97+
txs = append(txs, tx)
98+
}
99+
body.Tx = txs
100+
block.Body = body
101+
block.Header = header
102+
return block
103+
}
104+
83105
type AllegraBlockHeader struct {
84106
ShelleyBlockHeader
85107
}
@@ -129,6 +151,10 @@ func (t AllegraTransaction) Metadata() *cbor.Value {
129151
return t.TxMetadata
130152
}
131153

154+
func (t AllegraTransaction) Utxorpc() *utxorpc.Tx {
155+
return t.Body.Utxorpc()
156+
}
157+
132158
func (t *AllegraTransaction) Cbor() []byte {
133159
// Return stored CBOR if we have any
134160
cborData := t.DecodeStoreCbor.Cbor()

ledger/alonzo.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
package ledger
1616

1717
import (
18+
"encoding/hex"
1819
"encoding/json"
1920
"fmt"
2021

22+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
23+
2124
"github.com/blinklabs-io/gouroboros/cbor"
2225
)
2326

@@ -90,6 +93,25 @@ func (b *AlonzoBlock) Transactions() []Transaction {
9093
return ret
9194
}
9295

96+
func (b *AlonzoBlock) Utxorpc() *utxorpc.Block {
97+
var block *utxorpc.Block
98+
var body *utxorpc.BlockBody
99+
var header *utxorpc.BlockHeader
100+
var txs []*utxorpc.Tx
101+
header.Slot = b.SlotNumber()
102+
tmpHash, _ := hex.DecodeString(b.Hash())
103+
header.Hash = tmpHash
104+
header.Height = b.BlockNumber()
105+
for _, t := range b.Transactions() {
106+
tx := t.Utxorpc()
107+
txs = append(txs, tx)
108+
}
109+
body.Tx = txs
110+
block.Body = body
111+
block.Header = header
112+
return block
113+
}
114+
93115
type AlonzoBlockHeader struct {
94116
ShelleyBlockHeader
95117
}
@@ -180,6 +202,15 @@ func (o AlonzoTransactionOutput) Datum() *cbor.LazyValue {
180202
return nil
181203
}
182204

205+
func (o AlonzoTransactionOutput) Utxorpc() *utxorpc.TxOutput {
206+
return &utxorpc.TxOutput{
207+
Address: o.OutputAddress.Bytes(),
208+
Coin: o.Amount(),
209+
// Assets: o.Assets,
210+
DatumHash: o.TxOutputDatumHash.Bytes(),
211+
}
212+
}
213+
183214
type AlonzoTransactionWitnessSet struct {
184215
ShelleyTransactionWitnessSet
185216
PlutusScripts []cbor.RawMessage `cbor:"3,keyasint,omitempty"`
@@ -251,6 +282,10 @@ func (t *AlonzoTransaction) Cbor() []byte {
251282
return cborData
252283
}
253284

285+
func (t *AlonzoTransaction) Utxorpc() *utxorpc.Tx {
286+
return t.Body.Utxorpc()
287+
}
288+
254289
func NewAlonzoBlockFromCbor(data []byte) (*AlonzoBlock, error) {
255290
var alonzoBlock AlonzoBlock
256291
if _, err := cbor.Decode(data, &alonzoBlock); err != nil {

ledger/babbage.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
package ledger
1616

1717
import (
18+
"encoding/hex"
1819
"encoding/json"
1920
"fmt"
2021

22+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
23+
2124
"github.com/blinklabs-io/gouroboros/cbor"
2225
)
2326

@@ -90,6 +93,25 @@ func (b *BabbageBlock) Transactions() []Transaction {
9093
return ret
9194
}
9295

96+
func (b *BabbageBlock) Utxorpc() *utxorpc.Block {
97+
var block *utxorpc.Block
98+
var body *utxorpc.BlockBody
99+
var header *utxorpc.BlockHeader
100+
var txs []*utxorpc.Tx
101+
header.Slot = b.SlotNumber()
102+
tmpHash, _ := hex.DecodeString(b.Hash())
103+
header.Hash = tmpHash
104+
header.Height = b.BlockNumber()
105+
for _, t := range b.Transactions() {
106+
tx := t.Utxorpc()
107+
txs = append(txs, tx)
108+
}
109+
body.Tx = txs
110+
block.Body = body
111+
block.Header = header
112+
return block
113+
}
114+
93115
type BabbageBlockHeader struct {
94116
cbor.StructAsArray
95117
cbor.DecodeStoreCbor
@@ -304,6 +326,17 @@ func (o BabbageTransactionOutput) Datum() *cbor.LazyValue {
304326
return nil
305327
}
306328

329+
func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput {
330+
return &utxorpc.TxOutput{
331+
Address: o.OutputAddress.Bytes(),
332+
Coin: o.Amount(),
333+
// Assets: o.Assets(),
334+
// Datum: o.Datum(),
335+
DatumHash: o.DatumHash().Bytes(),
336+
// Script: o.ScriptRef,
337+
}
338+
}
339+
307340
type BabbageTransactionWitnessSet struct {
308341
AlonzoTransactionWitnessSet
309342
PlutusV2Scripts []cbor.RawMessage `cbor:"6,keyasint,omitempty"`
@@ -373,6 +406,10 @@ func (t *BabbageTransaction) Cbor() []byte {
373406
return cborData
374407
}
375408

409+
func (t *BabbageTransaction) Utxorpc() *utxorpc.Tx {
410+
return t.Body.Utxorpc()
411+
}
412+
376413
func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) {
377414
var babbageBlock BabbageBlock
378415
if _, err := cbor.Decode(data, &babbageBlock); err != nil {

ledger/block.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ import (
1818
"encoding/hex"
1919
"fmt"
2020

21+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2122
"golang.org/x/crypto/blake2b"
2223
)
2324

2425
type Block interface {
2526
BlockHeader
2627
Transactions() []Transaction
28+
Utxorpc() *utxorpc.Block
2729
}
2830

2931
type BlockHeader interface {

ledger/byron.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ package ledger
1717
import (
1818
"fmt"
1919

20+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
21+
2022
"github.com/blinklabs-io/gouroboros/cbor"
2123
)
2224

@@ -155,6 +157,10 @@ func (t *ByronTransaction) Metadata() *cbor.Value {
155157
return t.Attributes
156158
}
157159

160+
func (t *ByronTransaction) Utxorpc() *utxorpc.Tx {
161+
return &utxorpc.Tx{}
162+
}
163+
158164
type ByronMainBlockBody struct {
159165
cbor.StructAsArray
160166
// TODO: split this to its own type
@@ -268,6 +274,10 @@ func (b *ByronMainBlock) Transactions() []Transaction {
268274
return nil
269275
}
270276

277+
func (b *ByronMainBlock) Utxorpc() *utxorpc.Block {
278+
return &utxorpc.Block{}
279+
}
280+
271281
type ByronEpochBoundaryBlock struct {
272282
cbor.StructAsArray
273283
cbor.DecodeStoreCbor
@@ -309,6 +319,10 @@ func (b *ByronEpochBoundaryBlock) Transactions() []Transaction {
309319
return nil
310320
}
311321

322+
func (b *ByronEpochBoundaryBlock) Utxorpc() *utxorpc.Block {
323+
return &utxorpc.Block{}
324+
}
325+
312326
func NewByronEpochBoundaryBlockFromCbor(
313327
data []byte,
314328
) (*ByronEpochBoundaryBlock, error) {

ledger/conway.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
package ledger
1616

1717
import (
18+
"encoding/hex"
1819
"fmt"
1920

21+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
22+
2023
"github.com/blinklabs-io/gouroboros/cbor"
2124
)
2225

@@ -89,6 +92,25 @@ func (b *ConwayBlock) Transactions() []Transaction {
8992
return ret
9093
}
9194

95+
func (b *ConwayBlock) Utxorpc() *utxorpc.Block {
96+
var block *utxorpc.Block
97+
var body *utxorpc.BlockBody
98+
var header *utxorpc.BlockHeader
99+
var txs []*utxorpc.Tx
100+
header.Slot = b.SlotNumber()
101+
tmpHash, _ := hex.DecodeString(b.Hash())
102+
header.Hash = tmpHash
103+
header.Height = b.BlockNumber()
104+
for _, t := range b.Transactions() {
105+
tx := t.Utxorpc()
106+
txs = append(txs, tx)
107+
}
108+
body.Tx = txs
109+
block.Body = body
110+
block.Header = header
111+
return block
112+
}
113+
92114
type ConwayBlockHeader struct {
93115
BabbageBlockHeader
94116
}
@@ -169,6 +191,10 @@ func (t *ConwayTransaction) Cbor() []byte {
169191
return cborData
170192
}
171193

194+
func (t *ConwayTransaction) Utxorpc() *utxorpc.Tx {
195+
return t.Body.Utxorpc()
196+
}
197+
172198
func NewConwayBlockFromCbor(data []byte) (*ConwayBlock, error) {
173199
var conwayBlock ConwayBlock
174200
if _, err := cbor.Decode(data, &conwayBlock); err != nil {

ledger/mary.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
package ledger
1616

1717
import (
18+
"encoding/hex"
1819
"encoding/json"
1920
"fmt"
2021

22+
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
23+
2124
"github.com/blinklabs-io/gouroboros/cbor"
2225
)
2326

@@ -81,6 +84,25 @@ func (b *MaryBlock) Transactions() []Transaction {
8184
return ret
8285
}
8386

87+
func (b *MaryBlock) Utxorpc() *utxorpc.Block {
88+
var block *utxorpc.Block
89+
var body *utxorpc.BlockBody
90+
var header *utxorpc.BlockHeader
91+
var txs []*utxorpc.Tx
92+
header.Slot = b.SlotNumber()
93+
tmpHash, _ := hex.DecodeString(b.Hash())
94+
header.Hash = tmpHash
95+
header.Height = b.BlockNumber()
96+
for _, t := range b.Transactions() {
97+
tx := t.Utxorpc()
98+
txs = append(txs, tx)
99+
}
100+
body.Tx = txs
101+
block.Body = body
102+
block.Header = header
103+
return block
104+
}
105+
84106
type MaryBlockHeader struct {
85107
ShelleyBlockHeader
86108
}
@@ -166,6 +188,10 @@ func (t *MaryTransaction) Cbor() []byte {
166188
return cborData
167189
}
168190

191+
func (t *MaryTransaction) Utxorpc() *utxorpc.Tx {
192+
return t.Body.Utxorpc()
193+
}
194+
169195
type MaryTransactionOutput struct {
170196
cbor.StructAsArray
171197
cbor.DecodeStoreCbor
@@ -206,6 +232,14 @@ func (o MaryTransactionOutput) Datum() *cbor.LazyValue {
206232
return nil
207233
}
208234

235+
func (o MaryTransactionOutput) Utxorpc() *utxorpc.TxOutput {
236+
return &utxorpc.TxOutput{
237+
Address: o.OutputAddress.Bytes(),
238+
Coin: o.Amount(),
239+
// Assets: o.Assets,
240+
}
241+
}
242+
209243
type MaryTransactionOutputValue struct {
210244
cbor.StructAsArray
211245
Amount uint64

0 commit comments

Comments
 (0)