Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions packages/core-go/address/detect.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package address

// Detect identifies the AddressKind of a Stellar address string.
func Detect(addr string) (AddressKind, error) {
versionByte, _, err := DecodeStrKey(addr)
if err != nil {
return "", err
}

func Detect(address string) string {
if strkey.IsValidEd25519PublicKey(address) {
return "G"
switch versionByte {
case VersionByteG:
return KindG, nil
case VersionByteM:
return KindM, nil
case VersionByteC:
return KindC, nil
default:
return "", ErrUnknownVersionByteError
}
if strkey.IsValidMuxedAccountEd25519PublicKey(address) {
return "M"
}
if _, err := strkey.Decode(strkey.VersionByteContract, address); err == nil {
return "C"
}
return "invalid"
}
14 changes: 0 additions & 14 deletions packages/core-go/address/result.go

This file was deleted.

9 changes: 5 additions & 4 deletions packages/core-go/address/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ const (
// - For muxed addresses (KindM), BaseG is the underlying G-address and
// MuxedID is the 64-bit ID carried by the muxed account.
type Address struct {
Kind AddressKind
Raw string
BaseG string
MuxedID uint64
Kind AddressKind
Raw string
BaseG string
MuxedID uint64
Warnings []Warning
}

29 changes: 5 additions & 24 deletions packages/core-go/routing/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ func ExtractRouting(input RoutingInput) RoutingResult {
}
}

if parsed.Kind == "M" {
baseG, id, err := muxed.DecodeMuxed(parsed.Address)
if parsed.Kind == address.KindM {
baseG, id, err := muxed.DecodeMuxed(parsed.Raw)
if err != nil {
return RoutingResult{
RoutingSource: "none",
Expand Down Expand Up @@ -109,7 +109,7 @@ func ExtractRouting(input RoutingInput) RoutingResult {

return RoutingResult{
DestinationBaseAccount: baseG,
RoutingID: NewRoutingID(id),
RoutingID: NewRoutingID(strconv.FormatUint(id, 10)),
RoutingSource: "muxed",
Warnings: warnings,
}
Expand All @@ -134,21 +134,6 @@ func ExtractRouting(input RoutingInput) RoutingResult {
Severity: "warn",
Message: "MEMO_ID was empty, non-numeric, or exceeded uint64 max.",
})
} else {
normalized := strconv.FormatUint(val, 10)
if normalized != input.MemoValue {
warnings = append(warnings, address.Warning{
Code: address.WarnNonCanonicalRoutingID,
Severity: "warn",
Message: "Memo routing ID had leading zeros. Normalized to canonical decimal.",
Normalization: &address.Normalization{
Original: input.MemoValue,
Normalized: normalized,
},
})
}
routingID = &val
routingSource = "memo"
}
} else if input.MemoType == "text" && memoValue != "" {
norm := NormalizeMemoTextID(memoValue)
Expand Down Expand Up @@ -191,10 +176,6 @@ func ExtractRouting(input RoutingInput) RoutingResult {
}
}

func stringValue(s *string) string {
if s == nil {
return ""
}

return *s
func stringValue(s string) string {
return s
}
32 changes: 14 additions & 18 deletions packages/core-go/routing/extract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
},
expected: RoutingResult{
DestinationBaseAccount: testBaseG,
RoutingID: ptrUint64(100),
RoutingID: NewRoutingID("100"),
RoutingSource: "memo",
Warnings: []address.Warning{},
},
Expand All @@ -54,7 +54,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
},
expected: RoutingResult{
DestinationBaseAccount: testBaseG,
RoutingID: ptrUint64(0),
RoutingID: NewRoutingID("0"),
RoutingSource: "memo",
Warnings: []address.Warning{},
},
Expand All @@ -68,7 +68,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
},
expected: RoutingResult{
DestinationBaseAccount: testBaseG,
RoutingID: ptrUint64(18446744073709551615),
RoutingID: NewRoutingID("18446744073709551615"),
RoutingSource: "memo",
Warnings: []address.Warning{},
},
Expand Down Expand Up @@ -142,7 +142,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
},
expected: RoutingResult{
DestinationBaseAccount: testBaseG,
RoutingID: ptrUint64(7),
RoutingID: NewRoutingID("7"),
RoutingSource: "memo",
Warnings: []address.Warning{
{
Expand All @@ -166,7 +166,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
},
expected: RoutingResult{
DestinationBaseAccount: testBaseG,
RoutingID: ptrUint64(200),
RoutingID: NewRoutingID("200"),
RoutingSource: "memo",
Warnings: []address.Warning{},
},
Expand Down Expand Up @@ -198,7 +198,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
name: "memo-return",
input: RoutingInput{
Destination: testBaseG,
MemoType: "MEMO_RETURN",
MemoType: "return",
MemoValue: "also-not-a-routing-id",
},
expected: RoutingResult{
Expand Down Expand Up @@ -248,7 +248,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
},
expected: RoutingResult{
DestinationBaseAccount: testBaseG,
RoutingID: ptrUint64(9007199254740993),
RoutingID: NewRoutingID("9007199254740993"),
RoutingSource: "muxed",
Warnings: []address.Warning{},
},
Expand All @@ -262,7 +262,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
},
expected: RoutingResult{
DestinationBaseAccount: testBaseG,
RoutingID: ptrUint64(9007199254740993),
RoutingID: NewRoutingID("9007199254740993"),
RoutingSource: "muxed",
Warnings: []address.Warning{
{
Expand All @@ -282,7 +282,7 @@ func TestExtractRouting_RoutingMatrix(t *testing.T) {
},
expected: RoutingResult{
DestinationBaseAccount: testBaseG,
RoutingID: ptrUint64(9007199254740993),
RoutingID: NewRoutingID("9007199254740993"),
RoutingSource: "muxed",
Warnings: []address.Warning{
{
Expand Down Expand Up @@ -331,36 +331,32 @@ func TestExtractRouting_ContractSourceClearsRoutingState(t *testing.T) {
})
}

func ptrUint64(v uint64) *uint64 {
return &v
}

func assertRoutingResult(t *testing.T, got, want RoutingResult) {
t.Helper()

if got.DestinationBaseAccount != want.DestinationBaseAccount {
t.Errorf("DestinationBaseAccount = %v, want %v", got.DestinationBaseAccount, want.DestinationBaseAccount)
}
if !uint64PtrEqual(got.RoutingID, want.RoutingID) {
t.Errorf("RoutingID = %v, want %v", got.RoutingID, want.RoutingID)
if !routingIDEqual(got.RoutingID, want.RoutingID) {
t.Errorf("RoutingID = %v, want %v", got.RoutingID.String(), want.RoutingID.String())
}
if got.RoutingSource != want.RoutingSource {
t.Errorf("RoutingSource = %v, want %v", got.RoutingSource, want.RoutingSource)
}
if !reflect.DeepEqual(got.Warnings, want.Warnings) {
t.Errorf("Warnings = %#v, want %#v", got.Warnings, want.Warnings)
t.Errorf("Warnings = %#+v, want %#+v", got.Warnings, want.Warnings)
}
if !reflect.DeepEqual(got.DestinationError, want.DestinationError) {
t.Errorf("DestinationError = %#v, want %#v", got.DestinationError, want.DestinationError)
}
}

func uint64PtrEqual(a, b *uint64) bool {
func routingIDEqual(a, b *RoutingID) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
return *a == *b
return a.String() == b.String()
}
16 changes: 1 addition & 15 deletions packages/core-go/routing/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"encoding/json"
"fmt"
"strconv"

"github.com/stellar-address-kit/core-go/address"
)

type MemoType string
Expand All @@ -18,6 +16,7 @@ const (
MemoTypeReturn MemoType = "return"
)

// RoutingID is a wrapper around a numeric string representing a 64-bit unsigned integer.
type RoutingID struct {
raw string
}
Expand Down Expand Up @@ -67,16 +66,3 @@ func (r *RoutingID) Uint64() (uint64, error) {
func NewRoutingID(s string) *RoutingID {
return &RoutingID{raw: s}
}

type RoutingResult struct {
DestinationBaseAccount string
RoutingID *RoutingID
RoutingSource string // "muxed" | "memo" | "none"
Warnings []address.Warning
DestinationError *DestinationError
}

type DestinationError struct {
Code address.ErrorCode
Message string
}
2 changes: 1 addition & 1 deletion packages/core-go/routing/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type RoutingResult struct {

// Backward-compatible fields used by current extraction flow.
DestinationBaseAccount string `json:"destinationBaseAccount,omitempty"`
RoutingID *uint64 `json:"routingId,omitempty"`
RoutingID *RoutingID `json:"routingId,omitempty"`
RoutingSource string `json:"routingSource,omitempty"`
Warnings []address.Warning `json:"warnings,omitempty"`
DestinationError *DestinationError `json:"destinationError,omitempty"`
Expand Down
Loading