-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathpropose.ts
More file actions
117 lines (99 loc) · 4.34 KB
/
Copy pathpropose.ts
File metadata and controls
117 lines (99 loc) · 4.34 KB
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
/* eslint-disable no-console */
import { Command, flags } from '@oclif/command'
import { network } from '../../storage/networks'
import { CliUx } from '@oclif/core'
import { green, red } from 'colors'
import { getExplorer } from '../../apis/getExplorer'
import { Authorization } from '@proton/wrap-constants'
import { Serialize } from '@proton/js'
export default class MultisigPropose extends Command {
static description = 'Multisig Propose'
static args = [
{name: 'proposalName', required: true, help: 'Name of proposal'},
{name: 'actions', required: true, help: 'Actions JSON'},
{name: 'auth', required: true, help: 'Your authorization'},
]
static flags: { [k: string]: flags.IFlag<number>; } = {
blocksBehind: flags.integer({char: 'b', default: 30}),
expireSeconds: flags.integer({char: 'x', default: 60 * 60 * 24 * 7 }),
}
async run() {
const {args: {proposalName, actions, auth}, flags} = this.parse(MultisigPropose)
const [actor, permission] = auth.split('@')
// Serialize inner actions using the contract ABIs
const parsedActions = JSON.parse(actions)
const serializedActions = await network.api.serializeActions(parsedActions)
const transactionSettings = await network.protonApi.generateTransactionSettings(flags.expireSeconds, flags.blocksBehind, 0) as any
// Find required signers
let requested: Authorization[] = []
for (const action of parsedActions) {
for (const { actor, permission } of action.authorization) {
const requiredAccountsLocal = await network.protonApi.getRequiredAccounts(actor, permission)
requested = requested.concat(requiredAccountsLocal)
}
}
requested = requested.filter((item, pos) => requested.findIndex(_ => _.actor === item.actor) === pos)
// Manually serialize the inner transaction to avoid the library's
// recursive re-serialization bug (it tries to re-parse already-serialized
// action data as structured JSON, causing "Name should be less than 13
// characters" errors for actions with u64/u128 fields).
const trxBuf = new Serialize.SerialBuffer()
// Transaction header
const expDate = new Date(transactionSettings.expiration + 'Z')
trxBuf.pushUint32(Math.floor(expDate.getTime() / 1000))
trxBuf.pushUint16(transactionSettings.ref_block_num & 0xffff)
trxBuf.pushUint32(transactionSettings.ref_block_prefix)
trxBuf.pushVaruint32(0) // max_net_usage_words
trxBuf.push(0) // max_cpu_usage_ms
trxBuf.pushVaruint32(0) // delay_sec
// context_free_actions (empty)
trxBuf.pushVaruint32(0)
// actions
trxBuf.pushVaruint32(serializedActions.length)
for (const action of serializedActions) {
trxBuf.pushName(action.account)
trxBuf.pushName(action.name)
// authorization
trxBuf.pushVaruint32(action.authorization.length)
for (const auth of action.authorization) {
trxBuf.pushName(auth.actor)
trxBuf.pushName(auth.permission)
}
// data (already serialized hex)
const dataBytes = Buffer.from(action.data, 'hex')
trxBuf.pushVaruint32(dataBytes.length)
trxBuf.pushArray(dataBytes)
}
// transaction_extensions (empty)
trxBuf.pushVaruint32(0)
// Serialize the propose action data manually
const proposeBuf = new Serialize.SerialBuffer()
proposeBuf.pushName(actor) // proposer
proposeBuf.pushName(proposalName) // proposal_name
// requested (permission_level[])
proposeBuf.pushVaruint32(requested.length)
for (const req of requested) {
proposeBuf.pushName(req.actor)
proposeBuf.pushName(req.permission)
}
// trx (inline transaction struct, not length-prefixed)
const trxBytes = trxBuf.asUint8Array()
proposeBuf.pushArray(trxBytes)
const proposeDataHex = Buffer.from(proposeBuf.asUint8Array()).toString('hex')
try {
// Pass pre-serialized hex data to avoid recursive serialization
await network.transact({
actions: [{
account: 'eosio.msig',
name: 'propose',
data: proposeDataHex,
authorization: [{ actor, permission: permission || 'active' }]
}]
})
CliUx.ux.log(green(`Multisig ${proposalName} successfully proposed.`))
CliUx.ux.url(`View Proposal`, `${getExplorer()}/msig/${actor}/${proposalName}`)
} catch (err: any) {
return this.error(red(err));
}
}
}