forked from onflow/fcl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.js
181 lines (164 loc) Β· 5.24 KB
/
decode.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
const latestBlockDeprecationNotice = () => {
console.error(
`
%c@onflow/decode Deprecation Notice
========================
Operating upon data of the latestBlock field of the response object is deprecated and will no longer be recognized in future releases of @onflow/decode.
Find out more here: https://github.com/onflow/flow-js-sdk/blob/master/packages/decode/WARNINGS.md#0001-Deprecating-latestBlock-field
=======================
`
.replace(/\n\s+/g, "\n")
.trim(),
"font-weight:bold;font-family:monospace;"
)
}
const decodeNumber = async (num, _, stack) => {
try {
return Number(num)
} catch (e) {
throw new Error(`Decode Number Error : ${stack.join(".")}`)
}
}
const decodeImplicit = async (i) => i
const decodeVoid = async () => null
const decodeOptional = async (optional, decoders, stack) =>
optional ? await recurseDecode(optional, decoders, stack) : null
const decodeReference = async (v) => ({address: v.address, type: v.type})
const decodeArray = async (array, decoders, stack) =>
await Promise.all(
array.map(
(v) =>
new Promise(async (res) =>
res(await recurseDecode(v, decoders, [...stack, v.type]))
)
)
)
const decodeDictionary = async (dictionary, decoders, stack) =>
await dictionary.reduce(async (acc, v) => {
acc = await acc
acc[
await recurseDecode(v.key, decoders, [...stack, v.key])
] = await recurseDecode(v.value, decoders, [...stack, v.key])
return acc
}, Promise.resolve({}))
const decodeComposite = async (composite, decoders, stack) => {
const decoded = await composite.fields.reduce(async (acc, v) => {
acc = await acc
acc[v.name] = await recurseDecode(v.value, decoders, [...stack, v.name])
return acc
}, Promise.resolve({}))
const decoder = composite.id && decoderLookup(decoders, composite.id)
return decoder ? await decoder(decoded) : decoded
}
const defaultDecoders = {
UInt: decodeNumber,
Int: decodeNumber,
UInt8: decodeNumber,
Int8: decodeNumber,
UInt16: decodeNumber,
Int16: decodeNumber,
UInt32: decodeNumber,
Int32: decodeNumber,
UInt64: decodeNumber,
Int64: decodeNumber,
UInt128: decodeNumber,
Int128: decodeNumber,
UInt256: decodeNumber,
Int256: decodeNumber,
Word8: decodeNumber,
Word16: decodeNumber,
Word32: decodeNumber,
Word64: decodeNumber,
UFix64: decodeImplicit,
Fix64: decodeImplicit,
String: decodeImplicit,
Character: decodeImplicit,
Bool: decodeImplicit,
Address: decodeImplicit,
Void: decodeVoid,
Optional: decodeOptional,
Reference: decodeReference,
Array: decodeArray,
Dictionary: decodeDictionary,
Event: decodeComposite,
Resource: decodeComposite,
Struct: decodeComposite,
}
const decoderLookup = (decoders, lookup) => {
const found = Object.keys(decoders).find((decoder) => {
if (/^\/.*\/$/.test(decoder)) {
const reg = new RegExp(decoder.substring(1, decoder.length - 1))
return reg.test(lookup)
}
return decoder === lookup
})
return lookup && found && decoders[found]
}
const recurseDecode = async (decodeInstructions, decoders, stack) => {
let decoder = decoderLookup(decoders, decodeInstructions.type)
if (!decoder)
throw new Error(
`Undefined Decoder Error: ${decodeInstructions.type}@${stack.join(".")}`
)
return await decoder(decodeInstructions.value, decoders, stack)
}
export const decode = async (
decodeInstructions,
customDecoders = {},
stack = []
) => {
let decoders = {...defaultDecoders, ...customDecoders}
return await recurseDecode(decodeInstructions, decoders, stack)
}
export const decodeResponse = async (response, customDecoders = {}) => {
let decoders = {...defaultDecoders, ...customDecoders}
if (response.encodedData) {
return await decode(response.encodedData, decoders)
} else if (response.transactionStatus) {
return {
...response.transactionStatus,
events: await Promise.all(
response.transactionStatus.events.map(async function decodeEvents(e) {
return {
type: e.type,
transactionId: e.transactionId,
transactionIndex: e.transactionIndex,
eventIndex: e.eventIndex,
data: await decode(e.payload, decoders),
}
})
),
}
} else if (response.transaction) {
return response.transaction
} else if (response.events) {
return await Promise.all(
response.events.map(async function decodeEvents(e) {
return {
blockId: e.blockId,
blockHeight: e.blockHeight,
blockTimestamp: e.blockTimestamp,
type: e.type,
transactionId: e.transactionId,
transactionIndex: e.transactionIndex,
eventIndex: e.eventIndex,
data: await decode(e.payload, decoders),
}
})
)
} else if (response.account) {
return response.account
} else if (response.block) {
return response.block
} else if (response.blockHeader) {
return response.blockHeader
} else if (response.latestBlock) {
latestBlockDeprecationNotice()
return response.latestBlock
} else if (response.transactionId) {
return response.transactionId
} else if (response.collection) {
return response.collection
}
return null
}