-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAinUtil.js
87 lines (77 loc) · 1.7 KB
/
AinUtil.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
const ainUtil = require('@ainblockchain/ain-util');
const CURRENT_PROTOCOL_VERSION = '0.5.0';
function buildTrainRequestTxPath(userAddress, trainId) {
return `/train_tasks/${userAddress}/${trainId}/request`;
}
function buildAinftTrainRequestTxBody(payload) {
const { timestamp, address, trainId, metadata, ...rest } = payload;
return {
operation: {
type: 'SET_VALUE',
ref: buildTrainRequestTxPath(address, trainId),
value: {
data: {
...rest,
metadata,
},
},
},
timestamp,
nonce: -1,
};
}
function buildAuthTxBody(userAddress, timestamp) {
return {
operation: {
type: 'GET_AUTH_TOKEN',
value: {
params: {
address: userAddress,
},
},
},
timestamp,
nonce: -1,
};
}
function buildAuthTxBodyYJ(address, privateKey) {
const timestamp = Date.now();
const payload = signTx(
{
operation: {
type: 'GET_AUTH_TOKEN',
ref: '',
value: {
params: {
address: address,
},
},
},
timestamp,
nonce: -1,
},
privateKey,
);
return payload;
}
function signTx(txBody, privateKey) {
const keyBuffer = Buffer.from(privateKey, 'hex');
const sig = ainUtil.ecSignTransaction(txBody, keyBuffer);
const sigBuffer = ainUtil.toBuffer(sig);
const lenHash = sigBuffer.length - 65;
const hashedData = sigBuffer.slice(0, lenHash);
const txHash = '0x' + hashedData.toString('hex');
return {
txHash,
signedTx: {
protoVer: CURRENT_PROTOCOL_VERSION,
tx_body: txBody,
signature: sig,
},
};
}
module.exports = {
buildAinftTrainRequestTxBody,
buildAuthTxBody,
signTx,
}