-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
62 lines (62 loc) · 1.84 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
const Rareterm = require('rareterm')
const mushie = require('mushie')
const fetch = require('node-fetch')
const FormData = require('form-data');
const sigUtil = require('eth-sig-util');
const priv2addr = require('ethereum-private-key-to-address')
const { v4: uuidv4 } = require('uuid');
class FD extends FormData {
append(key, blob) {
let uuid = uuidv4();
return super.append(key, blob, { filename: uuid })
}
}
class RaretermNode extends Rareterm {
async login (path) {
await this.wallet.switch(path)
await this.sync(this.wallet)
return this.wallet.address
}
async init(o) {
let maker = await mushie.maker()
let derivationPath = (o.key ? o.key : "m'/44'/60'/0'/0/0") // if there's no key, use the default ETH path
let self = this;
this.wallet = await maker.make({
key: derivationPath,
init: async (mushie) => {
mushie.address = await mushie.ethereum.request({
method: "eth_requestAccounts"
}).then((r) => {
return r[0]
})
},
use: {
ethereum: (key) => {
return {
request: async (o) => {
if (o.method === "eth_signTypedData_v4") {
return sigUtil.signTypedData_v4(key.privateKey, { data: JSON.parse(o.params[1]) });
} else if (o.method === "personal_sign") {
return sigUtil.personalSign(key.privateKey, { data: o.params[1] });
} else if (o.method === "eth_requestAccounts") {
return [priv2addr(key.privateKey)]
}
}
}
},
host: (key) => {
return o.host
},
http: (key) => {
return {
fetch,
FormData: FD
}
}
}
})
let account = await super.init(this.wallet)
return account
}
}
module.exports = RaretermNode