Skip to content

add testnetOnly field into address struct #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
}