-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInter-exchange_Client_Address_Protocol_(ICAP).sol
73 lines (60 loc) · 2.18 KB
/
Inter-exchange_Client_Address_Protocol_(ICAP).sol
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
68
69
70
71
72
73
pragma solidity ^0.8.0;
contract ICAP {
function encode(bytes memory account, uint8 checksum) public pure returns (string memory) {
require(account.length == 20, "Invalid Ethereum account address length");
bytes memory iban = abi.encodePacked("XE", "00", checksum, account);
uint8[37] memory digits;
uint8 index;
for (uint i = 0; i < iban.length; i++) {
if (iban[i] >= 65 && iban[i] <= 90) {
digits[index] = uint8(iban[i]) - 55;
index++;
} else if (iban[i] >= 48 && iban[i] <= 57) {
digits[index] = uint8(iban[i]) - 48;
index++;
}
}
uint8 checksumResult = 0;
for (uint8 i = 0; i < 9; i++) {
checksumResult = checksumResult * 10 + digits[i];
checksumResult %= 97;
}
checksumResult = 98 - checksumResult;
digits[2] = checksumResult / 10;
digits[3] = checksumResult % 10;
bytes memory result = new bytes(34);
for (uint i = 0; i < 34; i += 2) {
result[i] = bytes1(digits[i/2] / 10 + 48);
result[i+1] = bytes1(digits[i/2] % 10 + 48);
}
return string(result);
}
function validate(string memory icap) public pure returns (bool) {
require(bytes(icap).length == 34, "Invalid ICAP length");
bytes memory iban = new bytes(25);
iban[0] = "X";
iban[1] = "E";
iban[2] = "0";
iban[3] = "0";
for (uint i = 0; i < 16; i++) {
iban[i+4] = bytes(icap)[i];
}
uint8[37] memory digits;
uint8 index;
for (uint i = 0; i < iban.length; i++) {
if (iban[i] >= 65 && iban[i] <= 90) {
digits[index] = uint8(iban[i]) - 55;
index++;
} else if (iban[i] >= 48 && iban[i] <= 57) {
digits[index] = uint8(iban[i]) - 48;
index++;
}
}
uint8 checksumResult = 0;
for (uint8 i = 0; i < 9; i++) {
checksumResult = checksumResult * 10 + digits[i];
checksumResult %= 97;
}
return checksumResult == 1;
}
}