-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathvoucher.ts
74 lines (66 loc) · 2.21 KB
/
voucher.ts
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
import {withPrefix} from "@onflow/util-address"
import {Voucher, encodeTxIdFromVoucher} from "../encode/encode"
import {Interaction} from "@onflow/typedefs"
export function findInsideSigners(ix: Interaction) {
// Inside Signers Are: (authorizers + proposer) - payer
let inside = new Set(ix.authorizations)
if (ix.proposer) {
inside.add(ix.proposer)
}
if (Array.isArray(ix.payer)) {
ix.payer.forEach(p => inside.delete(p))
} else {
inside.delete(ix.payer)
}
return Array.from(inside)
}
export function findOutsideSigners(ix: Interaction) {
// Outside Signers Are: (payer)
let outside = new Set(Array.isArray(ix.payer) ? ix.payer : [ix.payer])
return Array.from(outside)
}
export const createSignableVoucher = (ix: Interaction) => {
const buildAuthorizers = () => {
const authorizations = ix.authorizations
.map(cid => withPrefix(ix.accounts[cid].addr))
.reduce((prev: (string | null)[], current) => {
return prev.find(item => item === current) ? prev : [...prev, current]
}, [])
return authorizations
}
const buildInsideSigners = () =>
findInsideSigners(ix).map(id => ({
address: withPrefix(ix.accounts[id].addr),
keyId: ix.accounts[id].keyId,
sig: ix.accounts[id].signature,
}))
const buildOutsideSigners = () =>
findOutsideSigners(ix).map(id => ({
address: withPrefix(ix.accounts[id].addr),
keyId: ix.accounts[id].keyId,
sig: ix.accounts[id].signature,
}))
const proposalKey = ix.proposer
? {
address: withPrefix(ix.accounts[ix.proposer].addr),
keyId: ix.accounts[ix.proposer].keyId,
sequenceNum: ix.accounts[ix.proposer].sequenceNum,
}
: {}
return {
cadence: ix.message.cadence,
refBlock: ix.message.refBlock || null,
computeLimit: ix.message.computeLimit,
arguments: ix.message.arguments.map(id => ix.arguments[id].asArgument),
proposalKey,
payer: withPrefix(
ix.accounts[Array.isArray(ix.payer) ? ix.payer[0] : ix.payer].addr
),
authorizers: buildAuthorizers(),
payloadSigs: buildInsideSigners(),
envelopeSigs: buildOutsideSigners(),
}
}
export const voucherToTxId = (voucher: Voucher) => {
return encodeTxIdFromVoucher(voucher)
}