forked from tgrospic/rnode-client-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheth-wrapper.js
36 lines (27 loc) · 1.07 KB
/
eth-wrapper.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
// Metamask wrapper for Ethereum provider
// https://metamask.github.io/metamask-docs/guide/ethereum-provider.html#methods-new-api
// Ethereum object injected by Metamask
const eth_ = window.ethereum
export const ethDetected = !!eth_
// Send a request to Ethereum API (Metamask)
const ethSend = (method, args) => {
if (!eth_) throw Error(`Ethereum (Metamask) not detected.`)
return eth_.send(method, args).then(x => x.result)
}
// Request an address from the user
// - the first request will ask the user for permission
// - in Metamask settings the connection can be deleted
export const ethereumAddress = async () => {
const accounts = await ethSend('eth_requestAccounts')
if (!Array.isArray(accounts))
throw Error(`Ethereum RPC response is not a list of accounts.`)
// Returns ETH address in hex format
return accounts[0]
}
// Ethereum personal signature
export const ethereumSign = async (data, ethAddr) => {
// Create args, fix arrays/buffers
const args = [[...data], ethAddr]
// Returns signature in hex format
return await ethSend('personal_sign', args)
}