From 602c886400b1533b49b2fb06946a24c5cb70b814 Mon Sep 17 00:00:00 2001 From: Virgil Date: Sat, 4 Apr 2026 22:46:12 +0000 Subject: [PATCH] fix(types): track decoded address prefixes Co-Authored-By: Charon --- types/address.go | 10 +++++----- types/address_test.go | 25 +++++++++++++++++++++++++ wallet/account.go | 2 ++ 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/types/address.go b/types/address.go index aa4feab..a608bc4 100644 --- a/types/address.go +++ b/types/address.go @@ -31,6 +31,9 @@ type Address struct { SpendPublicKey PublicKey ViewPublicKey PublicKey Flags uint8 + // Prefix records the original base58 prefix when an address was decoded. + // It is optional for manually constructed addresses. + Prefix uint64 `json:"-"` } // IsAuditable reports whether the address has the auditable flag set. @@ -41,11 +44,7 @@ func (a *Address) IsAuditable() bool { // IsIntegrated reports whether the given prefix corresponds to an integrated // address type (standard integrated or auditable integrated). func (a *Address) IsIntegrated() bool { - // This method checks whether the address was decoded with an integrated - // prefix. Since we do not store the prefix in the Address struct, callers - // should use the prefix returned by DecodeAddress to determine this. - // This helper exists for convenience when the prefix is not available. - return false + return IsIntegratedPrefix(a.Prefix) } // IsIntegratedPrefix reports whether the given prefix corresponds to an @@ -117,6 +116,7 @@ func DecodeAddress(s string) (*Address, uint64, error) { copy(addr.SpendPublicKey[:], remaining[0:32]) copy(addr.ViewPublicKey[:], remaining[32:64]) addr.Flags = remaining[64] + addr.Prefix = prefix return addr, prefix, nil } diff --git a/types/address_test.go b/types/address_test.go index df9b404..150def0 100644 --- a/types/address_test.go +++ b/types/address_test.go @@ -65,6 +65,10 @@ func TestAddressEncodeDecodeRoundTrip_Good(t *testing.T) { if decoded.Flags != original.Flags { t.Errorf("Flags mismatch: got 0x%02x, want 0x%02x", decoded.Flags, original.Flags) } + + if decoded.IsIntegrated() != IsIntegratedPrefix(tt.prefix) { + t.Errorf("IsIntegrated mismatch: got %v, want %v", decoded.IsIntegrated(), IsIntegratedPrefix(tt.prefix)) + } }) } } @@ -103,6 +107,27 @@ func TestIsIntegratedPrefix_Good(t *testing.T) { } } +func TestAddressIsIntegrated_Good(t *testing.T) { + decoded, prefix, err := DecodeAddress(makeTestAddress(0x00).Encode(config.IntegratedAddressPrefix)) + if err != nil { + t.Fatalf("DecodeAddress failed: %v", err) + } + if prefix != config.IntegratedAddressPrefix { + t.Fatalf("prefix mismatch: got 0x%x, want 0x%x", prefix, config.IntegratedAddressPrefix) + } + if !decoded.IsIntegrated() { + t.Fatal("decoded integrated address should report IsIntegrated() == true") + } + + standard, _, err := DecodeAddress(makeTestAddress(0x00).Encode(config.AddressPrefix)) + if err != nil { + t.Fatalf("DecodeAddress failed: %v", err) + } + if standard.IsIntegrated() { + t.Fatal("decoded standard address should report IsIntegrated() == false") + } +} + func TestDecodeAddress_Bad(t *testing.T) { tests := []struct { name string diff --git a/wallet/account.go b/wallet/account.go index 0dbfdfd..af5e791 100644 --- a/wallet/account.go +++ b/wallet/account.go @@ -23,6 +23,7 @@ import ( store "dappco.re/go/core/store" + "dappco.re/go/core/blockchain/config" "dappco.re/go/core/blockchain/crypto" "dappco.re/go/core/blockchain/types" ) @@ -107,6 +108,7 @@ func (a *Account) Address() types.Address { return types.Address{ SpendPublicKey: a.SpendPublicKey, ViewPublicKey: a.ViewPublicKey, + Prefix: config.AddressPrefix, } }