/
extrapolateFromXdr.js
171 lines (151 loc) · 5.79 KB
/
extrapolateFromXdr.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
// This code originally taken from the XDR Viewer https://github.com/stellar/xdr-viewer
// by Stellar Development Foundation under Apache-2.0.
// This turns a base64 encoded xdr object with it's type, and turns it into an
// object with more detailed information suitable for use in the tree view.
// Values can be one of three types:
// - undefined
// - string: string values that appear as just plain text
// - object: typed values always with a type and value `{type: 'code', value: 'Foo();'}`
import {xdr, encodeCheck, Keypair, Operation} from 'stellar-sdk';
export default function extrapolateFromXdr(input, type) {
// TODO: Check to see if type exists
// TODO: input validation
let xdrObject;
try {
xdrObject = xdr[type].fromXDR(input, 'base64');
} catch(error) {
throw new Error('Input XDR could not be parsed');
}
let tree = [{}];
buildTreeFromObject(xdrObject, tree[0], type);
return tree;
}
function buildTreeFromObject(object, anchor, name) {
anchor.type = name;
if (_.isArray(object)) {
parseArray(anchor, object);
} else if (!hasChildren(object)) {
anchor.value = getValue(object, name);
} else if (object.switch) {
parseArm(anchor, object)
} else {
parseNormal(anchor, object)
}
}
function parseArray(anchor, object) {
anchor.value = `Array[${object.length}]`;
anchor.nodes = [];
for (var i = 0; i < object.length; i++) {
anchor.nodes.push({});
buildTreeFromObject(object[i], anchor.nodes[anchor.nodes.length-1], '[' + i + ']');
}
}
function parseArm(anchor, object) {
anchor.value = '['+object.switch().name+']';
if (_.isString(object.arm())) {
anchor.nodes = [{}];
buildTreeFromObject(object[object.arm()](), anchor.nodes[anchor.nodes.length-1], object.arm());
}
}
function parseNormal(anchor, object) {
anchor.nodes = [];
_(object).functions().without('toXDR', 'ext').value().forEach(function(name) {
anchor.nodes.push({});
buildTreeFromObject(object[name](), anchor.nodes[anchor.nodes.length-1], name);
});
}
function hasChildren(object) {
// string
if (_.isString(object)) {
return false;
}
// node buffer
if (object && object._isBuffer) {
return false;
}
var functions = _(object).functions();
if (functions.value().length == 0) {
return false;
}
// int64
if (functions.include('getLowBits') && functions.include('getHighBits')) {
return false;
}
return true;
}
const amountFields = ['amount', 'startingBalance', 'sendMax', 'destAmount', 'limit'];
function getValue(object, name) {
if (_.includes(amountFields, name)) {
return {
type: 'amount',
value: {
parsed: Operation._fromXDRAmount(object),
raw: object.toString()
}
};
}
if (name === 'hint') {
// strkey encoding is using base32 encoding. Encoded public key consists of:
//
// * 1 byte version byte (0x30 encoded as `G`)
// * 32 bytes public key
// * 2 bytes checksum
//
// Because base32 symbols are 5-bit, more than one symbol is needed to represent a single byte.
// Signature Hint is the last 4 bytes of the public key. So we need to try to show as many 5-bit
// chunks as possible included between bytes 30 and 33 (included).
//
// byte 1: ##### ###
// byte 2: ## ##### #
// byte 3: #### ####
// byte 4: # ##### ##
// byte 5: ### ##### <---------- 40 bits / full alignment
// byte 6: ##### ###
// byte 7: ## ##### #
//
// .....
//
// byte 26: ##### ###
// byte 27: ## ##### #
// byte 28: #### #### full b32 symbols
// byte 29: # ##### ## |--------------------------|
// byte 30: ### 48### |
// byte 31: Signature Hint start | 49### 50# | Signature Hint end
// byte 32: ## 51### 5 | |
// byte 33: 2### 53##
// byte 34: # 54### 55
// byte 35: ### 56###
//
let hintBytes = new Buffer(object, 'base64');
let partialPublicKey = Buffer.concat([new Buffer(28).fill(0), hintBytes]);
let keypair = new Keypair({publicKey: partialPublicKey});
let partialPublicKeyString =
'G'+
(new Buffer(46).fill('_').toString())+
keypair.accountId().substr(47, 5)+
(new Buffer(4).fill('_').toString());
return {type: 'code', value: partialPublicKeyString};
}
if (name === 'ed25519') {
var address = encodeCheck("accountId", object);
return {type: 'code', value: address};
}
if (name === 'assetCode' || name === 'assetCode4' || name === 'assetCode12') {
return object.toString();
}
if (object && object._isBuffer) {
return {type: 'code', value: new Buffer(object).toString('base64')};
}
if (typeof object === 'undefined') {
return;
}
// getValue is a leaf in the recursive xdr extrapolating function meaning that
// whatever this function returns will be in the final result as-is.
// Therefore, we want them in string format so that it displayable in React.
// One example of why we need this is that UnsignedHyper values won't get
// displayed unless we convert it to a string.
if (typeof object.toString === 'function') {
return object.toString();
}
throw new Error('Internal laboratory bug: Encountered value type in XDR viewer that does not have a toString method');
}