This repository has been archived by the owner on Sep 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
utils.js
137 lines (126 loc) · 3.96 KB
/
utils.js
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
const tokenSchemaBasic = ({
to,
gas,
value
}) => {
let base = `ethereum:${to}`,
gasBlock = '',
valueBlock = '';
if (gas) gasBlock = `[?gas=${gas}]`;
if (value) valueBlock = `[?value=${value}]`;
return `ethereum:${to}${gasBlock}${valueBlock}`;
}
/**
* generate string: ethereum:${to}${gasBlock}${valueBlock}${gasBlock}`
* @param {*} param0
*/
const tokenSchemaFunction = ({
to,
gas,
value,
functionSignature
}) => {
let base = `ethereum:${to}`,
functionBlock = '',
gasBlock = '',
valueBlock = '';
if (functionSignature) {
let convertedArgs = '',
payable = !!functionSignature.payable ? ' payable' : '';
functionSignature.args.forEach((arg, index) => {
const isLast = index < functionSignature.args.length - 1 ? ',' : '';
convertedArgs += `${arg.type} ${arg.name}${isLast}`;
});
functionBlock = `[?function=${functionSignature.name}${payable}(${convertedArgs})]`
}
if (gas) gasBlock = `[?gas=${gas}]`;
if (value) valueBlock = `[?value=${value}]`;
return `ethereum:${to}${gasBlock}${valueBlock}${functionBlock}`;
}
//todo add other params ??
const tokenSchemaContract = ({
to,
gas,
contract
}) => {
let base = `ethereum:${to}`,
gasBlock = '',
contractBlock = '';
if (gas) gasBlock = `[?gas=${gas}]`;
if (contract) contractBlock = `[?contract=${contract}]`;
return `ethereum:${to}${gasBlock}${contractBlock}`;
}
export default {
eth: tokenSchemaBasic,
function: tokenSchemaFunction,
erc20: tokenSchemaContract
}
const validStrRegEx = /^[^\\\/&]*$/;
const isValidString = str => str && str.length > 0 && str.match(validStrRegEx);
/**
* the corect format e.g. is:
*
* ..
* functionSignature: {
* 'name': 'myFunc',
* 'payable': false,
* 'args': [{
* 'name': 'adress',
* 'type': 'uint'
* }]
* },
* ..
*
*/
export const validateSignature = (signature) => {
if (signature.name === null || typeof signature.name === 'undefined') return false;
if (signature.payable === null || typeof signature.payable === 'undefined') return false;
if (!signature.args || signature.args.length === 0) return false;
let allArgsCheck = false;
signature.args.forEach(arg => {
if (!isValidString(arg.type) || !isValidString(arg.name)) allArgsCheck = false;
})
return allArgsCheck;
}
/**
* Checks if the given string is an address
* from ethereum.stackexchange.com/questions/1374/how-can-i-check-if-an-ethereum-address-is-valid
*
* @method isAddress
* @param {String} address the given HEX adress
* @return {Boolean}
*/
export const isAddress = function (address) {
if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) {
// check if it has the basic requirements of an address
return false;
} else if (/^(0x)?[0-9a-f]{40}$/.test(address) || /^(0x)?[0-9A-F]{40}$/.test(address)) {
// If it's all small caps or all all caps, return true
return true;
} else {
// Otherwise check each case
return true;
//todo - need to add SHA
//https://github.com/ethereum/go-ethereum/blob/aa9fff3e68b1def0a9a22009c233150bf9ba481f/jsre/ethereum_js.go
//return isChecksumAddress(address);
}
};
/**
* Checks if the given string is a checksummed address
*
* @method isChecksumAddress
* @param {String} address the given HEX adress
* @return {Boolean}
*/
export const isChecksumAddress = function (address) {
// Check each case
address = address.replace('0x', '');
var addressHash = sha3(address.toLowerCase());
for (var i = 0; i < 40; i++) {
// the nth letter should be uppercase if the nth digit of casemap is 1
if ((parseInt(addressHash[i], 16) > 7 && address[i].toUpperCase() !== address[i]) || (parseInt(addressHash[i], 16) <= 7 && address[i].toLowerCase() !== address[i])) {
return false;
}
}
return true;
};