Skip to content

Commit 967e6ee

Browse files
authored
Merge pull request #14 from qiwu7/qi/address_for_network
Support Address encoding based on network
2 parents e8d718a + 29f3aca commit 967e6ee

File tree

2 files changed

+186
-54
lines changed

2 files changed

+186
-54
lines changed

address.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ var addressAtlasEntry = atlas.BuildEntry(Address{}).Transform().
3232
})).
3333
Complete()
3434

35+
// CurrentNetwork specifies which network the address belongs to
36+
var CurrentNetwork = Testnet
37+
3538
// Address is the go type that represents an address in the filecoin network.
3639
type Address struct{ str string }
3740

@@ -93,7 +96,7 @@ func (a Address) Bytes() []byte {
9396

9497
// String returns an address encoded as a string.
9598
func (a Address) String() string {
96-
str, err := encode(Testnet, a)
99+
str, err := encode(CurrentNetwork, a)
97100
if err != nil {
98101
panic(err) // I don't know if this one is okay
99102
}

address_test.go

Lines changed: 182 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -136,76 +136,121 @@ func TestSecp256k1Address(t *testing.T) {
136136

137137
func TestVectorSecp256k1Address(t *testing.T) {
138138
testCases := []struct {
139-
input []byte
140-
expected string
139+
input []byte
140+
expectedTestnetAddrStr string
141+
expectedMainnetAddrStr string
141142
}{
142143
{[]byte{4, 148, 2, 250, 195, 126, 100, 50, 164, 22, 163, 160, 202, 84,
143144
38, 181, 24, 90, 179, 178, 79, 97, 52, 239, 162, 92, 228, 135, 200,
144145
45, 46, 78, 19, 191, 69, 37, 17, 224, 210, 36, 84, 33, 248, 97, 59,
145146
193, 13, 114, 250, 33, 102, 102, 169, 108, 59, 193, 57, 32, 211,
146147
255, 35, 63, 208, 188, 5},
147-
"t15ihq5ibzwki2b4ep2f46avlkrqzhpqgtga7pdrq"},
148+
"t15ihq5ibzwki2b4ep2f46avlkrqzhpqgtga7pdrq",
149+
"f15ihq5ibzwki2b4ep2f46avlkrqzhpqgtga7pdrq",
150+
},
148151

149152
{[]byte{4, 118, 135, 185, 16, 55, 155, 242, 140, 190, 58, 234, 103, 75,
150153
18, 0, 12, 107, 125, 186, 70, 255, 192, 95, 108, 148, 254, 42, 34,
151154
187, 204, 38, 2, 255, 127, 92, 118, 242, 28, 165, 93, 54, 149, 145,
152155
82, 176, 225, 232, 135, 145, 124, 57, 53, 118, 238, 240, 147, 246,
153156
30, 189, 58, 208, 111, 127, 218},
154-
"t12fiakbhe2gwd5cnmrenekasyn6v5tnaxaqizq6a"},
157+
"t12fiakbhe2gwd5cnmrenekasyn6v5tnaxaqizq6a",
158+
"f12fiakbhe2gwd5cnmrenekasyn6v5tnaxaqizq6a",
159+
},
155160
{[]byte{4, 222, 253, 208, 16, 1, 239, 184, 110, 1, 222, 213, 206, 52,
156161
248, 71, 167, 58, 20, 129, 158, 230, 65, 188, 182, 11, 185, 41, 147,
157162
89, 111, 5, 220, 45, 96, 95, 41, 133, 248, 209, 37, 129, 45, 172,
158163
65, 99, 163, 150, 52, 155, 35, 193, 28, 194, 255, 53, 157, 229, 75,
159164
226, 135, 234, 98, 49, 155},
160-
"t1wbxhu3ypkuo6eyp6hjx6davuelxaxrvwb2kuwva"},
165+
"t1wbxhu3ypkuo6eyp6hjx6davuelxaxrvwb2kuwva",
166+
"f1wbxhu3ypkuo6eyp6hjx6davuelxaxrvwb2kuwva",
167+
},
161168
{[]byte{4, 3, 237, 18, 200, 20, 182, 177, 13, 46, 224, 157, 149, 180,
162169
104, 141, 178, 209, 128, 208, 169, 163, 122, 107, 106, 125, 182, 61,
163170
41, 129, 30, 233, 115, 4, 121, 216, 239, 145, 57, 233, 18, 73, 202,
164171
189, 57, 50, 145, 207, 229, 210, 119, 186, 118, 222, 69, 227, 224,
165172
133, 163, 118, 129, 191, 54, 69, 210},
166-
"t1xtwapqc6nh4si2hcwpr3656iotzmlwumogqbuaa"},
173+
"t1xtwapqc6nh4si2hcwpr3656iotzmlwumogqbuaa",
174+
"f1xtwapqc6nh4si2hcwpr3656iotzmlwumogqbuaa",
175+
},
167176
{[]byte{4, 247, 150, 129, 154, 142, 39, 22, 49, 175, 124, 24, 151, 151,
168177
181, 69, 214, 2, 37, 147, 97, 71, 230, 1, 14, 101, 98, 179, 206, 158,
169178
254, 139, 16, 20, 65, 97, 169, 30, 208, 180, 236, 137, 8, 0, 37, 63,
170179
166, 252, 32, 172, 144, 251, 241, 251, 242, 113, 48, 164, 236, 195,
171180
228, 3, 183, 5, 118},
172-
"t1xcbgdhkgkwht3hrrnui3jdopeejsoatkzmoltqy"},
181+
"t1xcbgdhkgkwht3hrrnui3jdopeejsoatkzmoltqy",
182+
"f1xcbgdhkgkwht3hrrnui3jdopeejsoatkzmoltqy",
183+
},
173184
{[]byte{4, 66, 131, 43, 248, 124, 206, 158, 163, 69, 185, 3, 80, 222,
174185
125, 52, 149, 133, 156, 164, 73, 5, 156, 94, 136, 221, 231, 66, 133,
175186
223, 251, 158, 192, 30, 186, 188, 95, 200, 98, 104, 207, 234, 235,
176187
167, 174, 5, 191, 184, 214, 142, 183, 90, 82, 104, 120, 44, 248, 111,
177188
200, 112, 43, 239, 138, 31, 224},
178-
"t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy"},
189+
"t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
190+
"f17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
191+
},
179192
}
180193

