File tree Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Expand file tree Collapse file tree 2 files changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -273,10 +273,13 @@ func (a *Address) MarshalCBOR() ([]byte, error) {
273
273
274
274
func (a Address ) NetworkId () uint {
275
275
if a .addressType == AddressTypeByron {
276
+ // Use Shelley network ID convention
276
277
if a .byronAddressAttr .Network == nil {
278
+ // Return mainnet if no network ID is present in address
277
279
return AddressNetworkMainnet
278
280
}
279
- return uint (* a .byronAddressAttr .Network )
281
+ // Return testnet, since the convention says we only include network ID on testnets
282
+ return AddressNetworkTestnet
280
283
} else {
281
284
return uint (a .networkId )
282
285
}
Original file line number Diff line number Diff line change @@ -291,3 +291,36 @@ func TestAddressPaymentAddress_MixedCase(t *testing.T) {
291
291
assert .Nil (t , err , "Expected no error when decoding a mixed-case address" )
292
292
assert .NotNil (t , addr , "Expected a valid address object after decoding" )
293
293
}
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
+ }
You can’t perform that action at this time.
0 commit comments