fix(types): track decoded address prefixes
Co-Authored-By: Charon <charon@lethean.io>
This commit is contained in:
parent
474fa2f07d
commit
602c886400
3 changed files with 32 additions and 5 deletions
|
|
@ -31,6 +31,9 @@ type Address struct {
|
||||||
SpendPublicKey PublicKey
|
SpendPublicKey PublicKey
|
||||||
ViewPublicKey PublicKey
|
ViewPublicKey PublicKey
|
||||||
Flags uint8
|
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.
|
// 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
|
// IsIntegrated reports whether the given prefix corresponds to an integrated
|
||||||
// address type (standard integrated or auditable integrated).
|
// address type (standard integrated or auditable integrated).
|
||||||
func (a *Address) IsIntegrated() bool {
|
func (a *Address) IsIntegrated() bool {
|
||||||
// This method checks whether the address was decoded with an integrated
|
return IsIntegratedPrefix(a.Prefix)
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsIntegratedPrefix reports whether the given prefix corresponds to an
|
// 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.SpendPublicKey[:], remaining[0:32])
|
||||||
copy(addr.ViewPublicKey[:], remaining[32:64])
|
copy(addr.ViewPublicKey[:], remaining[32:64])
|
||||||
addr.Flags = remaining[64]
|
addr.Flags = remaining[64]
|
||||||
|
addr.Prefix = prefix
|
||||||
|
|
||||||
return addr, prefix, nil
|
return addr, prefix, nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,6 +65,10 @@ func TestAddressEncodeDecodeRoundTrip_Good(t *testing.T) {
|
||||||
if decoded.Flags != original.Flags {
|
if decoded.Flags != original.Flags {
|
||||||
t.Errorf("Flags mismatch: got 0x%02x, want 0x%02x", 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) {
|
func TestDecodeAddress_Bad(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
|
|
||||||
store "dappco.re/go/core/store"
|
store "dappco.re/go/core/store"
|
||||||
|
|
||||||
|
"dappco.re/go/core/blockchain/config"
|
||||||
"dappco.re/go/core/blockchain/crypto"
|
"dappco.re/go/core/blockchain/crypto"
|
||||||
"dappco.re/go/core/blockchain/types"
|
"dappco.re/go/core/blockchain/types"
|
||||||
)
|
)
|
||||||
|
|
@ -107,6 +108,7 @@ func (a *Account) Address() types.Address {
|
||||||
return types.Address{
|
return types.Address{
|
||||||
SpendPublicKey: a.SpendPublicKey,
|
SpendPublicKey: a.SpendPublicKey,
|
||||||
ViewPublicKey: a.ViewPublicKey,
|
ViewPublicKey: a.ViewPublicKey,
|
||||||
|
Prefix: config.AddressPrefix,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue