Skip to content

Commit 3cb9204

Browse files
authored
chore: move address and common types to sub-package (#689)
This is in preparation for further refactoring. Additionally, it: * adds compatability types/functions to keep existing code working * adds NewMultiAsset function to create MultiAsset from existing data * switches to public interface for iterating over MultiAsset in various places
1 parent b7595f9 commit 3cb9204

16 files changed

+149
-73
lines changed

ledger/allegra.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2222

2323
"github.com/blinklabs-io/gouroboros/cbor"
24+
"github.com/blinklabs-io/gouroboros/ledger/common"
2425
)
2526

2627
const (
@@ -202,7 +203,7 @@ func (t AllegraTransaction) RequiredSigners() []Blake2b224 {
202203
return t.Body.RequiredSigners()
203204
}
204205

205-
func (t AllegraTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
206+
func (t AllegraTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
206207
return t.Body.AssetMint()
207208
}
208209

ledger/alonzo.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2323

2424
"github.com/blinklabs-io/gouroboros/cbor"
25+
"github.com/blinklabs-io/gouroboros/ledger/common"
2526
)
2627

2728
const (
@@ -209,10 +210,10 @@ func (o *AlonzoTransactionOutput) MarshalCBOR() ([]byte, error) {
209210

210211
func (o AlonzoTransactionOutput) MarshalJSON() ([]byte, error) {
211212
tmpObj := struct {
212-
Address Address `json:"address"`
213-
Amount uint64 `json:"amount"`
214-
Assets *MultiAsset[MultiAssetTypeOutput] `json:"assets,omitempty"`
215-
DatumHash string `json:"datumHash,omitempty"`
213+
Address Address `json:"address"`
214+
Amount uint64 `json:"amount"`
215+
Assets *common.MultiAsset[common.MultiAssetTypeOutput] `json:"assets,omitempty"`
216+
DatumHash string `json:"datumHash,omitempty"`
216217
}{
217218
Address: o.OutputAddress,
218219
Amount: o.OutputAmount.Amount,
@@ -232,7 +233,7 @@ func (o AlonzoTransactionOutput) Amount() uint64 {
232233
return o.OutputAmount.Amount
233234
}
234235

235-
func (o AlonzoTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
236+
func (o AlonzoTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTypeOutput] {
236237
return o.OutputAmount.Assets
237238
}
238239

@@ -247,13 +248,15 @@ func (o AlonzoTransactionOutput) Datum() *cbor.LazyValue {
247248
func (o AlonzoTransactionOutput) Utxorpc() *utxorpc.TxOutput {
248249
var assets []*utxorpc.Multiasset
249250
if o.Assets() != nil {
250-
for policyId, policyData := range o.Assets().data {
251+
tmpAssets := o.Assets()
252+
for _, policyId := range tmpAssets.Policies() {
251253
var ma = &utxorpc.Multiasset{
252254
PolicyId: policyId.Bytes(),
253255
}
254-
for assetName, amount := range policyData {
256+
for _, assetName := range tmpAssets.Assets(policyId) {
257+
amount := tmpAssets.Asset(policyId, assetName)
255258
asset := &utxorpc.Asset{
256-
Name: assetName.Bytes(),
259+
Name: assetName,
257260
OutputCoin: amount,
258261
}
259262
ma.Assets = append(ma.Assets, asset)
@@ -366,7 +369,7 @@ func (t AlonzoTransaction) RequiredSigners() []Blake2b224 {
366369
return t.Body.RequiredSigners()
367370
}
368371

369-
func (t AlonzoTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
372+
func (t AlonzoTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
370373
return t.Body.AssetMint()
371374
}
372375

ledger/babbage.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2323

2424
"github.com/blinklabs-io/gouroboros/cbor"
25+
"github.com/blinklabs-io/gouroboros/ledger/common"
2526
)
2627

2728
const (
@@ -363,11 +364,11 @@ func (o *BabbageTransactionOutput) MarshalCBOR() ([]byte, error) {
363364

364365
func (o BabbageTransactionOutput) MarshalJSON() ([]byte, error) {
365366
tmpObj := struct {
366-
Address Address `json:"address"`
367-
Amount uint64 `json:"amount"`
368-
Assets *MultiAsset[MultiAssetTypeOutput] `json:"assets,omitempty"`
369-
Datum *cbor.LazyValue `json:"datum,omitempty"`
370-
DatumHash string `json:"datumHash,omitempty"`
367+
Address Address `json:"address"`
368+
Amount uint64 `json:"amount"`
369+
Assets *common.MultiAsset[common.MultiAssetTypeOutput] `json:"assets,omitempty"`
370+
Datum *cbor.LazyValue `json:"datum,omitempty"`
371+
DatumHash string `json:"datumHash,omitempty"`
371372
}{
372373
Address: o.OutputAddress,
373374
Amount: o.OutputAmount.Amount,
@@ -392,7 +393,7 @@ func (o BabbageTransactionOutput) Amount() uint64 {
392393
return o.OutputAmount.Amount
393394
}
394395

395-
func (o BabbageTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
396+
func (o BabbageTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTypeOutput] {
396397
return o.OutputAmount.Assets
397398
}
398399

@@ -420,13 +421,15 @@ func (o BabbageTransactionOutput) Utxorpc() *utxorpc.TxOutput {
420421

421422
var assets []*utxorpc.Multiasset
422423
if o.Assets() != nil {
423-
for policyId, policyData := range o.Assets().data {
424+
tmpAssets := o.Assets()
425+
for _, policyId := range tmpAssets.Policies() {
424426
var ma = &utxorpc.Multiasset{
425427
PolicyId: policyId.Bytes(),
426428
}
427-
for assetName, amount := range policyData {
429+
for _, assetName := range tmpAssets.Assets(policyId) {
430+
amount := tmpAssets.Asset(policyId, assetName)
428431
asset := &utxorpc.Asset{
429-
Name: assetName.Bytes(),
432+
Name: assetName,
430433
OutputCoin: amount,
431434
}
432435
ma.Assets = append(ma.Assets, asset)
@@ -540,7 +543,7 @@ func (t BabbageTransaction) RequiredSigners() []Blake2b224 {
540543
return t.Body.RequiredSigners()
541544
}
542545

543-
func (t BabbageTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
546+
func (t BabbageTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
544547
return t.Body.AssetMint()
545548
}
546549

ledger/byron.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2222

2323
"github.com/blinklabs-io/gouroboros/cbor"
24+
"github.com/blinklabs-io/gouroboros/ledger/common"
2425
)
2526

2627
const (
@@ -216,7 +217,7 @@ func (t *ByronTransaction) RequiredSigners() []Blake2b224 {
216217
return nil
217218
}
218219

219-
func (t *ByronTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
220+
func (t *ByronTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
220221
// No asset mints in Byron
221222
return nil
222223
}
@@ -385,7 +386,7 @@ func (o ByronTransactionOutput) Amount() uint64 {
385386
return o.OutputAmount
386387
}
387388

388-
func (o ByronTransactionOutput) Assets() *MultiAsset[MultiAssetTypeOutput] {
389+
func (o ByronTransactionOutput) Assets() *common.MultiAsset[common.MultiAssetTypeOutput] {
389390
return nil
390391
}
391392

ledger/address.go renamed to ledger/common/address.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package ledger
15+
package common
1616

1717
import (
1818
"fmt"

ledger/address_test.go renamed to ledger/common/address_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package ledger
15+
package common
1616

1717
import (
1818
"testing"

ledger/common.go renamed to ledger/common/common.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 Blink Labs Software
1+
// Copyright 2024 Blink Labs Software
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package ledger
15+
package common
1616

1717
import (
1818
"encoding/hex"
@@ -82,6 +82,12 @@ type MultiAsset[T MultiAssetTypeOutput | MultiAssetTypeMint] struct {
8282
data map[Blake2b224]map[cbor.ByteString]T
8383
}
8484

85+
// NewMultiAsset creates a MultiAsset with the specified data
86+
func NewMultiAsset[T MultiAssetTypeOutput | MultiAssetTypeMint](data map[Blake2b224]map[cbor.ByteString]T) MultiAsset[T] {
87+
return MultiAsset[T]{data: data}
88+
}
89+
90+
// multiAssetJson is a convenience type for marshaling/unmarshaling MultiAsset to/from JSON
8591
type multiAssetJson[T MultiAssetTypeOutput | MultiAssetTypeMint] struct {
8692
Name string `json:"name"`
8793
NameHex string `json:"nameHex"`

ledger/common_test.go renamed to ledger/common/common_test.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
package ledger
1+
// Copyright 2024 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package common
216

317
import (
418
"encoding/hex"

ledger/compat.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2024 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package ledger
16+
17+
import (
18+
"github.com/blinklabs-io/gouroboros/ledger/common"
19+
)
20+
21+
// The below are compatability types and functions to keep existing code working
22+
// after a refactor of the ledger package
23+
24+
// Hash types
25+
type Blake2b224 = common.Blake2b224
26+
type Blake2b256 = common.Blake2b256
27+
28+
func NewBlake2b224(data []byte) Blake2b224 {
29+
return common.NewBlake2b224(data)
30+
}
31+
32+
func NewBlake2b256(data []byte) Blake2b256 {
33+
return common.NewBlake2b256(data)
34+
}
35+
36+
// Address
37+
type Address = common.Address
38+
type AddrKeyHash = common.AddrKeyHash
39+
40+
func NewAddress(addr string) (Address, error) {
41+
return common.NewAddress(addr)
42+
}
43+
44+
// Other types
45+
type IssuerVkey = common.IssuerVkey
46+
type PoolId = common.PoolId
47+
48+
func NewPoolIdFromBech32(poolId string) (PoolId, error) {
49+
return common.NewPoolIdFromBech32(poolId)
50+
}
51+
52+
type AssetFingerprint = common.AssetFingerprint
53+
54+
func NewAssetFingerprint(policyId []byte, assetName []byte) AssetFingerprint {
55+
return common.NewAssetFingerprint(policyId, assetName)
56+
}

ledger/conway.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
utxorpc "github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
2222

2323
"github.com/blinklabs-io/gouroboros/cbor"
24+
"github.com/blinklabs-io/gouroboros/ledger/common"
2425
)
2526

2627
const (
@@ -502,7 +503,7 @@ func (t ConwayTransaction) RequiredSigners() []Blake2b224 {
502503
return t.Body.RequiredSigners()
503504
}
504505

505-
func (t ConwayTransaction) AssetMint() *MultiAsset[MultiAssetTypeMint] {
506+
func (t ConwayTransaction) AssetMint() *common.MultiAsset[common.MultiAssetTypeMint] {
506507
return t.Body.AssetMint()
507508
}
508509

0 commit comments

Comments
 (0)