-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
83 lines (67 loc) · 2.34 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
import './polyfill'
import { createInstance } from '../../lib.esm/index.js'
import { EvmSigner } from '../../lib.esm/signers/EvmSigner'
import { CoinType } from '../../lib.esm/const'
import { typedDataFromMetamask } from './typedData'
import { BrowserProvider } from 'ethers'
const $ = document.querySelector.bind(document)
const $inputAccount = $('#input-account')
const $inputSubAccount = $('#input-sub-account')
const $buttonRecords = $('#btn-records')
const $buttonMint = $('#btn-mint')
const $buttonInfo = $('#btn-into')
const $buttonPersonalSign = $('#btn-personal-sign')
const $buttonSignTypedData = $('#btn-sign-typed-data')
const $codeOutput = $('#code-output')
async function main () {
const provider = new BrowserProvider(window.ethereum)
const signer = new EvmSigner(await provider.getSigner())
const dotbit = window.dotbit = createInstance({ signer })
const addresses = await window.ethereum.send('eth_requestAccounts')
const address = addresses[0]
dotbit.accountInfo($inputAccount.value).then(info => {
$codeOutput.innerHTML = JSON.stringify(info, null, 2)
})
$buttonInfo.addEventListener('click', () => {
const account = $inputAccount.value
dotbit.accountInfo(account).then(info => {
$codeOutput.innerHTML = JSON.stringify(info, null, 2)
})
})
$buttonRecords.addEventListener('click', () => {
const account = $inputAccount.value
dotbit.records(account).then(records => {
$codeOutput.innerHTML = JSON.stringify(records, null, 2)
})
})
$buttonMint.addEventListener('click', async (a) => {
try {
const account = $inputAccount.value
const subAccount = $inputSubAccount.value
const bitAccount = dotbit.account(account)
const res = await bitAccount.mintSubAccount({
account: subAccount,
keyInfo: {
coin_type: CoinType.ETH,
key: address,
},
registerYears: 1,
})
$codeOutput.innerHTML = JSON.stringify(res, null, 2)
}
catch (e) {
window.alert(e.message)
}
})
$buttonPersonalSign.addEventListener('click', async (a) => {
signer.signData('0xtest').then(res => {
$codeOutput.innerHTML = res
})
})
$buttonSignTypedData.addEventListener('click', async (a) => {
signer.signData(typedDataFromMetamask, true).then(res => {
$codeOutput.innerHTML = res
})
})
}
void main()