ETH

兼容web3

MYKEY兼容web3协议,您可以直接开发兼容web3协议的dapp,再通过mykey内置浏览器访问。也可以参考以下链接,了解更多eth web3协议的内容:

登录

使用web3.eth的givenProvider方法即可获得用户的账号信息。
1
{
2
var Web3 = require('web3');
3
var web3;
4
if (typeof web3 !== 'undefined') {
5
web3 = new Web3(web3.currentProvider);
6
} else {
7
// set the provider you want from Web3.providers
8
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545")); //这一步是连接到以太坊节点
9
}
10
var version = web3.version;
11
//解析web3.eth.givenProvider的返回,即可获得用户的ETH地址
12
console.log(JSON.stringify(web3.eth.givenProvider));
13
}
Copied!

验证MYKEY的签名

因为mykey账号的独特设计,客户端会使用Reserved公钥签名,服务端需要使用Reserved公钥验证签名。比如我们使用web3协议进行签名和验签:
1
let sigUtil = require('eth-sig-util')
2
let Web3 = require('web3');
3
let web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io/v3/56444e75b6a24070a374f791bd25f811"));
4
let json = require('./AccountStorage.abi.json');
5
let AccountStorageABI = json.abi
6
let AccountStorageAddr = '0xADc92d1fD878580579716d944eF3460E241604b7'
7
let AccountStorageIns = new web3.eth.Contract(AccountStorageABI, AccountStorageAddr);
8
9
// 1. get mykey account Reserved key
10
// https://docs.mykey.org/dive-into-mykey/mykey-on-eos#keydata%E8%A1%A8%E4%B8%AD%E7%9A%84%E5%AF%86%E9%92%A5
11
let account = '0x67913A00a459fCd41CbF4124a887e8d8dE0742c0'
12
13
// account proxy
14
let reservedKeyAddr = await AccountStorageIns.methods.getKeyData(account, 3).call();
15
console.log(account, "reserved key:", reservedKeyAddr) // should be 0xd2F9b4652D80FA870207C2b421B8437d7D54a484
16
// 2. sign message,
17
web3.personal.sign("hello", web3.eth.coinbase, console.log);
18
let message = 'hello'
19
let privKeyHex = '78e2219400da88378b746499ec8ff0d6aa97f806950276f28c65b9d569f32f84' // prvkey of '0xd2F9b4652D80FA870207C2b421B8437d7D54a484'
20
let privKey = Buffer.from(privKeyHex, 'hex')
21
let msgParams = { data: message }
22
let signed = sigUtil.personalSign(privKey, msgParams)
23
console.log("signature:", signed)
24
25
// 3. recover
26
msgParams.sig = signed
27
let recovered = sigUtil.recoverPersonalSignature(msgParams)
28
console.log("recovered:", recovered)
29
console.log(reservedKeyAddr.toLowerCase() === recovered.toLowerCase()
Copied!
Last modified 1yr ago