Skip to content

Commit bb035cf

Browse files
committed
multi: move address marshal/unmarshal functions
To resolve another circular dependency issue in an upcoming commit, this time between the address and taprpc package, we move two more functions to another place where they fit better. In general, any RPC package should _not_ have a dependency into other packages, so this was wrong in the first place.
1 parent 15e7552 commit bb035cf

File tree

3 files changed

+40
-41
lines changed

3 files changed

+40
-41
lines changed

address/address.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/lightninglabs/taproot-assets/asset"
1818
"github.com/lightninglabs/taproot-assets/commitment"
1919
"github.com/lightninglabs/taproot-assets/fn"
20+
"github.com/lightninglabs/taproot-assets/taprpc"
2021
"github.com/lightningnetwork/lnd/tlv"
2122
)
2223

@@ -536,3 +537,40 @@ func DecodeAddress(addr string, net *ChainParams) (*Tap, error) {
536537

537538
return &a, nil
538539
}
540+
541+
// UnmarshalVersion parses an address version from the RPC variant.
542+
func UnmarshalVersion(version taprpc.AddrVersion) (Version, error) {
543+
// For now, we'll only support two address versions. The ones in the
544+
// future should be reserved for future use, so we disallow unknown
545+
// versions.
546+
switch version {
547+
case taprpc.AddrVersion_ADDR_VERSION_UNSPECIFIED:
548+
return V1, nil
549+
550+
case taprpc.AddrVersion_ADDR_VERSION_V0:
551+
return V0, nil
552+
553+
case taprpc.AddrVersion_ADDR_VERSION_V1:
554+
return V1, nil
555+
556+
default:
557+
return 0, fmt.Errorf("unknown address version: %v", version)
558+
}
559+
}
560+
561+
// MarshalVersion marshals the native address version into the RPC variant.
562+
func MarshalVersion(version Version) (taprpc.AddrVersion, error) {
563+
// For now, we'll only support two address versions. The ones in the
564+
// future should be reserved for future use, so we disallow unknown
565+
// versions.
566+
switch version {
567+
case V0:
568+
return taprpc.AddrVersion_ADDR_VERSION_V0, nil
569+
570+
case V1:
571+
return taprpc.AddrVersion_ADDR_VERSION_V1, nil
572+
573+
default:
574+
return 0, fmt.Errorf("unknown address version: %v", version)
575+
}
576+
}

rpcserver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,7 @@ func (r *rpcServer) NewAddr(ctx context.Context,
14611461
return nil, err
14621462
}
14631463

1464-
addrVersion, err := taprpc.UnmarshalAddressVersion(req.AddressVersion)
1464+
addrVersion, err := address.UnmarshalVersion(req.AddressVersion)
14651465
if err != nil {
14661466
return nil, err
14671467
}
@@ -3027,7 +3027,7 @@ func marshalAddr(addr *address.Tap,
30273027
return nil, err
30283028
}
30293029

3030-
addrVersion, err := taprpc.MarshalAddressVersion(addr.Version)
3030+
addrVersion, err := address.MarshalVersion(addr.Version)
30313031
if err != nil {
30323032
return nil, err
30333033
}

taprpc/marshal.go

Lines changed: 0 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111
"github.com/btcsuite/btcd/btcec/v2/schnorr"
1212
"github.com/btcsuite/btcd/txscript"
1313
"github.com/btcsuite/btcd/wire"
14-
"github.com/lightninglabs/taproot-assets/address"
1514
"github.com/lightninglabs/taproot-assets/asset"
1615
"github.com/lightninglabs/taproot-assets/commitment"
1716
"github.com/lightninglabs/taproot-assets/fn"
@@ -165,44 +164,6 @@ func MarshalAssetVersion(version asset.Version) (AssetVersion, error) {
165164
}
166165
}
167166

168-
// UnmarshalAddressVerion parses an address version from the RPC variant.
169-
func UnmarshalAddressVersion(version AddrVersion) (address.Version, error) {
170-
// For now we'll only support two address versions. The ones in the
171-
// future should be reserved for future use, so we disallow unknown
172-
// versions.
173-
switch version {
174-
case AddrVersion_ADDR_VERSION_UNSPECIFIED:
175-
return address.V1, nil
176-
177-
case AddrVersion_ADDR_VERSION_V0:
178-
return address.V0, nil
179-
180-
case AddrVersion_ADDR_VERSION_V1:
181-
return address.V1, nil
182-
183-
default:
184-
return 0, fmt.Errorf("unknown address version: %v", version)
185-
}
186-
}
187-
188-
// MarshalAddressVerion marshals the native address version into the RPC
189-
// variant.
190-
func MarshalAddressVersion(version address.Version) (AddrVersion, error) {
191-
// For now we'll only support two address versions. The ones in the
192-
// future should be reserved for future use, so we disallow unknown
193-
// versions.
194-
switch version {
195-
case address.V0:
196-
return AddrVersion_ADDR_VERSION_V0, nil
197-
198-
case address.V1:
199-
return AddrVersion_ADDR_VERSION_V1, nil
200-
201-
default:
202-
return 0, fmt.Errorf("unknown address version: %v", version)
203-
}
204-
}
205-
206167
// MarshalGenesisInfo marshals the native asset genesis into the RPC
207168
// counterpart.
208169
func MarshalGenesisInfo(gen *asset.Genesis, assetType asset.Type) *GenesisInfo {

0 commit comments

Comments
 (0)