forked from AElfProject/aelf-web3.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto.js
197 lines (180 loc) · 5.11 KB
/
proto.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
190
191
192
193
194
195
196
197
/**
* @file proto utils
* @author atom-yang
*/
import * as protobuf from '@aelfqueen/protobufjs/light';
import coreDescriptor from '../../proto/core.proto.json';
import * as utils from './utils';
import {
transform,
OUTPUT_TRANSFORMERS,
transformArrayToMap
} from './transform';
export const coreRootProto = protobuf.Root.fromJSON(coreDescriptor);
/* eslint-disable no-unused-vars */
export const {
Transaction,
Hash,
Address,
TransactionFeeCharged,
ResourceTokenCharged
} = coreRootProto;
export const getFee = (base64Str, type = 'TransactionFeeCharged') => {
if (['ResourceTokenCharged', 'TransactionFeeCharged'].indexOf(type) === -1) {
throw new Error('type needs to be one of ResourceTokenCharged and TransactionFeeCharged');
}
const dataType = coreRootProto[type];
let deserialize = dataType.decode(Buffer.from(base64Str, 'base64'));
deserialize = dataType.toObject(deserialize, {
enums: String, // enums as string names
longs: String, // longs as strings (requires long.js)
bytes: String, // bytes as base64 encoded strings
defaults: true, // includes default values
arrays: true, // populates empty arrays (repeated fields) even if defaults=false
objects: true, // populates empty objects (map fields) even if defaults=false
oneofs: true // includes virtual oneof fields set to the present field's name
});
// eslint-disable-next-line max-len
let deserializeLogResult = transform(dataType, deserialize, OUTPUT_TRANSFORMERS);
deserializeLogResult = transformArrayToMap(dataType, deserializeLogResult);
return deserializeLogResult;
};
export const getSerializedDataFromLog = log => {
const {
NonIndexed,
Indexed = []
} = log;
const serializedData = [...(Indexed || [])];
if (NonIndexed) {
serializedData.push(NonIndexed);
}
return serializedData.join('');
};
export const getResourceFee = (Logs = []) => {
if (!Array.isArray(Logs) || Logs.length === 0) {
return [];
}
return Logs.filter(log => log.Name === 'ResourceTokenCharged')
.map(v => getFee(getSerializedDataFromLog(v), 'ResourceTokenCharged'));
};
export const getTransactionFee = (Logs = []) => {
if (!Array.isArray(Logs) || Logs.length === 0) {
return [];
}
return Logs.filter(log => log.Name === 'TransactionFeeCharged')
.map(v => getFee(getSerializedDataFromLog(v), 'TransactionFeeCharged'));
};
/**
* arrayBuffer To Hex
*
* @alias module:AElf/pbUtils
* @param {Buffer} arrayBuffer arrayBuffer
* @return {string} hex string
*/
export const arrayBufferToHex = arrayBuffer => Array.prototype.map.call(
new Uint8Array(arrayBuffer),
n => (`0${n.toString(16)}`).slice(-2)
).join('');
/**
* get hex rep From Address
*
* @alias module:AElf/pbUtils
* @param {protobuf} address kernel.Address
* @return {string} hex rep of address
*/
export const getRepForAddress = address => {
const message = Address.fromObject(address);
let hex = '';
if (message.value instanceof Buffer) {
hex = message.value.toString('hex');
} else {
// Uint8Array
hex = arrayBufferToHex(message.value);
}
return utils.encodeAddressRep(hex);
};
/**
* get address From hex rep
*
* @alias module:AElf/pbUtils
* @param {string} rep address
* @return {protobuf} address kernel.Address
*/
export const getAddressFromRep = rep => {
const hex = utils.decodeAddressRep(rep);
return Address.create({
value: Buffer.from(hex.replace('0x', ''), 'hex')
});
};
/**
* get address From hex rep
*
* @alias module:AElf/pbUtils
* @param {string} rep address
* @return {protobuf} address kernel.Address
*/
export const getAddressObjectFromRep = rep => Address.toObject(getAddressFromRep(rep));
/**
* get hex rep From hash
*
* @alias module:AElf/pbUtils
* @param {protobuf} hash kernel.Hash
* @return {string} hex rep
*/
export const getRepForHash = hash => {
const message = Address.fromObject(hash);
let hex = '';
if (message.value instanceof Buffer) {
hex = message.value.toString('hex');
} else {
// Uint8Array
hex = arrayBufferToHex(message.value);
}
return hex;
};
/**
* get Hash From Hex
*
* @alias module:AElf/pbUtils
* @param {string} hex string
* @return {protobuf} kernel.Hash
*/
export const getHashFromHex = hex => Hash.create({
value: Buffer.from(hex.replace('0x', ''), 'hex')
});
/**
* get Hash Object From Hex
*
* @alias module:AElf/pbUtils
* @param {string} hex string
* @return {Object} kernel.Hash Hash ot Object
*/
export const getHashObjectFromHex = hex => Hash.toObject(getHashFromHex(hex));
/**
* encode Transaction to protobuf type
*
* @alias module:AElf/pbUtils
* @param {Object} tx object
* @return {protobuf} kernel.Transaction
*/
export const encodeTransaction = tx => Transaction.encode(tx).finish();
/**
* get Transaction
*
* @alias module:AElf/pbUtils
* @param {string} from
* @param {string} to
* @param {string} methodName
* @param {string} params
* @return {protobuf} kernel.Transaction
*/
export const getTransaction = (from, to, methodName, params) => {
const txn = {
from: getAddressFromRep(from),
to: getAddressFromRep(to),
methodName,
params
};
return Transaction.create(txn);
};
/* eslint-enable */