This repository has been archived by the owner on Aug 26, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 55
/
index.ts
229 lines (214 loc) · 6.11 KB
/
index.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
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
import { Linking, Platform } from "react-native";
import { Buffer } from "buffer";
import {
TrustCommand,
Request,
AccountsRequest,
MessageRequest,
TransactionRequest,
AndroidTransactionRequest,
DAppMetadata,
} from "./lib/commands";
import { TrustError } from "./lib/errors";
import { TW, CoinType } from "@trustwallet/wallet-core";
import { BigNumber } from 'ethers';
class TrustWallet {
callbackScheme: string;
callbackId = new Date().getTime();
app = {
name: "Trust",
scheme: "trust://",
AppStoreURL:
"https://itunes.apple.com/us/app/trust-ethereum-wallet/id1288339409",
GooglePlayURL:
"https://play.google.com/store/apps/details?id=com.wallet.crypto.trustapp",
};
resolvers: { [key: string]: (value: string) => void } = {};
rejectors: { [key: string]: (value: Object) => void } = {};
/**
* constructor
* @param callbackScheme default callback scheme
*/
constructor(callbackScheme: string) {
// Linking.getInitialURL().then((url: string) => this.handleURL(url))
this.callbackScheme = callbackScheme;
this.start();
}
/**
* start listening openURL event, you don"t need to call it unless you explicit called cleanup
*/
public start() {
Linking.addEventListener("url", this.handleOpenURL.bind(this));
}
/**
* stop listening openURL event and clean resolvers
*/
public cleanup() {
Linking.removeEventListener("url", this.handleOpenURL.bind(this));
this.resolvers = {};
}
/**
* check if Trust Wallet is installed
*/
public installed(): Promise<boolean> {
const testUrl = this.app.scheme + TrustCommand.requestAccounts; // works for iOS and Android
return Linking.canOpenURL(testUrl);
}
/**
* request coin addresses
* @param request account request
* @returns {Promise<string>} signed transaction hash
*/
public requestAccounts(coins: CoinType[]): Promise<string[]> {
const request = new AccountsRequest(
coins,
this.genId("acc_"),
this.callbackScheme
);
return this.sendRequest(request).then((result) => {
return result.split(",");
});
}
/**
* sign a transaction
* @param request message request
* @returns {Promise<string>} signed transaction hash
*/
public signMessage(message: string, coin: CoinType): Promise<string> {
const request = new MessageRequest(
coin,
message,
this.genId("msg_"),
this.callbackScheme
);
return this.sendRequest(request);
}
/**
* sign a transaction
* @param request transaction request
* @returns {Promise<string>} signed transaction hash
*/
public signTransaction(
input: Object,
coin: CoinType,
send: boolean = false,
meta?: DAppMetadata
): Promise<string> {
if (Platform.OS === "android") {
return this.signAndroidTransaction(input, coin, send);
} else {
return this.signIOSTransaction(input, coin, send, meta);
}
}
private signIOSTransaction(
input: Object,
coin: CoinType,
send: boolean = false,
meta?: DAppMetadata
): Promise<string> {
let data = new Uint8Array(0);
switch (coin) {
case CoinType.ethereum:
let proto = TW.Ethereum.Proto.SigningInput.create(input);
data = TW.Ethereum.Proto.SigningInput.encode(proto).finish();
break;
default:
throw new Error("not implemented yet");
}
const request = new TransactionRequest(
coin,
Buffer.from(data).toString("base64"),
this.genId("tx_"),
send,
meta,
this.callbackScheme
);
return this.sendRequest(request);
}
private signAndroidTransaction(
input: Object,
coin: CoinType,
send: boolean
): Promise<string> {
let proto = TW.Ethereum.Proto.SigningInput.create(input);
switch (coin) {
case CoinType.ethereum:
const request = new AndroidTransactionRequest(
coin.toString(),
proto.toAddress,
this.deserializeBigInt(proto.amount) || "0",
this.callbackScheme,
send,
this.genId("tx_"),
this.deserializeBigInt(proto.nonce),
this.deserializeBigInt(proto.gasPrice),
this.deserializeBigInt(proto.gasLimit),
'0x' + Buffer.from(proto.payload).toString("hex")
);
return this.sendRequest(request);
default:
throw new Error("not implemented yet");
}
}
private deserializeBigInt(value?: Uint8Array): string | undefined {
if (!value || value.length === 0) {
return undefined;
}
return BigNumber.from(value).toString();
}
private genId(prefix?: string): string {
this.callbackId++;
return (prefix || "") + this.callbackId;
}
private sendRequest(request: Request): Promise<string> {
return this.installed().then((result) => {
return new Promise<string>((resolve, reject) => {
if (result) {
if (request.callbackScheme.length <= 0) {
// set default callback scheme
request.callbackScheme = this.callbackScheme;
}
// tracking resolve/reject by payload id
this.resolvers[request.id] = resolve;
this.rejectors[request.id] = reject;
const url = TrustCommand.getURL(request);
Linking.openURL(url);
} else {
reject({
error: TrustError.notInstalled,
message: TrustError.toString(TrustError.notInstalled),
});
}
});
});
}
private handleOpenURL(event: { url: string }) {
const response = TrustCommand.parseURL(event.url);
const resolver = this.resolvers[response.id];
const rejector = this.rejectors[response.id];
if (!resolver || !rejector) {
return;
}
if (response.error !== TrustError.none) {
rejector({
error: response.error,
message: TrustError.toString(response.error),
});
} else {
resolver(response.result);
}
delete this.resolvers[response.id];
delete this.rejectors[response.id];
}
}
export default TrustWallet;
export {
TrustCommand,
AccountsRequest,
MessageRequest,
TransactionRequest,
AndroidTransactionRequest,
CoinType,
TrustError,
DAppMetadata,
};