-
Notifications
You must be signed in to change notification settings - Fork 38
/
hashconnect.ts
303 lines (220 loc) · 11 KB
/
hashconnect.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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import { Event } from "ts-typed-events";
import { IRelay, WebSocketRelay } from "./types/relay";
import { MessageUtil, MessageHandler, MessageTypes, RelayMessage, RelayMessageType } from "./message"
import { HashConnectTypes, IHashConnect } from "./types/hashconnect";
global.Buffer = global.Buffer || require('buffer').Buffer;
/**
* Main interface with hashpack
*/
export class HashConnect implements IHashConnect {
relay: IRelay;
// events
foundExtensionEvent: Event<HashConnectTypes.WalletMetadata>;
pairingEvent: Event<MessageTypes.ApprovePairing>;
transactionEvent: Event<MessageTypes.Transaction>;
transactionResponseEvent: Event<MessageTypes.TransactionResponse>;
acknowledgeMessageEvent: Event<MessageTypes.Acknowledge>;
additionalAccountRequestEvent: Event<MessageTypes.AdditionalAccountRequest>;
additionalAccountResponseEvent: Event<MessageTypes.AdditionalAccountResponse>;
// messages util
messageParser: MessageHandler;
messages: MessageUtil;
private metadata!: HashConnectTypes.AppMetadata | HashConnectTypes.WalletMetadata;
publicKeys: Record<string, string> = {};
private privateKey: string;
debug: boolean = false;
constructor(debug?: boolean) {
this.relay = new WebSocketRelay(this);
this.foundExtensionEvent = new Event<HashConnectTypes.WalletMetadata>();
this.pairingEvent = new Event<MessageTypes.ApprovePairing>();
this.transactionEvent = new Event<MessageTypes.Transaction>();
this.transactionResponseEvent = new Event<MessageTypes.TransactionResponse>();
this.acknowledgeMessageEvent = new Event<MessageTypes.Acknowledge>();
this.additionalAccountRequestEvent = new Event<MessageTypes.AdditionalAccountRequest>();
this.additionalAccountResponseEvent = new Event<MessageTypes.AdditionalAccountResponse>();
this.messages = new MessageUtil();
this.messageParser = new MessageHandler();
if (debug) this.debug = debug;
this.setupEvents();
}
async init(metadata: HashConnectTypes.AppMetadata | HashConnectTypes.WalletMetadata, privKey?: string): Promise<HashConnectTypes.InitilizationData> {
return new Promise(async (resolve) => {
this.metadata = metadata;
if (this.debug) console.log("hashconnect - Initializing")
if (!privKey)
this.privateKey = await this.generateEncryptionKeys();
else
this.privateKey = privKey;
metadata.publicKey = this.privateKey;
let initData: HashConnectTypes.InitilizationData = {
privKey: this.privateKey
}
if (window)
this.metadata.url = window.location.origin;
await this.relay.init();
if (this.debug) console.log("hashconnect - Initialized")
resolve(initData);
});
}
async connect(topic?: string, metadataToConnect?: HashConnectTypes.AppMetadata | HashConnectTypes.WalletMetadata): Promise<HashConnectTypes.ConnectionState> {
if (!topic) {
topic = this.messages.createRandomTopicId();
this.publicKeys[topic] = this.privateKey;
if (this.debug) console.log("hashconnect - Created new topic id - " + topic);
}
if (metadataToConnect)
this.publicKeys[topic] = metadataToConnect.publicKey as string;
let state: HashConnectTypes.ConnectionState = {
topic: topic,
expires: 0
}
await this.relay.subscribe(state.topic);
return state;
}
/**
* Set up event connections
*/
private setupEvents() {
// This will listen for a payload emission from the relay
if (this.debug) console.log("hashconnect - Setting up events");
this.relay.payload.on(async (payload) => {
if (!payload) return;
//this is redundant until protobuffs are re-implemented
const message: RelayMessage = await this.messages.decode(payload, this);
await this.messageParser.onPayload(message, this);
})
}
/**
* Send functions
*/
async sendTransaction(topic: string, transaction: MessageTypes.Transaction): Promise<string> {
transaction.byteArray = Buffer.from(transaction.byteArray).toString("base64");
const msg = await this.messages.prepareSimpleMessage(RelayMessageType.Transaction, transaction, topic, this);
await this.relay.publish(topic, msg, this.publicKeys[topic]);
this.sendEncryptedLocalTransaction(msg);
return transaction.id!;
}
async requestAdditionalAccounts(topic: string, message: MessageTypes.AdditionalAccountRequest): Promise<string> {
const msg = await this.messages.prepareSimpleMessage(RelayMessageType.AdditionalAccountRequest, message, topic, this);
await this.relay.publish(topic, msg, this.publicKeys[topic]);
return message.id!;
}
async sendAdditionalAccounts(topic: string, message: MessageTypes.AdditionalAccountResponse): Promise<string> {
message.accountIds = message.accountIds.map(id => { return id });
const msg = await this.messages.prepareSimpleMessage(RelayMessageType.AdditionalAccountResponse, message, topic, this);
await this.relay.publish(topic, msg, this.publicKeys[topic]);
return message.id!;
}
async sendTransactionResponse(topic: string, message: MessageTypes.TransactionResponse): Promise<string> {
if (message.receipt) message.receipt = Buffer.from(message.receipt).toString("base64");
if (message.signedTransaction) message.signedTransaction = Buffer.from(message.signedTransaction).toString("base64");
const msg = await this.messages.prepareSimpleMessage(RelayMessageType.TransactionResponse, message, topic, this);
await this.relay.publish(topic, msg, this.publicKeys[topic]);
return message.id!;
}
async pair(pairingData: HashConnectTypes.PairingData, accounts: string[], network: string): Promise<HashConnectTypes.ConnectionState> {
if (this.debug) console.log("hashconnect - Pairing to " + pairingData.metadata.name);
let state = await this.connect(pairingData.topic);
let msg: MessageTypes.ApprovePairing = {
metadata: this.metadata as HashConnectTypes.WalletMetadata,
topic: pairingData.topic,
accountIds: accounts,
network: network
}
msg.metadata.description = this.sanitizeString(msg.metadata.description);
msg.metadata.name = this.sanitizeString(msg.metadata.name);
msg.metadata.publicKey = pairingData.metadata.publicKey;
msg.network = this.sanitizeString(msg.network);
msg.metadata.url = this.sanitizeString(msg.metadata.url!);
msg.accountIds = msg.accountIds.map(id => { return id });
this.publicKeys[pairingData.topic] = pairingData.metadata.publicKey as string;
const payload = await this.messages.prepareSimpleMessage(RelayMessageType.ApprovePairing, msg, msg.topic, this)
this.relay.publish(pairingData.topic, payload, this.publicKeys[pairingData.topic])
return state;
}
async reject(topic: string, reason: string, msg_id: string) {
let reject: MessageTypes.Rejected = {
reason: reason,
topic: topic,
msg_id: msg_id
}
reject.reason = this.sanitizeString(reject.reason!);
// create protobuf message
const msg = await this.messages.prepareSimpleMessage(RelayMessageType.RejectPairing, reject, topic, this)
// Publish the rejection
await this.relay.publish(topic, msg, this.publicKeys[topic]);
}
async acknowledge(topic: string, pubKey: string, msg_id: string) {
const ack: MessageTypes.Acknowledge = {
result: true,
topic: topic,
msg_id: msg_id
}
const ackPayload = await this.messages.prepareSimpleMessage(RelayMessageType.Acknowledge, ack, topic, this);
await this.relay.publish(topic, ackPayload, pubKey)
}
/**
* Helpers
*/
generatePairingString(state: HashConnectTypes.ConnectionState, network: string, multiAccount: boolean): string {
if (this.debug) console.log("hashconnect - Generating pairing string");
let data: HashConnectTypes.PairingData = {
metadata: this.metadata,
topic: state.topic,
network: network,
multiAccount: multiAccount
}
data.metadata.description = this.sanitizeString(data.metadata.description);
data.metadata.name = this.sanitizeString(data.metadata.name);
data.network = this.sanitizeString(data.network);
data.metadata.url = this.sanitizeString(data.metadata.url!);
let pairingString: string = Buffer.from(JSON.stringify(data)).toString("base64")
return pairingString;
}
decodePairingString(pairingString: string) {
let json_string: string = Buffer.from(pairingString, 'base64').toString();
let data: HashConnectTypes.PairingData = JSON.parse(json_string);
// data.metadata.publicKey = Buffer.from(data.metadata.publicKey as string, 'base64');
return data;
}
private async generateEncryptionKeys(): Promise<string> { //https://github.com/diafygi/webcrypto-examples/#rsa-oaep---encrypt
let key = this.messages.createRandomTopicId()
if (this.debug) console.log("hashconnect - Generated new encryption key - " + key);
return key;
}
private sanitizeString(str: string) {
return str.replace(/[^\w. ]/gi, function (c) {
if (c == ".") return ".";
return '&#' + c.charCodeAt(0) + ';';
});
}
/**
* Local wallet stuff
*/
findLocalWallets() {
if (this.debug) console.log("hashconnect - Finding local wallets");
window.addEventListener("message", (event) => {
if (event.data.type && (event.data.type == "hashconnect-query-extension-response")) {
if (this.debug) console.log("hashconnect - Local wallet metadata recieved", event.data);
if (event.data.metadata)
this.foundExtensionEvent.emit(event.data.metadata);
}
}, false);
setTimeout(() => {
window.postMessage({ type: "hashconnect-query-extension" }, "*");
}, 50);
}
connectToLocalWallet(pairingString: string) {
if (this.debug) console.log("hashconnect - Connecting to local wallet")
//todo: add extension metadata support
window.postMessage({ type: "hashconnect-connect-extension", pairingString: pairingString }, "*")
}
sendEncryptedLocalTransaction(message: string) {
if (this.debug) console.log("hashconnect - sending local transaction", message);
window.postMessage({ type: "hashconnect-send-local-transaction", message: message }, "*")
}
async decodeLocalTransaction(message: string){
const local_message: RelayMessage = await this.messages.decode(message, this);
return local_message;
}
}