-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
404 lines (361 loc) · 12.7 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
const { EventEmitter } = require('events');
const ethUtil = require('ethereumjs-util');
const {
concatSig,
SignTypedDataVersion,
typedSignatureHash,
TypedDataUtils,
} = require('@metamask/eth-sig-util');
const { TransactionFactory } = require('@ethereumjs/tx');
const {
gql,
ApolloClient,
createHttpLink,
InMemoryCache,
split,
} = require('@apollo/client/core');
const { GraphQLWsLink } = require('@apollo/client/link/subscriptions');
const { getMainDefinition } = require('@apollo/client/utilities');
const { createClient } = require('graphql-ws');
const { setContext } = require('@apollo/client/link/context');
const uuidv4 = require('uuid').v4;
const fetch = require('cross-fetch');
const type = 'Kevlar Co. MPC';
const baseAPIUrl = 'https://api-staging.kevlarco.com';
const httpLink = createHttpLink({
uri: `${baseAPIUrl}/graphql`,
credentials: 'include',
fetch
});
const [apiProtocol, apiPathname] = baseAPIUrl.split('://');
const CREATE_WALLET = gql`
mutation CreateWallet($data: CreateOneWalletInput!) {
createWallet(data: $data) {
id
name
address
currency
}
}
`;
const LIST_WALLETS = gql`
query ListWallets {
wallets {
id
name
currency
blockchain
address
keyQuorum {
address
}
}
}
`;
const SIGN_MESSAGE = gql`
mutation SignMessage($data: SignMessageInput!) {
signMessage(data: $data) {
... on Signature {
fullSig
r
s
v
publicKey
}
... on ErrorResponse {
message
}
}
}
`;
const hexes = Array.from({ length: 256 }, (v, i) => i.toString(16).padStart(2, '0'));
class WhaleKeyring extends EventEmitter {
constructor(accessToken) {
super();
this.type = type;
this.deserialize(accessToken);
const wsProtocol = apiProtocol === 'https' ? 'wss' : 'ws';
// @todo add authentication over WebSocket
const wsLink = new GraphQLWsLink(
createClient({
url: `${wsProtocol}://${apiPathname}/graphql`,
webSocketImpl: WebSocket
}),
);
const authLink = setContext(async (_, { headers }) => {
if (headers === undefined) headers = {};
headers.authorization = `Bearer ${this.accessToken}`;
return { headers };
});
// The split function takes three parameters:
//
// * A function that's called for each operation to execute
// * The Link to use for an operation if the function returns a "truthy" value
// * The Link to use for an operation if the function returns a "falsy" value
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink,
);
this.apolloClient = new ApolloClient({
cache: new InMemoryCache(),
link: authLink.concat(splitLink),
});
}
// Not really serializing anything but we'll call it this to keep things similar to MM
async serialize() {
return this.accessToken;
}
// Not really deserializing anything but we'll call it this to keep things similar to MM
async deserialize(accessToken) {
this.accessToken = accessToken;
}
async addAccounts(n = 1, names) {
const prevAccountCount = (await this.getAccounts()).length;
const newWallets = [];
for (let i = 0; i < n; i++) {
const res = await this.apolloClient.mutate({
mutation: CREATE_WALLET,
variables: {
data: {
sessionId: uuidv4(),
name: names !== undefined ? names[i] : `Account ${
prevAccountCount + 1 + i
} from ${new Date().toDateString()}`,
parties: 3,
threshold: 2,
blockchain: 'ETHEREUM',
currency: 'USD',
},
},
});
newWallets.push(res.data.createWallet.address);
}
this.newAccountsCache = this.newAccountsCache !== undefined ? this.newAccountsCache.concat(newWallets) : newWallets;
return newWallets;
}
async getAccounts() {
const res = await this.apolloClient.query({
query: LIST_WALLETS,
});
var wallets = res.data.wallets.map(({ address }) => address);
return this.newAccountsCache !== undefined ? wallets.concat(this.newAccountsCache.filter((item) => wallets.indexOf(item) < 0)) : wallets;
}
// tx is an instance of the ethereumjs-transaction class.
signTransaction(address, tx) {
let rawTxHex;
// transactions built with older versions of ethereumjs-tx have a
// getChainId method that newer versions do not. Older versions are mutable
// while newer versions default to being immutable. Expected shape and type
// of data for v, r and s differ (Buffer (old) vs BN (new))
if (typeof tx.getChainId === 'function') {
// In this version of ethereumjs-tx we must add the chainId in hex format
// to the initial v value. The chainId must be included in the serialized
// transaction which is only communicated to ethereumjs-tx in this
// value. In newer versions the chainId is communicated via the 'Common'
// object.
tx.v = ethUtil.bufferToHex(tx.getChainId());
tx.r = '0x00';
tx.s = '0x00';
rawTxHex = tx.serialize().toString('hex');
return this._signTransaction(address, rawTxHex, (payload) => {
tx.v = Buffer.from(payload.v, 'hex');
tx.r = Buffer.from(payload.r, 'hex');
tx.s = Buffer.from(payload.s, 'hex');
return tx;
});
}
// The below `encode` call is only necessary for legacy transactions, as `getMessageToSign`
// calls `rlp.encode` internally for non-legacy transactions. As per the "Transaction Execution"
// section of the ethereum yellow paper, transactions need to be "well-formed RLP, with no additional
// trailing bytes".
// Note also that `getMessageToSign` will return valid RLP for all transaction types, whereas the
// `serialize` method will not for any transaction type except legacy. This is because `serialize` includes
// empty r, s and v values in the encoded rlp. This is why we use `getMessageToSign` here instead of `serialize`.
const messageToSign = tx.getMessageToSign(false);
rawTxHex = Buffer.isBuffer(messageToSign)
? messageToSign.toString('hex')
: ethUtil.rlp.encode(messageToSign).toString('hex');
return this._signTransaction(address, rawTxHex, (payload) => {
// Because tx will be immutable, first get a plain javascript object that
// represents the transaction. Using txData here as it aligns with the
// nomenclature of ethereumjs/tx.
const txData = tx.toJSON();
// The fromTxData utility expects a type to support transactions with a type other than 0
txData.type = tx.type;
// The fromTxData utility expects v,r and s to be hex prefixed
txData.v = ethUtil.addHexPrefix(payload.v);
txData.r = ethUtil.addHexPrefix(payload.r);
txData.s = ethUtil.addHexPrefix(payload.s);
// Adopt the 'common' option from the original transaction and set the
// returned object to be frozen if the original is frozen.
return TransactionFactory.fromTxData(txData, {
common: tx.common,
freeze: Object.isFrozen(tx),
});
});
}
_signTransaction(address, rawTxHex, handleSigning) {
return new Promise((resolve, reject) => {
this._signData(address, this.bytesToHex(ethUtil.keccak(Buffer.from(rawTxHex, 'hex'), 256))).then(function (payload) {
const newOrMutatedTx = handleSigning(payload);
const valid = newOrMutatedTx.verifySignature();
if (valid) {
resolve(newOrMutatedTx);
} else {
reject(
new Error(
'Kevlar Co. MPC: The transaction signature is not valid',
),
);
}
});
});
}
_signData(address, message) {
return this.apolloClient.mutate({
mutation: SIGN_MESSAGE,
variables: {
data: {
walletAddress: address, // TODO: Make API case-insensitive
content: message.substring(0, 2) === "0x" ? message : "0x" + message
},
},
});
}
// For eth_sign, we need to sign arbitrary data:
async signMessage(address, data, _opts = {}) {
const res = await this._signData(address, this.bytesToHex(ethUtil.keccak(Buffer.from(data, 'hex'), 256)));
const rawMsgSig = concatSig(res.data.signMessage.v, res.data.signMessage.r, res.data.signMessage.s);
return rawMsgSig;
}
bytesToHex(uint8a) {
// pre-caching improves the speed 6x
if (!(uint8a instanceof Uint8Array)) throw new Error('Uint8Array expected');
let hex = '';
for (let i = 0; i < uint8a.length; i++) {
hex += hexes[uint8a[i]];
}
return hex;
}
// For personal_sign, we need to prefix the message:
async signPersonalMessage(address, msgHex, _opts = {}) {
const msgHashHex = ethUtil.bufferToHex(
ethUtil.hashPersonalMessage(Buffer.from(msgHex, 'hex')),
);
const msgSig = await this._signData(address, msgHashHex);
const rawMsgSig = concatSig(msgSig.v, msgSig.r, msgSig.s);
return rawMsgSig;
}
// For eth_decryptMessage:
async decryptMessage(_withAccount, _encryptedData) {
throw new Error(
"decryptMessage is not implemented in Kevlar Co.'s WhaleKeyring.",
);
}
// personal_signTypedData, signs data along with the schema
signTypedData(
withAccount,
typedData,
opts = { version: SignTypedDataVersion.V1 },
) {
// Treat invalid versions as "V1"
const version = Object.keys(SignTypedDataVersion).includes(opts.version)
? opts.version
: SignTypedDataVersion.V1;
return this._signTypedData({
address: withAccount,
data: typedData,
version,
});
}
/**
* Sign typed data according to EIP-712. The signing differs based upon the `version`.
*
* V1 is based upon [an early version of EIP-712](https://github.com/ethereum/EIPs/pull/712/commits/21abe254fe0452d8583d5b132b1d7be87c0439ca)
* that lacked some later security improvements, and should generally be neglected in favor of
* later versions.
*
* V3 is based on [EIP-712](https://eips.ethereum.org/EIPS/eip-712), except that arrays and
* recursive data structures are not supported.
*
* V4 is based on [EIP-712](https://eips.ethereum.org/EIPS/eip-712), and includes full support of
* arrays and recursive data structures.
*
* @param options - The signing options.
* @param options.privateKey - The private key to sign with.
* @param options.data - The typed data to sign.
* @param options.version - The signing version to use.
* @returns The '0x'-prefixed hex encoded signature.
*/
async _signTypedData({ address, data, version }) {
this.validateVersion(version);
if (this.isNullish(data)) {
throw new Error('Missing data parameter');
} else if (this.isNullish(address)) {
throw new Error('Missing private key parameter');
}
const messageHash =
version === SignTypedDataVersion.V1
? Buffer.from(typedSignatureHash(data), 'hex')
: TypedDataUtils.eip712Hash(data, version);
const sig = await this._signData(address, messageHash);
return concatSig(ethUtil.toBuffer(sig.v), sig.r, sig.s);
}
/**
* Validate that the given value is a valid version string.
*
* @param version - The version value to validate.
* @param allowedVersions - A list of allowed versions. If omitted, all versions are assumed to be
* allowed.
*/
validateVersion(
version,
allowedVersions
) {
if (!Object.keys(SignTypedDataVersion).includes(version)) {
throw new Error(`Invalid version: '${version}'`);
} else if (allowedVersions && !allowedVersions.includes(version)) {
throw new Error(
`SignTypedDataVersion not allowed: '${version}'. Allowed versions are: ${allowedVersions.join(
', ',
)}`,
);
}
}
/**
* Returns `true` if the given value is nullish.
*
* @param value - The value being checked.
* @returns Whether the value is nullish.
*/
isNullish(value) {
return value === null || value === undefined;
}
// get public key for nacl
async getEncryptionPublicKey(_withAccount, _opts = {}) {
throw new Error(
"getEncryptionPublicKey is not implemented in Kevlar Co.'s WhaleKeyring.",
);
}
// returns an address specific to an app
async getAppKeyAddress(_address, _origin) {
throw new Error(
"getAppKeyAddress is not implemented in Kevlar Co.'s WhaleKeyring.",
);
}
// exportAccount should return a hex-encoded private key:
async exportAccount(address, opts = {}) {
throw new Error(
"exportAccount is not implemented in Kevlar Co.'s WhaleKeyring.",
);
}
}
WhaleKeyring.type = type;
module.exports = WhaleKeyring;