181194
for _, tc := range testCases {
182195
tc := tc
183-
t.Run(fmt.Sprintf("testing secp256k1 address: %s", tc.expected), func(t *testing.T) {
196+
name := fmt.Sprintf(
197+
"testing secp256k1 address: %s (testnet), %s (mainnet)",
198+
tc.expectedTestnetAddrStr,
199+
tc.expectedMainnetAddrStr,
200+
)
201+
t.Run(name, func(t *testing.T) {
184202
assert := assert.New(t)
185203

204+
// Testnet
186205
// Round trip encoding and decoding from string
206+
CurrentNetwork = Testnet
187207
addr, err := NewSecp256k1Address(tc.input)
188208
assert.NoError(err)
189-
assert.Equal(tc.expected, addr.String())
209+
assert.Equal(tc.expectedTestnetAddrStr, addr.String())
190210

191-
maybeAddr, err := NewFromString(tc.expected)
211+
maybeTestnetAddr, err := NewFromString(tc.expectedTestnetAddrStr)
192212
assert.NoError(err)
193-
assert.Equal(SECP256K1, maybeAddr.Protocol())
194-
assert.Equal(addressHash(tc.input), maybeAddr.Payload())
213+
assert.Equal(SECP256K1, maybeTestnetAddr.Protocol())
214+
assert.Equal(addressHash(tc.input), maybeTestnetAddr.Payload())
195215

196216
// Round trip to and from bytes
197-
maybeAddrBytes, err := NewFromBytes(maybeAddr.Bytes())
217+
maybeTestnetAddrBytes, err := NewFromBytes(maybeTestnetAddr.Bytes())
198218
assert.NoError(err)
199-
assert.Equal(maybeAddr, maybeAddrBytes)
219+
assert.Equal(maybeTestnetAddr, maybeTestnetAddrBytes)
200220

201221
// Round trip encoding and decoding json
202-
b, err := addr.MarshalJSON()
222+
tb, err := addr.MarshalJSON()
203223
assert.NoError(err)
204224

205-
var newAddr Address
206-
err = newAddr.UnmarshalJSON(b)
225+
var newTestnetAddr Address
226+
err = newTestnetAddr.UnmarshalJSON(tb)
207227
assert.NoError(err)
208-
assert.Equal(addr, newAddr)
228+
assert.Equal(addr, newTestnetAddr)
229+
230+
// Mainnet
231+
// Round trip encoding and decoding from string
232+
CurrentNetwork = Mainnet
233+
assert.Equal(tc.expectedMainnetAddrStr, addr.String())
234+
235+
maybeMainnetAddr, err := NewFromString(tc.expectedMainnetAddrStr)
236+
assert.NoError(err)
237+
assert.Equal(SECP256K1, maybeMainnetAddr.Protocol())
238+
assert.Equal(addressHash(tc.input), maybeMainnetAddr.Payload())
239+
240+
// Round trip to and from bytes
241+
maybeMainnetAddrBytes, err := NewFromBytes(maybeMainnetAddr.Bytes())
242+
assert.NoError(err)
243+
assert.Equal(maybeMainnetAddr, maybeMainnetAddrBytes)
244+
245+
// Round trip encoding and decoding json
246+
mb, err := addr.MarshalJSON()
247+
assert.NoError(err)
248+
249+
var newMainnetAddr Address
250+
err = newMainnetAddr.UnmarshalJSON(mb)
251+
assert.NoError(err)
252+
assert.Equal(addr, newMainnetAddr)
253+
209254
})
210255
}
211256
}
@@ -232,117 +277,201 @@ func TestRandomActorAddress(t *testing.T) {
232277
func TestVectorActorAddress(t *testing.T) {
233278
testCases := []struct {
234279
input []byte
235-
expected string
280+
expectedTestnetAddrStr string
281+
expectedMainnetAddrStr string
236282
}{
237283
{[]byte{118, 18, 129, 144, 205, 240, 104, 209, 65, 128, 68, 172, 192,
238284
62, 11, 103, 129, 151, 13, 96},
239-
"t24vg6ut43yw2h2jqydgbg2xq7x6f4kub3bg6as6i"},
285+
"t24vg6ut43yw2h2jqydgbg2xq7x6f4kub3bg6as6i",
286+
"f24vg6ut43yw2h2jqydgbg2xq7x6f4kub3bg6as6i",
287+
},
240288
{[]byte{44, 175, 184, 226, 224, 107, 186, 152, 234, 101, 124, 92, 245,
241289
244, 32, 35, 170, 35, 232, 142},
242-
"t25nml2cfbljvn4goqtclhifepvfnicv6g7mfmmvq"},
290+
"t25nml2cfbljvn4goqtclhifepvfnicv6g7mfmmvq",
291+
"f25nml2cfbljvn4goqtclhifepvfnicv6g7mfmmvq",
292+
},
243293
{[]byte{2, 44, 158, 14, 162, 157, 143, 64, 197, 106, 190, 195, 92, 141,
244294
88, 125, 160, 166, 76, 24},
245-
"t2nuqrg7vuysaue2pistjjnt3fadsdzvyuatqtfei"},
295+
"t2nuqrg7vuysaue2pistjjnt3fadsdzvyuatqtfei",
296+
"f2nuqrg7vuysaue2pistjjnt3fadsdzvyuatqtfei",
297+
},
246298
{[]byte{223, 236, 3, 14, 32, 79, 15, 89, 216, 15, 29, 94, 233, 29, 253,
247299
6, 109, 127, 99, 189},
248-
"t24dd4ox4c2vpf5vk5wkadgyyn6qtuvgcpxxon64a"},
300+
"t24dd4ox4c2vpf5vk5wkadgyyn6qtuvgcpxxon64a",
301+
"f24dd4ox4c2vpf5vk5wkadgyyn6qtuvgcpxxon64a",
302+
},
249303
{[]byte{61, 58, 137, 232, 221, 171, 84, 120, 50, 113, 108, 109, 70, 140,
250304
53, 96, 201, 244, 127, 216},
251-
"t2gfvuyh7v2sx3patm5k23wdzmhyhtmqctasbr23y"},
305+
"t2gfvuyh7v2sx3patm5k23wdzmhyhtmqctasbr23y",
306+
"f2gfvuyh7v2sx3patm5k23wdzmhyhtmqctasbr23y",
307+
},
252308
}
253309

