forked from near/multisig-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactions.js
281 lines (260 loc) · 8.99 KB
/
actions.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
import * as nearAPI from 'near-api-js';
import * as utils from './utils.js';
import {deployLockup} from './lockup.js';
import {createLedgerU2FClient} from './ledger.js'
import sha256 from "js-sha256";
async function setAccountSigner(contract) {
const accessKeys = await contract.getAccessKeys();
console.log(accessKeys);
let {publicKey, path} = await utils.findPath(accessKeys.map(({public_key}) => public_key));
if (path == null) {
alert(`Ledger path not found. Make sure to add it first in "Keys" section`);
throw new Error(`No key found`);
}
console.log(`Found ${publicKey} at ${path}`);
const client = await createLedgerU2FClient();
publicKey = nearAPI.utils.PublicKey.fromString(publicKey);
contract.connection.signer = {
async getPublicKey() {
return publicKey;
},
async signMessage(message) {
const signature = await client.sign(message, path);
return {signature, publicKey};
}
};
}
function funcCall(methodName, args, deposit, gas) {
return {
"type": "FunctionCall",
"method_name": methodName,
"args": btoa(JSON.stringify(args ? args : {})),
"deposit": deposit ? deposit : '0',
"gas": gas ? gas : '100000000000000'
};
}
async function addKey(contract, requestOnly) {
let accountId = contract.accountId;
let publicKeyStr = document.querySelector('#new-key').value;
// check it's a valid key.
let publicKey = nearAPI.utils.PublicKey.fromString(publicKeyStr);
console.log(`Add ${publicKey.toString()} key`);
let methodNames = ['add_request', 'add_request_and_confirm', 'confirm', 'delete_request'];
if (requestOnly) {
methodNames = ['add_request'];
}
await contract.functionCall(accountId, 'add_request', {
request: {
receiver_id: accountId,
actions: [
{
type: "AddKey",
public_key: publicKey.toString().replace('ed25519:', ''),
permission: {
allowance: null,
receiver_id: accountId,
method_names: methodNames,
}
}
]
}
})
}
async function lockupEnableTransfer(contract) {
const accountId = contract.accountId;
const lockupAccountId = utils.accountToLockup(utils.LOCKUP_BASE, accountId);
await contract.functionCall(accountId, 'add_request', {
request: {
receiver_id: lockupAccountId,
actions: [
funcCall("check_transfers_vote", {})
]
}
});
}
async function transfer(contract, isLockup) {
let accountId = contract.accountId;
let receiverId = document.querySelector('#transfer-receiver').value;
if (!await utils.accountExists(window.near.connection, receiverId)) {
alert(`Account ${receiverId} doesn't exist`);
return;
}
let amount = document.querySelector('#transfer-amount').value;
console.log(`Send from ${accountId} to ${receiverId} ${amount}`);
amount = utils.parseAmount(amount);
if (isLockup) {
const lockupAccountId = utils.accountToLockup(utils.LOCKUP_BASE, accountId);
await contract.functionCall(accountId, 'add_request', {
request: {
receiver_id: lockupAccountId,
actions: [
funcCall("transfer", {receiver_id: receiverId})
]
}
});
} else {
await contract.functionCall(accountId, 'add_request', {
request: {
receiver_id: receiverId,
actions: [
{type: "Transfer", amount}
]
}
});
}
}
async function setNumConfirmations(contract) {
let accountId = contract.accountId;
let numConfirmations = document.querySelector('#num-confirmations').value;
try {
numConfirmations = parseInt(numConfirmations);
} catch (error) {
alert(error);
return;
}
const accessKeys = await contract.getAccessKeys();
console.log(`Change ${accountId} to ${numConfirmations} of ${accessKeys.length} multisig`);
if (numConfirmations + 1 > accessKeys.length) {
alert(`Dangerously high number of confirmations. Set lower or add more keys`);
return;
}
if (numConfirmations < 1) {
alert('Min num confirmations is 1');
return;
}
await contract.functionCall(accountId, 'add_request', {
request: {
receiver_id: accountId,
actions: [
{type: "SetNumConfirmations", num_confirmations: numConfirmations}
]
}
});
}
async function vestingTermination(contract, requestKind) {
let accountId = contract.accountId;
let lockupAccountId = document.querySelector('#lockup-account-id').value;
if (!await utils.accountExists(window.near.connection, lockupAccountId)) {
alert(`Account ${lockupAccountId} doesn't exist`);
return;
}
const lockupAccount = await window.near.account(lockupAccountId);
console.log(`Vesting ${requestKind} for ${lockupAccountId}`);
if (requestKind === "terminate_vesting") {
await contract.functionCall(accountId, 'add_request', {
request: {
receiver_id: lockupAccountId,
actions: [
funcCall("terminate_vesting", {})
]
}
});
} else if (requestKind === "termination_withdraw") {
await contract.functionCall(accountId, 'add_request', {
request: {
receiver_id: lockupAccountId,
actions: [
funcCall("termination_withdraw", {receiver_id: accountId})
]
}
});
}
}
function dateToEpoch(date) {
return Math.floor(date.getTime() / 1000.0 * 1000000)
}
async function vestingPrivateTermination(contract, requestKind) {
let accountId = contract.accountId;
let lockupAccountId = document.querySelector('#lockup-account-id').value;
let lockupVestingStartDate = dateToEpoch(new Date(document.querySelector('#lockup-vesting-start-date').value));
let lockupVestingEndDate = dateToEpoch(new Date(document.querySelector('#lockup-vesting-end-date').value));
let lockupVestingCliffDate = dateToEpoch(new Date(document.querySelector('#lockup-vesting-cliff-date').value));
let lockupVestingSalt = document.querySelector('#lockup-vesting-salt').value;
if (!await utils.accountExists(window.near.connection, lockupAccountId)) {
alert(`Account ${lockupAccountId} doesn't exist`);
return;
}
const lockupAccount = await window.near.account(lockupAccountId);
console.log(`Vesting ${requestKind} for ${lockupAccountId}`);
let publicKeyStr = document.querySelector('#new-key').value;
let publicKey = nearAPI.utils.PublicKey.fromString(publicKeyStr);
const salt = Buffer.from(sha256(Buffer.from(lockupVestingSalt + publicKey)), 'hex').toString('base64');
let args = {
vesting_schedule_with_salt:
{
vesting_schedule:
{
start_timestamp: lockupVestingStartDate.toString(),
cliff_timestamp: lockupVestingCliffDate.toString(),
end_timestamp: lockupVestingEndDate.toString()
},
salt
}
}
//console.log(args);
//args = Buffer.from(JSON.stringify(args)).toString('base64');
//console.log(args);
try {
await contract.functionCall(accountId, 'add_request', {
request: {
receiver_id: lockupAccountId,
actions: [funcCall("terminate_vesting", args)],
}
});
} catch (e) {
console.log(e);
}
}
async function setupMultisig(contract) {
}
async function setupLockup(contract) {
let accountId = document.querySelector('#create-lockup-account-id').value;
let amount = document.querySelector('#create-lockup-amount').value;
let duration = document.querySelector('#create-lockup-duration').value;
let allowStaking = document.querySelector('#create-lockup-staking').value;
if (!await utils.accountExists(window.near.connection, accountId)) {
alert(`${accountId} doesn't exit. Create it first.`);
return;
}
amount = utils.parseAmount(amount);
try {
duration = parseInt(duration);
} catch (error) {
alert(`Failed to parse duration ${duration}`);
return;
}
// Days to nano seconds.
duration = duration * 60 * 60 * 24 * 1000 * 1000 * 1000;
await deployLockup(contract, accountId, amount, duration, allowStaking);
}
async function submitRequest(accountId, requestKind) {
let contract = await window.near.account(accountId);
try {
await setAccountSigner(contract);
if (requestKind === "add_key" || requestKind === "add_request_key") {
await addKey(contract, requestKind === "add_request_key");
} else if (requestKind === "transfer" || requestKind === "transfer_lockup") {
await transfer(contract, requestKind === "transfer_lockup");
} else if (requestKind === "num_confirmations") {
await setNumConfirmations(contract);
} else if (requestKind === "terminate_vesting" || requestKind === "termination_withdraw") {
await vestingTermination(contract, requestKind);
} else if (requestKind === "terminate_private_vesting") {
await vestingPrivateTermination(contract, requestKind);
} else if (requestKind === "multisig") {
await setupMultisig(contract);
} else if (requestKind === "lockup") {
await setupLockup(contract);
} else {
alert(`Unkonwn request kind: ${requestKind}`);
}
} catch (error) {
console.log(error);
alert(error);
}
}
module.exports = {
setAccountSigner,
submitRequest,
funcCall
}