This repository has been archived by the owner on May 10, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 86
/
uportWeb3.js
148 lines (136 loc) · 4.4 KB
/
uportWeb3.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { expect } from 'chai'
import Web3 from 'web3'
import { Connect } from './uport-connect'
import Autosigner from '../utils/autosigner'
import testData from './testData.json'
// import TestRPC from 'ethereumjs-testrpc'
const addr1 = '0x9d00733ae37f34cdebe443e5cda8e9721fffa092'
function mockCredentials (receive) {
return {
settings: {},
receive
}
}
describe('uportWeb3 integration tests', function () {
this.timeout(30000)
let autosigner, status, vanillaWeb3, web3
const coolStatus = 'Writing some tests!'
before(done => {
global.navigator = {}
// const testrpcProv = TestRPC.provider()
const testrpcProv = new Web3.providers.HttpProvider('http://localhost:8545')
vanillaWeb3 = new Web3(testrpcProv)
// Create Autosigner
Autosigner.load(testrpcProv, (err, as) => {
if (err) {
console.log('error loading autosigner')
console.log(err)
return done()
}
autosigner = as
vanillaWeb3.eth.getAccounts((err1, accounts) => {
if (err1) {
console.log(err1)
return done()
}
// Create status contract
const statusContractABI = vanillaWeb3.eth.contract(testData.statusContractAbiData)
statusContractABI.new({
data: testData.statusContractBin,
from: accounts[0],
gas: 3000000
},
(err2, contract) => {
if (err2) {
console.log(err2)
return done()
}
if (!contract.address) return
// console.log(contract)
// Send ether to Autosigner
vanillaWeb3.eth.sendTransaction({from: accounts[0], to: autosigner.address, value: vanillaWeb3.toWei(90)}, (err3, r) => {
if (err3) {
console.log(err3)
return done()
}
// Change provider
// Autosigner is a qrDisplay
// that automatically signs transactions
const uport = new Connect('Integration Tests', {
credentials: mockCredentials(() => ({ address: autosigner.address })),
provider: testrpcProv,
uriHandler: autosigner.openQr.bind(autosigner)
})
web3 = uport.getWeb3()
status = web3.eth.contract(testData.statusContractAbiData).at(contract.address)
done()
})
})
})
})
})
it('getCoinbase', (done) => {
web3.eth.getCoinbase((err, address) => {
expect(err).to.be.null
expect(address).to.equal(autosigner.address)
done()
})
})
it('getAccounts', (done) => {
web3.eth.getAccounts((err, addressList) => {
expect(err).to.be.null
expect(addressList).to.deep.equal([autosigner.address])
done()
})
})
it('sendTransaction', (done) => {
web3.eth.sendTransaction({value: web3.toWei(2), to: addr1}, (err, txHash) => {
expect(err).to.be.null
expect(txHash).to.be
web3.eth.getBalance(addr1, (err, balance) => {
expect(err).to.be.null
expect(balance.toString()).to.equal(web3.toWei(2))
done()
})
})
})
it('use contract', (done) => {
status.updateStatus(coolStatus, (err, res) => {
expect(err).to.be.null
if (err) {
console.log(err.message)
return done()
}
expect(res).to.be
web3.eth.getTransactionReceipt(res, (err, tx) => {
expect(tx.blockNumber).to.be
status.getStatus.call(autosigner.address, (err, myStatus) => {
expect(err).to.be.null
expect(myStatus).to.be.equal(coolStatus)
done()
})
})
})
})
it('handles batches', (done) => {
var batch = web3.createBatch()
batch.add(web3.eth.getBalance.request(autosigner.address, 'latest', (error, balance) => {
expect(error).to.be.null
expect(balance).to.be.greaterThan(0)
}))
batch.add(web3.eth.getBalance.request(autosigner.address, 'latest', (error, balance) => {
expect(error).to.be.null
expect(balance).to.be.greaterThan(0)
}))
batch.add(status.getStatus.request(autosigner.address, (error, myStatus) => {
expect(error).to.be.null
expect(myStatus).to.be.equal(coolStatus)
}))
batch.execute()
setTimeout(done, 1000)
})
it('does not handle sync calls', (done) => {
expect(() => web3.eth.getBalance(autosigner.address)).to.throw('Uport Web3 SubProvider does not support synchronous requests.');
done()
})
})