Skip to content

Commit d4521ff

Browse files
authored
added address utils to find network for address (#174)
1 parent c724fc9 commit d4521ff

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

address.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package overflow
2+
3+
import (
4+
"math/big"
5+
"strings"
6+
)
7+
8+
var (
9+
linearCodeN = 64
10+
11+
parityCheckMatrixColumns = []*big.Int{
12+
big.NewInt(0x00001), big.NewInt(0x00002), big.NewInt(0x00004), big.NewInt(0x00008),
13+
big.NewInt(0x00010), big.NewInt(0x00020), big.NewInt(0x00040), big.NewInt(0x00080),
14+
big.NewInt(0x00100), big.NewInt(0x00200), big.NewInt(0x00400), big.NewInt(0x00800),
15+
big.NewInt(0x01000), big.NewInt(0x02000), big.NewInt(0x04000), big.NewInt(0x08000),
16+
big.NewInt(0x10000), big.NewInt(0x20000), big.NewInt(0x40000), big.NewInt(0x7328d),
17+
big.NewInt(0x6689a), big.NewInt(0x6112f), big.NewInt(0x6084b), big.NewInt(0x433fd),
18+
big.NewInt(0x42aab), big.NewInt(0x41951), big.NewInt(0x233ce), big.NewInt(0x22a81),
19+
big.NewInt(0x21948), big.NewInt(0x1ef60), big.NewInt(0x1deca), big.NewInt(0x1c639),
20+
big.NewInt(0x1bdd8), big.NewInt(0x1a535), big.NewInt(0x194ac), big.NewInt(0x18c46),
21+
big.NewInt(0x1632b), big.NewInt(0x1529b), big.NewInt(0x14a43), big.NewInt(0x13184),
22+
big.NewInt(0x12942), big.NewInt(0x118c1), big.NewInt(0x0f812), big.NewInt(0x0e027),
23+
big.NewInt(0x0d00e), big.NewInt(0x0c83c), big.NewInt(0x0b01d), big.NewInt(0x0a831),
24+
big.NewInt(0x982b), big.NewInt(0x07034), big.NewInt(0x0682a), big.NewInt(0x05819),
25+
big.NewInt(0x03807), big.NewInt(0x007d2), big.NewInt(0x00727), big.NewInt(0x0068e),
26+
big.NewInt(0x0067c), big.NewInt(0x0059d), big.NewInt(0x004eb), big.NewInt(0x003b4),
27+
big.NewInt(0x0036a), big.NewInt(0x002d9), big.NewInt(0x001c7), big.NewInt(0x0003f),
28+
}
29+
)
30+
31+
func GetNetworkFromAddress(input string) string {
32+
address := strings.TrimPrefix(input, "0x")
33+
testnet, _ := new(big.Int).SetString("6834ba37b3980209", 16)
34+
emulator, _ := new(big.Int).SetString("1cb159857af02018", 16)
35+
36+
networkCodewords := map[string]*big.Int{
37+
"mainnet": big.NewInt(0),
38+
"testnet": testnet,
39+
"emulator": emulator,
40+
}
41+
42+
for network, codeWord := range networkCodewords {
43+
if IsValidAddressForNetwork(address, network, codeWord) {
44+
return network
45+
}
46+
}
47+
return ""
48+
}
49+
50+
func IsValidAddressForNetwork(address, network string, codeWord *big.Int) bool {
51+
flowAddress, ok := new(big.Int).SetString(address, 16)
52+
if !ok {
53+
panic("not valid address")
54+
}
55+
codeWord.Xor(codeWord, flowAddress)
56+
57+
if codeWord.Cmp(big.NewInt(0)) == 0 {
58+
return false
59+
}
60+
61+
parity := big.NewInt(0)
62+
for i := 0; i < linearCodeN; i++ {
63+
if codeWord.Bit(0) == 1 {
64+
parity.Xor(parity, parityCheckMatrixColumns[i])
65+
}
66+
codeWord.Rsh(codeWord, 1)
67+
}
68+
69+
return parity.Cmp(big.NewInt(0)) == 0 && codeWord.Cmp(big.NewInt(0)) == 0
70+
}

address_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,18 @@ func TestGetAddress(t *testing.T) {
2323
})
2424
}
2525
}
26+
27+
func TestAddressNetworks(t *testing.T) {
28+
t.Run("emulator with prefix", func(t *testing.T) {
29+
assert.Equal(t, "emulator", GetNetworkFromAddress("0xf8d6e0586b0a20c7"))
30+
})
31+
t.Run("emulator", func(t *testing.T) {
32+
assert.Equal(t, "emulator", GetNetworkFromAddress("f8d6e0586b0a20c7"))
33+
})
34+
t.Run("testnet", func(t *testing.T) {
35+
assert.Equal(t, "testnet", GetNetworkFromAddress("9a0766d93b6608b7"))
36+
})
37+
t.Run("mainnet", func(t *testing.T) {
38+
assert.Equal(t, "mainnet", GetNetworkFromAddress("f233dcee88fe0abe"))
39+
})
40+
}

0 commit comments

Comments
 (0)