-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
117 lines (95 loc) · 3.52 KB
/
index.js
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
import nacl from 'tweetnacl';
import { deriveKeyPair } from './privateKey';
import { assertInput, assertConfirmation, assertAllStrings, assertIsString, assertIsBoolean, assertIsArray, bytesToHex, hexToBytes } from './utils';
import { renderGetPublicKey, renderSignTransaction, renderSignAllTransactions, renderSignMessage } from './ui';
module.exports.onRpcRequest = async ({ origin, request }) => {
if (
!origin ||
(
!origin.match(/^https:\/\/(?:\S+\.)?risewallet\.io$/) &&
!origin.match(/^https:\/\/(?:\S+\.)?risewallet\.dev$/)
)
) {
throw new Error('Invalid origin');
}
const dappOrigin = request?.params?.origin || origin;
const dappHost = (new URL(dappOrigin))?.host;
switch (request.method) {
case 'getPublicKey': {
const { derivationPath, confirm = false } = request.params || {};
assertInput(derivationPath);
assertIsString(derivationPath);
assertIsBoolean(confirm);
const keyPair = await deriveKeyPair(derivationPath);
const pubkey = bytesToHex(keyPair.publicKey);
if (confirm) {
const accepted = await renderGetPublicKey(dappHost, pubkey);
assertConfirmation(accepted);
}
return pubkey;
}
case 'signTransaction': {
const { derivationPath, message } = request.params || {};
assertInput(derivationPath);
assertIsString(derivationPath);
assertInput(message);
assertIsString(message);
const accepted = await renderSignTransaction(dappHost, message);
assertConfirmation(accepted);
const keyPair = await deriveKeyPair(derivationPath);
const messageBytes = hexToBytes(message);
const signature = nacl.sign.detached(messageBytes, keyPair.secretKey);
return {
publicKey: bytesToHex(keyPair.publicKey),
signature: bytesToHex(signature)
};
}
case 'signAllTransactions': {
const { derivationPath, messages } = request.params || {};
assertInput(derivationPath);
assertIsString(derivationPath);
assertInput(messages);
assertIsArray(messages);
assertInput(messages.length);
assertAllStrings(messages);
const accepted = await renderSignAllTransactions(dappHost, messages);
assertConfirmation(accepted);
const keyPair = await deriveKeyPair(derivationPath);
const signatures = messages
.map((message) => hexToBytes(message))
.map((message) => nacl.sign.detached(message, keyPair.secretKey))
.map((signature) => bytesToHex(signature));
return {
publicKey: bytesToHex(keyPair.publicKey),
signatures
};
}
case 'signMessage': {
const { derivationPath, message } = request.params || {};
assertInput(derivationPath);
assertIsString(derivationPath);
assertInput(message);
assertIsString(message);
const keyPair = await deriveKeyPair(derivationPath);
const messageBytes = hexToBytes(message);
let decodedMessage = '';
try {
decodedMessage = (new TextDecoder()).decode(messageBytes);
} catch (error) {
decodedMessage = 'Unable to decode message';
}
const accepted = await renderSignMessage(dappHost, decodedMessage);
assertConfirmation(accepted);
const signature = nacl.sign.detached(messageBytes, keyPair.secretKey);
return {
publicKey: bytesToHex(keyPair.publicKey),
signature: bytesToHex(signature)
};
}
default:
throw {
code: 4200,
message: 'The requested method is not supported.'
};
}
};