forked from ainblockchain/ain-blockchain
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain-util.js
189 lines (163 loc) · 4.38 KB
/
chain-util.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const EC = require('elliptic').ec;
const ec = new EC('secp256k1');
const stringify = require('fast-json-stable-stringify');
const ainUtil = require('@ainblockchain/ain-util');
const RuleUtil = require('./db/rule-util');
const ruleUtil = new RuleUtil();
const PRIVATE_KEY = process.env.PRIVATE_KEY || null;
class ChainUtil {
static hashString(stringData) {
if (typeof stringData !== 'string') return '';
return '0x' + ainUtil.hashMessage(stringData).toString('hex');
}
static shortenHash(hash) {
if (typeof hash !== 'string' || hash.length < 10) return hash;
return hash.substring(0,6) + '...' + hash.substring(hash.length - 4, hash.length);
}
static hashSignature(sig) {
const sigBuffer = ainUtil.toBuffer(sig);
const lenHash = sigBuffer.length - 65;
const hashedData = sigBuffer.slice(0, lenHash);
return '0x' + hashedData.toString('hex');
}
// TODO (lia): remove this function
static genKeyPair() {
let keyPair;
if (PRIVATE_KEY) {
keyPair = ec.keyFromPrivate(PRIVATE_KEY, 'hex');
keyPair.getPublic();
} else {
keyPair = ec.genKeyPair();
}
return keyPair;
}
static isBool(value) {
return ruleUtil.isBool(value);
}
static isNumber(num) {
return ruleUtil.isNumber(num);
}
static isString(value) {
return ruleUtil.isString(value);
}
static isArray(value) {
return ruleUtil.isArray(value);
}
static isDict(value) {
return ruleUtil.isDict(value);
}
static isEmptyNode(value) {
return ruleUtil.isEmptyNode(value);
}
static isValAddr(value) {
return ruleUtil.isValAddr(value);
}
static isCksumAddr(addr) {
return ruleUtil.isCksumAddr(addr);
}
static isValShardProto(value) {
return ruleUtil.isValShardProto(value);
}
static boolOrFalse(value) {
return ChainUtil.isBool(value) ? value : false;
}
static numberOrZero(num) {
return ChainUtil.isNumber(num) ? num : 0;
}
static stringOrEmpty(str) {
return ChainUtil.isString(str) ? str : '';
}
static toBool(value) {
return ruleUtil.toBool(value);
}
// TODO(lia): normalize addresses in user inputs using this function.
static toCksumAddr(addr) {
return ruleUtil.toCksumAddr(addr);
}
static toString(value) {
if (ChainUtil.isBool(value)) {
return value.toString();
} else if (ChainUtil.isNumber(value)) {
return value.toString();
} else if (ChainUtil.isString(value)) {
return value;
} else if (value === undefined) {
return '';
} else {
return JSON.stringify(value);
}
}
static parsePath(path) {
if (!path) {
return [];
}
return path.split('/').filter((node) => {
return !!node;
});
}
static formatPath(parsedPath) {
if (!Array.isArray(parsedPath) || parsedPath.length === 0) {
return '/';
}
let formatted = '';
for (const label of parsedPath) {
if (ChainUtil.isString(label)) {
formatted += '/' + label;
} else {
formatted += '/' + stringify(label);
}
}
return (formatted.startsWith('/') ? '' : '/') + formatted;
}
static getJsObject(obj, path) {
if (!ChainUtil.isArray(path)) {
return null;
}
let ref = obj;
for (let i = 0; i < path.length; i++) {
const key = ChainUtil.toString(path[i]);
if (!ChainUtil.isDict(ref)) {
return null;
}
ref = ref[key];
}
return ref === undefined ? null : ref;
}
static setJsObject(obj, path, value) {
if (!ChainUtil.isArray(path)) {
return false;
}
if (!ChainUtil.isDict(obj)) {
return false;
}
if (path.length == 0) {
return false;
}
let ref = obj;
for (let i = 0; i < path.length - 1; i++) {
const key = ChainUtil.toString(path[i]);
if (!ChainUtil.isDict(ref[key])) {
ref[key] = {};
}
ref = ref[key];
}
const key = ChainUtil.toString(path[path.length - 1]);
ref[key] = value;
return true;
}
static transactionFailed(response) {
if (Array.isArray(response)) {
response.forEach(res => {
if (ChainUtil.checkForTransactionErrorCode(res)) {
return true;
}
});
return false;
}
return ChainUtil.checkForTransactionErrorCode(response);
}
static checkForTransactionErrorCode(response) {
return response === null || (response.code !== undefined && response.code !== 0);
}
}
module.exports = ChainUtil;