-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathexample.js
37 lines (31 loc) · 1.13 KB
/
example.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
const PluginEthereumPaychan = require('./plugin')
const Web3 = require('web3')
const promisify = require('util').promisify
async function main () {
const provider = new Web3.providers.HttpProvider('http://localhost:8545')
const web3 = new Web3(provider)
const accounts = await promisify(web3.eth.getAccounts)()
const senderAccount = accounts[0]
const receiverAccount = accounts[1]
const sender = new PluginEthereumPaychan({
account: senderAccount,
server: 'http://localhost:3000',
db: 'sender_db'
})
const receiver = new PluginEthereumPaychan({
account: receiverAccount,
port: 3000,
db: 'receiver_db'
})
receiver.registerDataHandler((buffer) => Promise.resolve(Buffer.alloc(32, 255)))
receiver.registerMoneyHandler((amount) => console.log(`receiver got: ${amount} unit of money`))
await receiver.connect()
await sender.connect()
const response = await sender.sendData(Buffer.alloc(32, 0))
console.log('receiver responded:', response.toString('hex'))
await sender.sendMoney(1)
await receiver.disconnect()
await sender.disconnect()
process.exit(0)
}
main().catch(err => console.log(err))