-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathmethods.ts
115 lines (103 loc) · 2.99 KB
/
methods.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
108
109
110
111
112
113
114
115
import { ExtensionPageConnection } from '@fuel-wallet/connections';
import { transactionRequestify } from 'fuels';
import { IS_CRX } from '~/config';
import { Services, store } from '~/store';
import { AccountService } from '~/systems/Account/services/account';
import type {
MessageInputs,
PopUpServiceInputs,
} from '~/systems/CRX/background/services/types';
import { listenToGlobalErrors } from '~/systems/Core/utils/listenToGlobalErrors';
// On external methods we need to wait for the state to be updated
// this can take time as the user can take a while to respond
// so we use a default timeout of 5 minutes
const WAIT_FOR_CONFIG = {
timeout: 1000 * 60 * 5,
};
export class RequestMethods extends ExtensionPageConnection {
readonly methods = [
this.requestConnection,
this.signMessage,
this.sendTransaction,
this.addAssets,
this.selectNetwork,
this.addNetwork,
];
constructor() {
super();
super.externalMethods(this.methods);
}
static start() {
return new RequestMethods();
}
async requestConnection(input: MessageInputs['requestConnection']) {
const state = await store
.requestConnection(input)
.waitForState(Services.connectRequest, WAIT_FOR_CONFIG);
return !!state.context.isConnected;
}
async signMessage(input: MessageInputs['signMessage']) {
const state = await store
.requestMessage(input)
.waitForState(Services.msgRequest, WAIT_FOR_CONFIG);
return state.context.signedMessage;
}
async sendTransaction(input: MessageInputs['sendTransaction']) {
const {
address,
provider,
transaction,
origin,
title,
favIconUrl,
skipCustomFee,
transactionState,
transactionSummary,
} = input;
const transactionRequest = transactionRequestify(JSON.parse(transaction));
const state = await store
.requestTransaction({
origin,
transactionRequest,
address,
providerConfig: provider,
title,
favIconUrl,
skipCustomFee,
transactionState,
transactionSummary,
})
.waitForState(Services.txRequest, {
...WAIT_FOR_CONFIG,
done: 'txSuccess',
});
return state.context.response?.txSummaryExecuted?.id;
}
async addAssets(input: MessageInputs['addAssets']) {
await store
.requestAddAsset(input)
.waitForState(Services.addAssetRequest, WAIT_FOR_CONFIG);
return true;
}
async selectNetwork(input: PopUpServiceInputs['selectNetwork']) {
await store
.requestSelectNetwork(input)
.waitForState(Services.selectNetworkRequest, WAIT_FOR_CONFIG);
return true;
}
async addNetwork(input: PopUpServiceInputs['addNetwork']) {
await store
.requestSelectNetwork(input)
.waitForState(Services.selectNetworkRequest, WAIT_FOR_CONFIG);
return true;
}
}
if (IS_CRX) {
RequestMethods.start();
}
listenToGlobalErrors((error) => {
store.send(Services.reportError, {
type: 'SAVE_ERROR',
input: error,
});
});