-
Notifications
You must be signed in to change notification settings - Fork 1
/
amino.js
92 lines (80 loc) · 2.46 KB
/
amino.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
'use strict';
const Codec = require("./codec.js");
const Sha256 = require("sha256");
const Config = require('./config');
const R_Cosmos = require('./tx');
/**
* 处理amino编码(目前支持序列化)
*
*/
class Amino {
constructor() {
this._keyMap = {};
}
/**
*/
GetRegisterInfo(key) {
let info = this._keyMap[key];
if (info === undefined) {
throw new Error("not Registered");
}
return info
}
/**
* 注册amino类型
*
* @param class field的类型
* @param key amino前缀
*/
RegisterConcrete(type, key) {
this._keyMap[key] = {
prefix: this._aminoPrefix(key),
classType: type
}
}
/**
* 给消息加上amino前缀
*
* @param key amino前缀
* @param message 编码msg
* @returns { Array }
*/
MarshalBinary(key, message) {
let prefixBytes = this._keyMap[key].prefix;
prefixBytes = Buffer.from(prefixBytes.concat(message.length));
prefixBytes = Buffer.concat([prefixBytes, message]);
return prefixBytes
}
MarshalJSON(key, message) {
let pair = {
"type": key,
"value": message
};
return pair
}
_aminoPrefix(name) {
let a = Sha256(name);
let b = Codec.Hex.hexToBytes(a);
while (b[0] === 0) {
b = b.slice(1, b.length - 1)
}
b = b.slice(3, b.length - 1);
while (b[0] === 0) {
b = b.slice(1, b.length - 1)
}
b = b.slice(0, 4);//注意和go-amino v0.6.2以前的不一样
return b
}
}
let amino = new Amino();
amino.RegisterConcrete(null, Config.amino.pubKey);
amino.RegisterConcrete(null, Config.amino.signature);
amino.RegisterConcrete(R_Cosmos.MsgDelegate, Config.tx.delegate.prefix);
amino.RegisterConcrete(R_Cosmos.MsgSend, Config.tx.transfer.prefix);
amino.RegisterConcrete(R_Cosmos.MsgSetWithdrawAddress, Config.tx.setWithdrawAddress.prefix);
amino.RegisterConcrete(R_Cosmos.MsgWithdrawDelegatorReward, Config.tx.withdrawDelegatorReward.prefix);
amino.RegisterConcrete(R_Cosmos.MsgWithdrawValidatorCommission, Config.tx.withdrawValidatorCommission.prefix);
amino.RegisterConcrete(R_Cosmos.MsgUndelegate, Config.tx.undelegate.prefix);
amino.RegisterConcrete(R_Cosmos.MsgBeginRedelegate, Config.tx.beginRedelegate.prefix);
amino.RegisterConcrete(R_Cosmos.StdTx, Config.tx.stdTx.prefix);
module.exports = amino;