-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy patheth_test.dart
More file actions
67 lines (59 loc) · 2.25 KB
/
eth_test.dart
File metadata and controls
67 lines (59 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import 'package:blockchain_utils/bech32/bech32.dart';
import 'package:blockchain_utils/bip/address/addr_dec_utils.dart';
import 'package:blockchain_utils/bip/address/eth_addr.dart';
import 'package:blockchain_utils/bip/coin_conf/constant/coins_conf.dart';
import '../../quick_hex.dart';
import 'package:blockchain_utils/utils/utils.dart';
import 'package:test/test.dart';
import 'test_vector.dart' show testVector, convertorTestVector;
void main() {
test("eth address test", () {
for (final i in testVector) {
final params = Map<String, dynamic>.from(i["params"]);
final z = EthAddrEncoder()
.encodeKey(BytesUtils.fromHexString(i["public"]), params);
expect(z, i["address"]);
final decode = EthAddrDecoder().decodeAddr(z, params);
expect(decode.toHex(), i["decode"]);
}
});
test("eth address convertor test", () {
for (final i in convertorTestVector) {
final bech32Address = i["bech32"]!;
final ethAddress = i["hex"]!;
expect(
AddrDecUtils.validateAndRemovePrefix(
ethAddress, CoinsConf.ethereum.params.addrPrefix!
).length,
EthAddrConst.addrLen
);
final prefix = bech32Address.split(Bech32Const.separator)[0];
// Convert Bech32 to Hex
final convertedHex =
EthBech32Converter.bech32ToEthAddress(bech32Address, prefix);
expect(convertedHex, ethAddress.toLowerCase(),
reason:
"Converting $bech32Address, Expected: $ethAddress, but got: $convertedHex");
// Convert Hex to Bech32
final convertedBech32 =
EthBech32Converter.ethAddressToBech32(ethAddress, prefix);
expect(convertedBech32, bech32Address,
reason:
"Converting $ethAddress, Expected: $bech32Address, but got: $convertedBech32");
}
final invalidAddressWithWrongLength =
"0x1448b2449076672aCD167b91406c09552101C5";
expect(
AddrDecUtils.validateAndRemovePrefix(
invalidAddressWithWrongLength,
CoinsConf.ethereum.params.addrPrefix!
).length,
isNot(EthAddrConst.addrLen)
);
(
() => EthBech32Converter.ethAddressToBech32(
invalidAddressWithWrongLength, "eth"),
throwsA(isA<AssertionError>())
);
});
}