forked from tgrospic/rnode-client-js
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eth-wrapper.js
44 lines (33 loc) · 1.39 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
37
38
39
40
41
42
43
44
// Metamask wrapper for Ethereum provider
// https://metamask.github.io/metamask-docs/guide/ethereum-provider.html#methods-new-api
// Updated by EIP-1193 (ethereum.request)
// https://eips.ethereum.org/EIPS/eip-1193
import { encodeBase16 } from "../lib"
// Ethereum object injected by Metamask
const eth_ = window.ethereum
export const ethDetected = !!eth_
// https://docs.metamask.io/guide/ethereum-provider.html#properties
if (ethDetected) eth_.autoRefreshOnNetworkChange = false
// Send a request to Ethereum API (Metamask)
const ethRequest = (method, args) => {
if (!eth_) throw Error(`Ethereum (Metamask) not detected.`)
return eth_.request({method, ...args})
}
// Request an address selected in Metamask
// - the first request will ask the user for permission
export const ethereumAddress = async () => {
const accounts = await ethRequest('eth_requestAccounts')
if (!Array.isArray(accounts))
throw Error(`Ethereum RPC response is not a list of accounts (${accounts}).`)
// Returns ETH address in hex format
return accounts[0]
}
// Ethereum personal signature
// https://github.com/ethereum/go-ethereum/wiki/Management-APIs#personal_sign
export const ethereumSign = async (bytes, ethAddr) => {
// Create args, fix arrays/buffers
const msg = encodeBase16(bytes)
const args = { params: [msg, ethAddr] }
// Returns signature in hex format
return await ethRequest('personal_sign', args)
}