-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathGeneric.ts
107 lines (96 loc) · 2.8 KB
/
Generic.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
100
101
102
103
104
105
106
107
import type * as nt from 'nekoton-wasm';
import { Address, TokensObject } from 'everscale-inpage-provider';
import { Account, PrepareMessageParams, AccountsStorageContext } from './';
type GenericAccountCall = {
method: string;
params: TokensObject<string>;
stateInit?: string;
};
type PrepareMessage = (args: PrepareMessageParams, ctx: AccountsStorageContext) => Promise<GenericAccountCall>;
/**
* @category AccountsStorage
*/
export class GenericAccount implements Account {
public readonly address: Address;
private readonly abi: string;
private readonly prepareMessageImpl: PrepareMessage;
private publicKey?: string;
constructor(args: {
address: string | Address,
abi: object | string,
prepareMessage: PrepareMessage
publicKey?: string,
}) {
this.address = args.address instanceof Address ? args.address : new Address(args.address);
this.abi = typeof args.abi === 'string' ? args.abi : JSON.stringify(args.abi);
this.prepareMessageImpl = args.prepareMessage;
this.publicKey = args.publicKey;
}
public async fetchPublicKey(ctx: AccountsStorageContext): Promise<string> {
if (this.publicKey != null) {
return this.publicKey;
}
this.publicKey = await ctx.fetchPublicKey(this.address);
return this.publicKey;
}
async prepareMessage(args: PrepareMessageParams, ctx: AccountsStorageContext): Promise<nt.SignedMessage> {
const publicKey = await this.fetchPublicKey(ctx);
const signer = await ctx.getSigner(publicKey);
const { method, params, stateInit } = await this.prepareMessageImpl(args, ctx);
return await ctx.createExternalMessage({
address: this.address,
signer,
timeout: args.timeout,
abi: this.abi,
method,
params,
stateInit,
});
}
}
/**
* @category AccountsStorage
*/
export class MsigAccount extends GenericAccount {
constructor(args: {
address: string | Address,
publicKey?: string
}) {
super({
address: args.address,
publicKey: args.publicKey,
abi: MSIG_ABI,
prepareMessage: async (args, ctx) => {
const payload = args.payload
? ctx.encodeInternalInput(args.payload)
: '';
return {
method: 'sendTransaction',
params: {
dest: args.recipient,
value: args.amount,
bounce: args.bounce,
flags: 3,
payload,
},
};
},
});
}
}
const MSIG_ABI = `{
"ABI version": 2,
"header": ["pubkey", "time", "expire"],
"functions": [{
"name": "sendTransaction",
"inputs": [
{"name":"dest","type":"address"},
{"name":"value","type":"uint128"},
{"name":"bounce","type":"bool"},
{"name":"flags","type":"uint8"},
{"name":"payload","type":"cell"}
],
"outputs": []
}],
"events": []
}`;