Skip to content

Commit 9bd865d

Browse files
committed
fix: use Shelley address network ID for Byron addresses
Signed-off-by: Aurora Gaffney <[email protected]>
1 parent 498c233 commit 9bd865d

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

ledger/common/address.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,10 +273,13 @@ func (a *Address) MarshalCBOR() ([]byte, error) {
273273

274274
func (a Address) NetworkId() uint {
275275
if a.addressType == AddressTypeByron {
276+
// Use Shelley network ID convention
276277
if a.byronAddressAttr.Network == nil {
278+
// Return mainnet if no network ID is present in address
277279
return AddressNetworkMainnet
278280
}
279-
return uint(*a.byronAddressAttr.Network)
281+
// Return testnet, since the convention says we only include network ID on testnets
282+
return AddressNetworkTestnet
280283
} else {
281284
return uint(a.networkId)
282285
}

ledger/common/address_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,36 @@ func TestAddressPaymentAddress_MixedCase(t *testing.T) {
291291
assert.Nil(t, err, "Expected no error when decoding a mixed-case address")
292292
assert.NotNil(t, addr, "Expected a valid address object after decoding")
293293
}
294+
295+
func TestAddressNetworkId(t *testing.T) {
296+
testDefs := []struct {
297+
address string
298+
expectedNetworkId uint
299+
}{
300+
{
301+
address: "addr_test1wpvdxve27gk4ylwyf7t6xn3c7swrfzwz9uv0akwnpctkc4qdhgye0",
302+
expectedNetworkId: AddressNetworkTestnet,
303+
},
304+
{
305+
address: "FHnt4NL7yPXsabNmoHMHCfzUVkAC1vSZKd3fgyPfvRGhoXdum5oadfcrADWF8Fc",
306+
expectedNetworkId: AddressNetworkTestnet,
307+
},
308+
{
309+
address: "addr1q862w5ru0hpxl4r6vezgtegrfqve0dm2dp3yj2f7y4arrf223wd3fr6qcumc6873am478xnxmfp8lgpe6q6ju9ttjgns2xavze",
310+
expectedNetworkId: AddressNetworkMainnet,
311+
},
312+
}
313+
for _, testDef := range testDefs {
314+
addr, err := NewAddress(testDef.address)
315+
if err != nil {
316+
t.Fatalf("failed to decode address: %s", err)
317+
}
318+
if addr.NetworkId() != testDef.expectedNetworkId {
319+
t.Fatalf(
320+
"address did not return expected network ID, got: %d, wanted: %d",
321+
addr.NetworkId(),
322+
testDef.expectedNetworkId,
323+
)
324+
}
325+
}
326+
}

0 commit comments

Comments
 (0)