-
Notifications
You must be signed in to change notification settings - Fork 249
/
delegate.ts
99 lines (93 loc) · 3.29 KB
/
delegate.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
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
import { PublicKey } from '@near-js/crypto';
import { Assignable } from '@near-js/types';
import BN from 'bn.js';
import { actionCreators } from './action_creators';
import { Action } from './actions';
const {
addKey,
createAccount,
deleteAccount,
deleteKey,
deployContract,
functionCall,
stake,
transfer,
} = actionCreators;
export class DelegateAction extends Assignable {
senderId: string;
receiverId: string;
actions: Array<Action>;
nonce: BN;
maxBlockHeight: BN;
publicKey: PublicKey;
}
/**
* Compose a delegate action for inclusion with a meta transaction signed on the sender's behalf
* @params.actions The set of actions to be included in the meta transaction
* @params.maxBlockHeight The maximum block height for which this action can be executed as part of a transaction
* @params.nonce Current nonce on the access key used to sign the delegate action
* @params.publicKey Public key for the access key used to sign the delegate action
* @params.receiverId Account ID for the intended receiver of the meta transaction
* @params.senderId Account ID for the intended signer of the delegate action
*/
export function buildDelegateAction({
actions,
maxBlockHeight,
nonce,
publicKey,
receiverId,
senderId,
}: DelegateAction): DelegateAction {
return new DelegateAction({
senderId,
receiverId,
actions: actions.map((a) => {
// @ts-expect-error type workaround
if (!a.type && !a.params) {
return a;
}
// @ts-expect-error type workaround
switch (a.type) {
case 'AddKey': {
// @ts-expect-error type workaround
const { publicKey, accessKey } = a.params;
return addKey(publicKey, accessKey);
}
case 'CreateAccount': {
// @ts-expect-error type workaround
return createAccount(a.params.createAccount);
}
case 'DeleteAccount': {
// @ts-expect-error type workaround
return deleteAccount(a.params.deleteAccount);
}
case 'DeleteKey': {
// @ts-expect-error type workaround
return deleteKey(a.params.publicKey);
}
case 'DeployContract': {
// @ts-expect-error type workaround
return deployContract(a.params.code);
}
case 'FunctionCall': {
// @ts-expect-error type workaround
const { methodName, args, gas, deposit } = a.params;
return functionCall(methodName, args, gas, deposit);
}
case 'Stake': {
// @ts-expect-error type workaround
return stake(a.params.stake, a.params.publicKey);
}
case 'Transfer': {
// @ts-expect-error type workaround
const { deposit } = a.params;
return transfer(deposit);
}
}
throw new Error('Unrecognized action');
}),
nonce,
maxBlockHeight,
publicKey,
});
}