Skip to content

Commit

Permalink
add testnetOnly field into address struct
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhar-petukhov committed Nov 23, 2023
1 parent 0cc0e19 commit 0b2fa5e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
3 changes: 2 additions & 1 deletion account.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,11 @@ func (p *addressParser) ParseAddress(ctx context.Context, address string) (ton.A
if len(bytesAddress) == 36 {
checksum := uint64(binary.BigEndian.Uint16(bytesAddress[34:36]))
if checksum == crc.CalculateCRC(crc.XMODEM, bytesAddress[0:34]) {
testnetOnly := bytesAddress[0]&0b10000000 != 0
bounce := bytesAddress[0]&0x11 == 0x11
accountID.Workchain = int32(int8(bytesAddress[1]))
copy(accountID.Address[:], bytesAddress[2:34])
return ton.Address{ID: accountID, Bounce: bounce, StateInit: nil}, nil
return ton.Address{ID: accountID, Bounce: bounce, StateInit: nil, TestnetOnly: testnetOnly}, nil
}
}
if !strings.Contains(address, ".") {
Expand Down
63 changes: 40 additions & 23 deletions address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,52 @@ func TestParseAddress(t *testing.T) {
parseToHumanAddress = iota
parseToRawAddress
parseDnsToRawAddress
parseTestnetOnlyAddress
)

type testCase struct {
name string
typeParse int
request string
response string
name string
typeParse int
request string
response string
responseTestnetOnly bool
}

for _, test := range []testCase{
{
name: "Parse to raw address",
typeParse: parseToHumanAddress,
request: "0:91d73056e035232f09aaf8242a1d51eea98b6a5bebbf8ac0c9e521d02a1a4bdb",
response: "EQCR1zBW4DUjLwmq-CQqHVHuqYtqW-u_isDJ5SHQKhpL2wQV",
name: "Parse to raw address",
typeParse: parseToHumanAddress,
request: "0:91d73056e035232f09aaf8242a1d51eea98b6a5bebbf8ac0c9e521d02a1a4bdb",
response: "EQCR1zBW4DUjLwmq-CQqHVHuqYtqW-u_isDJ5SHQKhpL2wQV",
responseTestnetOnly: false,
},
{
name: "Parse to human address",
typeParse: parseToRawAddress,
request: "EQCR1zBW4DUjLwmq-CQqHVHuqYtqW-u_isDJ5SHQKhpL2wQV",
response: "0:91d73056e035232f09aaf8242a1d51eea98b6a5bebbf8ac0c9e521d02a1a4bdb",
name: "Parse to human address",
typeParse: parseToRawAddress,
request: "EQCR1zBW4DUjLwmq-CQqHVHuqYtqW-u_isDJ5SHQKhpL2wQV",
response: "0:91d73056e035232f09aaf8242a1d51eea98b6a5bebbf8ac0c9e521d02a1a4bdb",
responseTestnetOnly: false,
},
{
name: "Parse dns to raw address",
typeParse: parseDnsToRawAddress,
request: "blackpepper.ton",
response: "0:44556b55c15052eb44c6b75a9eccbc6280d32d598d12e975f435195795bb11d5",
name: "Parse dns to raw address",
typeParse: parseDnsToRawAddress,
request: "blackpepper.ton",
response: "0:44556b55c15052eb44c6b75a9eccbc6280d32d598d12e975f435195795bb11d5",
responseTestnetOnly: false,
},
{
name: "Parse dns to raw address",
typeParse: parseDnsToRawAddress,
request: "subbotin.ton",
response: "0:2cf3b5b8c891e517c9addbda1c0386a09ccacbb0e3faf630b51cfc8152325acb",
name: "Parse dns to raw address",
typeParse: parseDnsToRawAddress,
request: "subbotin.ton",
response: "0:2cf3b5b8c891e517c9addbda1c0386a09ccacbb0e3faf630b51cfc8152325acb",
responseTestnetOnly: false,
},
{
name: "Parse only testnet",
typeParse: parseTestnetOnlyAddress,
request: "kQCR1zBW4DUjLwmq-CQqHVHuqYtqW-u_isDJ5SHQKhpL27-f",
response: "0:91d73056e035232f09aaf8242a1d51eea98b6a5bebbf8ac0c9e521d02a1a4bdb",
responseTestnetOnly: true,
},
} {
t.Run(test.name, func(t *testing.T) {
Expand All @@ -54,15 +67,19 @@ func TestParseAddress(t *testing.T) {
}
switch test.typeParse {
case parseToHumanAddress:
if account.ID.ToHuman(true, false) != test.response {
if account.ID.ToHuman(true, false) != test.response && account.TestnetOnly != test.responseTestnetOnly {
t.Fatalf("not equal address")
}
case parseToRawAddress:
if account.ID.ToRaw() != test.response {
if account.ID.ToRaw() != test.response && account.TestnetOnly != test.responseTestnetOnly {
t.Fatalf("not equal address")
}
case parseDnsToRawAddress:
if account.ID.ToRaw() != test.response {
if account.ID.ToRaw() != test.response && account.TestnetOnly != test.responseTestnetOnly {
t.Fatalf("not equal address")
}
case parseTestnetOnlyAddress:
if account.ID.ToRaw() != test.response && account.TestnetOnly != test.responseTestnetOnly {
t.Fatalf("not equal address")
}
}
Expand Down
7 changes: 4 additions & 3 deletions ton/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
// Address is a high-level abstraction containing additional information besides AccountID,
// which is useful for building more advanced workflows.
type Address struct {
ID AccountID
Bounce bool
StateInit *tlb.StateInit
ID AccountID
Bounce bool
StateInit *tlb.StateInit
TestnetOnly bool
}

0 comments on commit 0b2fa5e

Please sign in to comment.