254310
for _, tc := range testCases {
255311
tc := tc
256-
t.Run(fmt.Sprintf("testing Actor address: %s", tc.expected), func(t *testing.T) {
312+
name := fmt.Sprintf(
313+
"testing Actor address: %s (testnet), %s (mainnet)",
314+
tc.expectedTestnetAddrStr,
315+
tc.expectedMainnetAddrStr,
316+
)
317+
t.Run(name, func(t *testing.T) {
257318
assert := assert.New(t)
258319

320+
// Testnet
259321
// Round trip encoding and decoding from string
322+
CurrentNetwork = Testnet
260323
addr, err := NewActorAddress(tc.input)
261324
assert.NoError(err)
262-
assert.Equal(tc.expected, addr.String())
325+
assert.Equal(tc.expectedTestnetAddrStr, addr.String())
263326

264-
maybeAddr, err := NewFromString(tc.expected)
327+
maybeTestnetAddr, err := NewFromString(tc.expectedTestnetAddrStr)
265328
assert.NoError(err)
266-
assert.Equal(Actor, maybeAddr.Protocol())
267-
assert.Equal(addressHash(tc.input), maybeAddr.Payload())
329+
assert.Equal(Actor, maybeTestnetAddr.Protocol())
330+
assert.Equal(addressHash(tc.input), maybeTestnetAddr.Payload())
268331

269332
// Round trip to and from bytes
270-
maybeAddrBytes, err := NewFromBytes(maybeAddr.Bytes())
333+
maybeTestnetAddrBytes, err := NewFromBytes(maybeTestnetAddr.Bytes())
271334
assert.NoError(err)
272-
assert.Equal(maybeAddr, maybeAddrBytes)
335+
assert.Equal(maybeTestnetAddr, maybeTestnetAddrBytes)
273336

274337
// Round trip encoding and decoding json
275-
b, err := addr.MarshalJSON()
338+
tb, err := addr.MarshalJSON()
276339
assert.NoError(err)
277340

278-
var newAddr Address
279-
err = newAddr.UnmarshalJSON(b)
341+
var newTestnetAddr Address
342+
err = newTestnetAddr.UnmarshalJSON(tb)
280343
assert.NoError(err)
281-
assert.Equal(addr, newAddr)
344+
assert.Equal(addr, newTestnetAddr)
345+
346+
// Mainnet
347+
// Round trip encoding and decoding from string
348+
CurrentNetwork = Mainnet
349+
assert.Equal(tc.expectedMainnetAddrStr, addr.String())
350+
351+
maybeMainnetAddr, err := NewFromString(tc.expectedMainnetAddrStr)
352+
assert.NoError(err)
353+
assert.Equal(Actor, maybeMainnetAddr.Protocol())
354+
assert.Equal(addressHash(tc.input), maybeMainnetAddr.Payload())
355+
356+
// Round trip to and from bytes
357+
maybeMainnetAddrBytes, err := NewFromBytes(maybeMainnetAddr.Bytes())
358+
assert.NoError(err)
359+
assert.Equal(maybeMainnetAddr, maybeMainnetAddrBytes)
360+
361+
// Round trip encoding and decoding json
362+
mb, err := addr.MarshalJSON()
363+
assert.NoError(err)
364+
365+
var newMainnetAddr Address
366+
err = newMainnetAddr.UnmarshalJSON(mb)
367+
assert.NoError(err)
368+
assert.Equal(addr, newMainnetAddr)
282369
})
283370
}
284371
}
285372

286373
func TestVectorBLSAddress(t *testing.T) {
287374
testCases := []struct {
288375
input []byte
289-
expected string
376+
expectedTestnetAddrStr string
377+
expectedMainnetAddrStr string
290378
}{
291379
{[]byte{173, 88, 223, 105, 110, 45, 78, 145, 234, 134, 200, 129, 233, 56,
292380
186, 78, 168, 27, 57, 94, 18, 121, 123, 132, 185, 207, 49, 75, 149, 70,
293381
112, 94, 131, 156, 122, 153, 214, 6, 178, 71, 221, 180, 249, 172, 122,
294382
52, 20, 221},
295-
"t3vvmn62lofvhjd2ugzca6sof2j2ubwok6cj4xxbfzz4yuxfkgobpihhd2thlanmsh3w2ptld2gqkn2jvlss4a"},
383+
"t3vvmn62lofvhjd2ugzca6sof2j2ubwok6cj4xxbfzz4yuxfkgobpihhd2thlanmsh3w2ptld2gqkn2jvlss4a",
384+
"f3vvmn62lofvhjd2ugzca6sof2j2ubwok6cj4xxbfzz4yuxfkgobpihhd2thlanmsh3w2ptld2gqkn2jvlss4a",
385+
},
296386
{[]byte{179, 41, 79, 10, 46, 41, 224, 198, 110, 188, 35, 93, 47, 237,
297387
202, 86, 151, 191, 120, 74, 246, 5, 199, 90, 246, 8, 230, 166, 61, 92,
298388
211, 142, 168, 92, 168, 152, 158, 14, 253, 233, 24, 139, 56, 47,
299389
147, 114, 70, 13},
300-
"t3wmuu6crofhqmm3v4enos73okk2l366ck6yc4owxwbdtkmpk42ohkqxfitcpa57pjdcftql4tojda2poeruwa"},
390+
"t3wmuu6crofhqmm3v4enos73okk2l366ck6yc4owxwbdtkmpk42ohkqxfitcpa57pjdcftql4tojda2poeruwa",
391+
"f3wmuu6crofhqmm3v4enos73okk2l366ck6yc4owxwbdtkmpk42ohkqxfitcpa57pjdcftql4tojda2poeruwa",
392+
},
301393
{[]byte{150, 161, 163, 228, 234, 122, 20, 212, 153, 133, 230, 97, 178,
302394
36, 1, 212, 79, 237, 64, 45, 29, 9, 37, 178, 67, 201, 35, 88, 156,
303395
15, 188, 126, 50, 205, 4, 226, 158, 215, 141, 21, 211, 125, 58, 170,
304396
63, 230, 218, 51},
305-
"t3s2q2hzhkpiknjgmf4zq3ejab2rh62qbndueslmsdzervrhapxr7dftie4kpnpdiv2n6tvkr743ndhrsw6d3a"},
397+
"t3s2q2hzhkpiknjgmf4zq3ejab2rh62qbndueslmsdzervrhapxr7dftie4kpnpdiv2n6tvkr743ndhrsw6d3a",
398+
"f3s2q2hzhkpiknjgmf4zq3ejab2rh62qbndueslmsdzervrhapxr7dftie4kpnpdiv2n6tvkr743ndhrsw6d3a",
399+
},
306400
{[]byte{134, 180, 84, 37, 140, 88, 148, 117, 247, 209, 111, 90, 172, 1,
307401
138, 121, 246, 193, 22, 157, 32, 252, 51, 146, 29, 216, 181, 206, 28,
308402
172, 108, 52, 143, 144, 163, 96, 54, 36, 246, 174, 185, 27, 100, 81,
309403
140, 46, 128, 149},
310-
"t3q22fijmmlckhl56rn5nkyamkph3mcfu5ed6dheq53c244hfmnq2i7efdma3cj5voxenwiummf2ajlsbxc65a"},
404+
"t3q22fijmmlckhl56rn5nkyamkph3mcfu5ed6dheq53c244hfmnq2i7efdma3cj5voxenwiummf2ajlsbxc65a",
405+
"f3q22fijmmlckhl56rn5nkyamkph3mcfu5ed6dheq53c244hfmnq2i7efdma3cj5voxenwiummf2ajlsbxc65a",
406+
},
311407
{[]byte{167, 114, 107, 3, 128, 34, 247, 90, 56, 70, 23, 88, 83, 96, 206,
312408
230, 41, 7, 10, 45, 157, 40, 113, 41, 101, 229, 242, 110, 204, 64,
313409
133, 131, 130, 128, 55, 36, 237, 52, 242, 114, 3, 54, 240, 157, 182,
314410
49, 240, 116},
315-
"t3u5zgwa4ael3vuocgc5mfgygo4yuqocrntuuhcklf4xzg5tcaqwbyfabxetwtj4tsam3pbhnwghyhijr5mixa"},
411+
"t3u5zgwa4ael3vuocgc5mfgygo4yuqocrntuuhcklf4xzg5tcaqwbyfabxetwtj4tsam3pbhnwghyhijr5mixa",
412+
"f3u5zgwa4ael3vuocgc5mfgygo4yuqocrntuuhcklf4xzg5tcaqwbyfabxetwtj4tsam3pbhnwghyhijr5mixa",
413+
},
316414
}
317415

