forked from onflow/fcl-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinteraction.js
282 lines (245 loc) · 8.06 KB
/
interaction.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import {invariant} from "@onflow/util-invariant"
export const UNKNOWN /* */ = "UNKNOWN"
export const SCRIPT /* */ = "SCRIPT"
export const TRANSACTION /* */ = "TRANSACTION"
export const GET_TRANSACTION_STATUS /* */ = "GET_TRANSACTION_STATUS"
export const GET_ACCOUNT /* */ = "GET_ACCOUNT"
export const GET_EVENTS /* */ = "GET_EVENTS"
export const GET_LATEST_BLOCK /* */ = "GET_LATEST_BLOCK"
export const PING /* */ = "PING"
export const GET_TRANSACTION /* */ = "GET_TRANSACTION"
export const GET_BLOCK_BY_ID /* */ = "GET_BLOCK_BY_ID"
export const GET_BLOCK_BY_HEIGHT /* */ = "GET_BLOCK_BY_HEIGHT"
export const GET_BLOCK /* */ = "GET_BLOCK"
export const GET_BLOCK_HEADER /* */ = "GET_BLOCK_HEADER"
export const GET_COLLECTION /* */ = "GET_COLLECTION"
export const BAD /* */ = "BAD"
export const OK /* */ = "OK"
export const ACCOUNT /* */ = "ACCOUNT"
export const PARAM /* */ = "PARAM"
export const ARGUMENT /* */ = "ARGUMENT"
export const AUTHORIZER /* */ = "authorizer"
export const PAYER /* */ = "payer"
export const PROPOSER /* */ = "proposer"
const ACCT = `{
"kind":"${ACCOUNT}",
"tempId":null,
"addr":null,
"keyId":null,
"sequenceNum":null,
"signature":null,
"signingFunction":null,
"resolve":null,
"role": {
"proposer":false,
"authorizer":false,
"payer":false,
"param":false
}
}`
const PRM = `{
"kind":"${PARAM}",
"tempId":null,
"key":null,
"value":null,
"asParam":null,
"xform":null,
"resolve": null
}`
const ARG = `{
"kind":"${ARGUMENT}",
"tempId":null,
"value":null,
"asArgument":null,
"xform":null,
"resolve": null,
"resolveArgument": null
}`
const IX = `{
"tag":"${UNKNOWN}",
"assigns":{},
"status":"${OK}",
"reason":null,
"accounts":{},
"params":{},
"arguments":{},
"message": {
"cadence":null,
"refBlock":null,
"computeLimit":null,
"proposer":null,
"payer":null,
"authorizations":[],
"params":[],
"arguments":[]
},
"proposer":null,
"authorizations":[],
"payer":null,
"events": {
"eventType":null,
"start":null,
"end":null,
"blockIds":[]
},
"transaction": {
"id":null
},
"block": {
"id":null,
"height":null,
"isSealed":null
},
"account": {
"addr":null
},
"collection": {
"id":null
}
}`
const KEYS = new Set(Object.keys(JSON.parse(IX)))
export const interaction = () => JSON.parse(IX)
const CHARS = "abcdefghijklmnopqrstuvwxyz0123456789".split("")
const randChar = () => CHARS[~~(Math.random() * CHARS.length)]
export const uuid = () => Array.from({length: 10}, randChar).join("")
export const isNumber = d => typeof d === "number"
export const isArray = d => Array.isArray(d)
export const isObj = d => d !== null && typeof d === "object"
export const isNull = d => d == null
export const isFn = d => typeof d === "function"
export const isInteraction = ix => {
if (!isObj(ix) || isNull(ix) || isNumber(ix)) return false
for (let key of KEYS) if (!ix.hasOwnProperty(key)) return false
return true
}
export const Ok = ix => {
ix.status = OK
return ix
}
export const Bad = (ix, reason) => {
ix.status = BAD
ix.reason = reason
return ix
}
const makeIx = wat => ix => {
ix.tag = wat
return Ok(ix)
}
export const prepAccount = (acct, opts = {}) => ix => {
invariant(
typeof acct === "function" || typeof acct === "object",
"prepAccount must be passed an authorization function or an account object"
)
invariant(opts.role != null, "Account must have a role")
const ACCOUNT = JSON.parse(ACCT)
const role = opts.role
const tempId = uuid()
if (acct.authorization && isFn(acct.authorization)) acct = {resolve: acct.authorization}
if (!acct.authorization && isFn(acct)) acct = {resolve: acct}
ix.accounts[tempId] = {
...ACCOUNT,
tempId,
...acct,
role: {
...ACCOUNT.role,
...(typeof acct.role === "object" ? acct.role : {}),
[role]: true,
},
}
if (role === AUTHORIZER) {
ix.authorizations.push(tempId)
} else {
ix[role] = tempId
}
return ix
}
export const makeArgument = arg => ix => {
let tempId = uuid()
ix.message.arguments.push(tempId)
ix.arguments[tempId] = JSON.parse(ARG)
ix.arguments[tempId].tempId = tempId
ix.arguments[tempId].value = arg.value
ix.arguments[tempId].asArgument = arg.asArgument
ix.arguments[tempId].xform = arg.xform
ix.arguments[tempId].resolve = arg.resolve
ix.arguments[tempId].resolveArgument = arg.resolveArgument
return Ok(ix)
}
export const makeUnknown /* */ = makeIx(UNKNOWN)
export const makeScript /* */ = makeIx(SCRIPT)
export const makeTransaction /* */ = makeIx(TRANSACTION)
export const makeGetTransactionStatus /* */ = makeIx(GET_TRANSACTION_STATUS)
export const makeGetTransaction /* */ = makeIx(GET_TRANSACTION)
export const makeGetAccount /* */ = makeIx(GET_ACCOUNT)
export const makeGetEvents /* */ = makeIx(GET_EVENTS)
export const makeGetLatestBlock /* */ = makeIx(GET_LATEST_BLOCK)
export const makeGetBlockById /* */ = makeIx(GET_BLOCK_BY_ID)
export const makeGetBlockByHeight /* */ = makeIx(GET_BLOCK_BY_HEIGHT)
export const makePing /* */ = makeIx(PING)
export const makeGetBlock /* */ = makeIx(GET_BLOCK)
export const makeGetBlockHeader /* */ = makeIx(GET_BLOCK_HEADER)
export const makeGetCollection /* */ = makeIx(GET_COLLECTION)
const is = wat => ix => ix.tag === wat
export const isUnknown /* */ = is(UNKNOWN)
export const isScript /* */ = is(SCRIPT)
export const isTransaction /* */ = is(TRANSACTION)
export const isGetTransactionStatus /* */ = is(GET_TRANSACTION_STATUS)
export const isGetTransaction /* */ = is(GET_TRANSACTION)
export const isGetAccount /* */ = is(GET_ACCOUNT)
export const isGetEvents /* */ = is(GET_EVENTS)
export const isGetLatestBlock /* */ = is(GET_LATEST_BLOCK)
export const isGetBlockById /* */ = is(GET_BLOCK_BY_ID)
export const isGetBlockByHeight /* */ = is(GET_BLOCK_BY_HEIGHT)
export const isPing /* */ = is(PING)
export const isGetBlock /* */ = is(GET_BLOCK)
export const isGetBlockHeader /* */ = is(GET_BLOCK_HEADER)
export const isGetCollection /* */ = is(GET_COLLECTION)
export const isOk /* */ = ix => ix.status === OK
export const isBad /* */ = ix => ix.status === BAD
export const why /* */ = ix => ix.reason
export const isAccount /* */ = account => account.kind === ACCOUNT
export const isParam /* */ = param => param.kind === PARAM
export const isArgument /* */ = argument => argument.kind === ARGUMENT
const hardMode = ix => {
for (let key of Object.keys(ix)) {
if (!KEYS.has(key))
throw new Error(`"${key}" is an invalid root level Interaction property.`)
}
return ix
}
const recPipe = async (ix, fns = []) => {
try {
ix = hardMode(await ix)
if (isBad(ix)) throw new Error(`Interaction Error: ${ix.reason}`)
if (!fns.length) return ix
const [hd, ...rest] = fns
const cur = await hd
if (isFn(cur)) return recPipe(cur(ix), rest)
if (isNull(cur) || !cur) return recPipe(ix, rest)
if (isInteraction(cur)) return recPipe(cur, rest)
throw new Error("Invalid Interaction Composition")
} catch (e) {
throw e
}
}
export const pipe = (...args) => {
const [arg1, arg2] = args
if (isArray(arg1) && arg2 == null) return d => pipe(d, arg1)
return recPipe(arg1, arg2)
}
const identity = v => v
export const get = (ix, key, fallback) => {
return ix.assigns[key] == null ? fallback : ix.assigns[key]
}
export const put = (key, value) => ix => {
ix.assigns[key] = value
return Ok(ix)
}
export const update = (key, fn = identity) => ix => {
ix.assigns[key] = fn(ix.assigns[key], ix)
return Ok(ix)
}
export const destroy = key => ix => {
delete ix.assigns[key]
return Ok(ix)
}