318416
for _, tc := range testCases {
319417
tc := tc
320-
t.Run(fmt.Sprintf("testing bls address: %s", tc.expected), func(t *testing.T) {
418+
name := fmt.Sprintf(
419+
"testing bls address: %s (testnet), %s (mainnet)",
420+
tc.expectedTestnetAddrStr,
421+
tc.expectedMainnetAddrStr,
422+
)
423+
t.Run(name, func(t *testing.T) {
321424
assert := assert.New(t)
322425

426+
// Testnet
323427
// Round trip encoding and decoding from string
428+
CurrentNetwork = Testnet
324429
addr, err := NewBLSAddress(tc.input)
325430
assert.NoError(err)
326-
assert.Equal(tc.expected, addr.String())
431+
assert.Equal(tc.expectedTestnetAddrStr, addr.String())
327432

328-
maybeAddr, err := NewFromString(tc.expected)
433+
maybeTestnetAddr, err := NewFromString(tc.expectedTestnetAddrStr)
329434
assert.NoError(err)
330-
assert.Equal(BLS, maybeAddr.Protocol())
331-
assert.Equal(tc.input, maybeAddr.Payload())
435+
assert.Equal(BLS, maybeTestnetAddr.Protocol())
436+
assert.Equal(tc.input, maybeTestnetAddr.Payload())
332437

333438
// Round trip to and from bytes
334-
maybeAddrBytes, err := NewFromBytes(maybeAddr.Bytes())
439+
maybeTestnetAddrBytes, err := NewFromBytes(maybeTestnetAddr.Bytes())
335440
assert.NoError(err)
336-
assert.Equal(maybeAddr, maybeAddrBytes)
441+
assert.Equal(maybeTestnetAddr, maybeTestnetAddrBytes)
337442

338443
// Round trip encoding and decoding json
339-
b, err := addr.MarshalJSON()
444+
tb, err := addr.MarshalJSON()
340445
assert.NoError(err)
341446

342-
var newAddr Address
343-
err = newAddr.UnmarshalJSON(b)
447+
var newTestnetAddr Address
448+
err = newTestnetAddr.UnmarshalJSON(tb)
344449
assert.NoError(err)
345-
assert.Equal(addr, newAddr)
450+
assert.Equal(addr, newTestnetAddr)
451+
452+
// Mainnet
453+
// Round trip encoding and decoding from string
454+
CurrentNetwork = Mainnet
455+
assert.Equal(tc.expectedMainnetAddrStr, addr.String())
456+
457+
maybeMainnetAddr, err := NewFromString(tc.expectedMainnetAddrStr)
458+
assert.NoError(err)
459+
assert.Equal(BLS, maybeMainnetAddr.Protocol())
460+
assert.Equal(tc.input, maybeMainnetAddr.Payload())
461+
462+
// Round trip to and from bytes
463+
maybeMainnetAddrBytes, err := NewFromBytes(maybeMainnetAddr.Bytes())
464+
assert.NoError(err)
465+
assert.Equal(maybeMainnetAddr, maybeMainnetAddrBytes)
466+
467+
// Round trip encoding and decoding json
468+
mb, err := addr.MarshalJSON()
469+
assert.NoError(err)
470+
471+
var newMainnetAddr Address
472+
err = newMainnetAddr.UnmarshalJSON(mb)
473+
assert.NoError(err)
474+
assert.Equal(addr, newMainnetAddr)
346475
})
347476
}
348477
}

0 commit comments

Comments
 (0)