psffpp 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.editorconfig +30 -0
- package/.eslintrc.json +10 -0
- package/.on-save.json +8 -0
- package/.travis.yml +33 -0
- package/LICENSE.md +8 -0
- package/PEDIGREE.md +3 -0
- package/README.md +211 -0
- package/apidoc.json +3 -0
- package/examples/create-wallet.js +23 -0
- package/examples/list-tokens.js +26 -0
- package/examples/send-bch.js +60 -0
- package/examples/send-tokens.js +66 -0
- package/index.js +329 -0
- package/lib/nfts.js +113 -0
- package/lib/util.js +50 -0
- package/package.json +63 -0
- package/test/integration/main-index-integration.js +117 -0
- package/test/unit/main-index-unit.js +372 -0
- package/test/unit/mocks/main-index-mocks.js +290 -0
- package/test/unit/mocks/util-mocks.js +32 -0
- package/test/unit/nfts-unit.js +190 -0
- package/test/unit/util-unit.js +91 -0
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Unit tests for the nfts.js utility library.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// npm libraries
|
|
6
|
+
const chai = require('chai')
|
|
7
|
+
const sinon = require('sinon')
|
|
8
|
+
// const cloneDeep = require('lodash.clonedeep')
|
|
9
|
+
const SlpWallet = require('minimal-slp-wallet')
|
|
10
|
+
|
|
11
|
+
// Locally global variables.
|
|
12
|
+
const assert = chai.assert
|
|
13
|
+
|
|
14
|
+
// Mocking data libraries.
|
|
15
|
+
// const mockDataLib = require('./mocks/util-mocks')
|
|
16
|
+
|
|
17
|
+
// Unit under test
|
|
18
|
+
const NftsLib = require('../../lib/nfts')
|
|
19
|
+
|
|
20
|
+
describe('#NFTs', () => {
|
|
21
|
+
let sandbox
|
|
22
|
+
// let mockData
|
|
23
|
+
let uut
|
|
24
|
+
let wallet
|
|
25
|
+
|
|
26
|
+
before(async () => {
|
|
27
|
+
wallet = new SlpWallet(undefined, { interface: 'consumer-api' })
|
|
28
|
+
await wallet.walletInfoPromise
|
|
29
|
+
})
|
|
30
|
+
|
|
31
|
+
beforeEach(() => {
|
|
32
|
+
// Restore the sandbox before each test.
|
|
33
|
+
sandbox = sinon.createSandbox()
|
|
34
|
+
|
|
35
|
+
// Clone the mock data.
|
|
36
|
+
// mockData = cloneDeep(mockDataLib)
|
|
37
|
+
|
|
38
|
+
uut = new NftsLib({ wallet })
|
|
39
|
+
})
|
|
40
|
+
|
|
41
|
+
afterEach(() => sandbox.restore())
|
|
42
|
+
|
|
43
|
+
describe('#constructor', () => {
|
|
44
|
+
it('should throw an error if wallet is not passed', () => {
|
|
45
|
+
try {
|
|
46
|
+
uut = new NftsLib()
|
|
47
|
+
console.log('uut: ', uut)
|
|
48
|
+
|
|
49
|
+
assert.fail('Unexpected result')
|
|
50
|
+
} catch (err) {
|
|
51
|
+
assert.include(err.message, 'Instance of minimal-slp-wallet must be passed in as a property called \'wallet\', when initializing the nfts.js library.')
|
|
52
|
+
}
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('#getNftsFromGroup', () => {
|
|
57
|
+
it('should get NFT token IDs from a Group token', async () => {
|
|
58
|
+
// Mock dependencies and force desired code path
|
|
59
|
+
sandbox.stub(uut.wallet, 'getTokenData').resolves({
|
|
60
|
+
genesisData: {
|
|
61
|
+
nfts: ['a', 'b', 'c']
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
const result = await uut.getNftsFromGroup('fake-group-id')
|
|
66
|
+
|
|
67
|
+
assert.isArray(result)
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
it('should catch and throw errors', async () => {
|
|
71
|
+
try {
|
|
72
|
+
// Mock dependencies and force desired code path
|
|
73
|
+
sandbox.stub(uut.wallet, 'getTokenData').rejects(new Error('test error'))
|
|
74
|
+
|
|
75
|
+
await uut.getNftsFromGroup()
|
|
76
|
+
|
|
77
|
+
assert.fail('Unexpected code path')
|
|
78
|
+
} catch (err) {
|
|
79
|
+
assert.include(err.message, 'test error')
|
|
80
|
+
}
|
|
81
|
+
})
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
describe('#getAddrsFromNfts', () => {
|
|
85
|
+
it('should should return addresses associated with each NFT', async () => {
|
|
86
|
+
// Mock dependencies and force desired code path.
|
|
87
|
+
sandbox.stub(uut.wallet, 'getTokenData').resolves({
|
|
88
|
+
genesisData: {
|
|
89
|
+
nftHolder: 'sam'
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
const nfts = ['a']
|
|
94
|
+
|
|
95
|
+
const result = await uut.getAddrsFromNfts(nfts)
|
|
96
|
+
|
|
97
|
+
assert.isArray(result)
|
|
98
|
+
assert.equal(result[0], 'sam')
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('should catch and throw errors', async () => {
|
|
102
|
+
try {
|
|
103
|
+
// Force an error
|
|
104
|
+
sandbox.stub(uut.wallet, 'getTokenData').rejects(new Error('test error'))
|
|
105
|
+
|
|
106
|
+
const nfts = ['a']
|
|
107
|
+
|
|
108
|
+
await uut.getAddrsFromNfts(nfts)
|
|
109
|
+
|
|
110
|
+
assert.fail('Unexpected code path')
|
|
111
|
+
} catch (err) {
|
|
112
|
+
assert.include(err.message, 'test error')
|
|
113
|
+
}
|
|
114
|
+
})
|
|
115
|
+
|
|
116
|
+
it('should throw an error if Cash Stack server does not return nftHolder property', async () => {
|
|
117
|
+
try {
|
|
118
|
+
// Mock dependencies and force desired code path.
|
|
119
|
+
sandbox.stub(uut.wallet, 'getTokenData').resolves({
|
|
120
|
+
genesisData: {}
|
|
121
|
+
})
|
|
122
|
+
|
|
123
|
+
const nfts = ['a']
|
|
124
|
+
|
|
125
|
+
await uut.getAddrsFromNfts(nfts)
|
|
126
|
+
|
|
127
|
+
assert.fail('Unexpected code path')
|
|
128
|
+
} catch (err) {
|
|
129
|
+
assert.include(err.message, 'SLP indexer data does not include a holder address for this NFT.')
|
|
130
|
+
}
|
|
131
|
+
})
|
|
132
|
+
})
|
|
133
|
+
|
|
134
|
+
describe('#findKeys', () => {
|
|
135
|
+
it('should collect public keys for an addresses', async () => {
|
|
136
|
+
const addrs = ['bitcoincash:qzwahhjldv0qsecfxlmcenzvkjv9rlv9au2hcfggl6']
|
|
137
|
+
const nfts = ['fb707a9d8a4d6ba47ef0c510714ca46d4523cd29c8f4e3fd6a63a85edb8b05d2']
|
|
138
|
+
|
|
139
|
+
// Mock dependencies and force desired code path.
|
|
140
|
+
sandbox.stub(uut.wallet, 'getPubKey').resolves('02055962631b236ddcd2c17cd0b711f12438b93bcf01b206cadb351cc3e6e3e269')
|
|
141
|
+
|
|
142
|
+
const result = await uut.findKeys(addrs, nfts)
|
|
143
|
+
// console.log('result: ', result)
|
|
144
|
+
|
|
145
|
+
// Assert expected properties exist
|
|
146
|
+
assert.property(result, 'keys')
|
|
147
|
+
assert.property(result, 'keysNotFound')
|
|
148
|
+
|
|
149
|
+
// Assert that each property is an array.
|
|
150
|
+
assert.isArray(result.keys)
|
|
151
|
+
assert.isArray(result.keysNotFound)
|
|
152
|
+
|
|
153
|
+
// Assert expected values exist
|
|
154
|
+
assert.equal(result.keys[0].addr, 'bitcoincash:qzwahhjldv0qsecfxlmcenzvkjv9rlv9au2hcfggl6')
|
|
155
|
+
assert.equal(result.keys[0].pubKey, '02055962631b236ddcd2c17cd0b711f12438b93bcf01b206cadb351cc3e6e3e269')
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
it('should handle address without a public key', async () => {
|
|
159
|
+
const addrs = ['bitcoincash:qzwahhjldv0qsecfxlmcenzvkjv9rlv9au2hcfggl6']
|
|
160
|
+
const nfts = ['fb707a9d8a4d6ba47ef0c510714ca46d4523cd29c8f4e3fd6a63a85edb8b05d2']
|
|
161
|
+
|
|
162
|
+
// Mock dependencies and force desired code path.
|
|
163
|
+
sandbox.stub(uut.wallet, 'getPubKey').resolves('not found')
|
|
164
|
+
|
|
165
|
+
const result = await uut.findKeys(addrs, nfts)
|
|
166
|
+
// console.log('result: ', result)
|
|
167
|
+
|
|
168
|
+
// Assert expected properties exist
|
|
169
|
+
assert.property(result, 'keys')
|
|
170
|
+
assert.property(result, 'keysNotFound')
|
|
171
|
+
|
|
172
|
+
// Assert that each property is an array.
|
|
173
|
+
assert.isArray(result.keys)
|
|
174
|
+
assert.isArray(result.keysNotFound)
|
|
175
|
+
|
|
176
|
+
// Assert expected values exist
|
|
177
|
+
assert.equal(result.keysNotFound[0], 'bitcoincash:qzwahhjldv0qsecfxlmcenzvkjv9rlv9au2hcfggl6')
|
|
178
|
+
})
|
|
179
|
+
|
|
180
|
+
it('should catch and throw errors', async () => {
|
|
181
|
+
try {
|
|
182
|
+
await uut.findKeys()
|
|
183
|
+
|
|
184
|
+
assert.fail('Unexpected code path')
|
|
185
|
+
} catch (err) {
|
|
186
|
+
assert.include(err.message, 'Cannot read')
|
|
187
|
+
}
|
|
188
|
+
})
|
|
189
|
+
})
|
|
190
|
+
})
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Unit tests for the util.js utility library.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// npm libraries
|
|
6
|
+
const assert = require('chai').assert
|
|
7
|
+
const sinon = require('sinon')
|
|
8
|
+
// const cloneDeep = require('lodash.clonedeep')
|
|
9
|
+
const SlpWallet = require('minimal-slp-wallet')
|
|
10
|
+
|
|
11
|
+
// Local libraries
|
|
12
|
+
// const mockDataLib = require('./mocks/util-mocks')
|
|
13
|
+
|
|
14
|
+
// Unit under test
|
|
15
|
+
const UtilLib = require('../../lib/util')
|
|
16
|
+
|
|
17
|
+
describe('#Util', () => {
|
|
18
|
+
let sandbox
|
|
19
|
+
// let mockData
|
|
20
|
+
let uut
|
|
21
|
+
let wallet
|
|
22
|
+
|
|
23
|
+
before(async () => {
|
|
24
|
+
wallet = new SlpWallet(undefined, { interface: 'consumer-api' })
|
|
25
|
+
await wallet.walletInfoPromise
|
|
26
|
+
})
|
|
27
|
+
|
|
28
|
+
beforeEach(() => {
|
|
29
|
+
// Restore the sandbox before each test.
|
|
30
|
+
sandbox = sinon.createSandbox()
|
|
31
|
+
|
|
32
|
+
// Clone the mock data.
|
|
33
|
+
// mockData = cloneDeep(mockDataLib)
|
|
34
|
+
|
|
35
|
+
uut = new UtilLib({ wallet })
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
afterEach(() => sandbox.restore())
|
|
39
|
+
|
|
40
|
+
describe('#constructor', () => {
|
|
41
|
+
it('should throw an error if wallet is not passed', () => {
|
|
42
|
+
try {
|
|
43
|
+
uut = new UtilLib()
|
|
44
|
+
console.log('uut: ', uut)
|
|
45
|
+
|
|
46
|
+
assert.fail('Unexpected result')
|
|
47
|
+
} catch (err) {
|
|
48
|
+
assert.include(err.message, 'Instance of minimal-slp-wallet must be passed in as a property called \'wallet\', when initializing the util library.')
|
|
49
|
+
}
|
|
50
|
+
})
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
describe('#getTxData', () => {
|
|
54
|
+
it('should get TX data from the full node', async () => {
|
|
55
|
+
// Mock dependencies and force desired code path
|
|
56
|
+
sandbox.stub(uut.wallet, 'getTxData').resolves([{ txid: 'fake-txid' }])
|
|
57
|
+
|
|
58
|
+
const txid = 'fake-txid'
|
|
59
|
+
|
|
60
|
+
const result = await uut.getTxData(txid)
|
|
61
|
+
// console.log('result: ', result)
|
|
62
|
+
|
|
63
|
+
assert.equal(result.txid, 'fake-txid')
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
it('should get TX data from the cache', async () => {
|
|
67
|
+
// Mock dependencies and force desired code path
|
|
68
|
+
sandbox.stub(uut.wallet, 'getTxData').rejects(new Error('test error'))
|
|
69
|
+
uut.txCache['fake-txid'] = { txid: 'fake-txid' }
|
|
70
|
+
|
|
71
|
+
const txid = 'fake-txid'
|
|
72
|
+
|
|
73
|
+
const result = await uut.getTxData(txid)
|
|
74
|
+
// console.log('result: ', result)
|
|
75
|
+
|
|
76
|
+
// Assert expected result
|
|
77
|
+
assert.equal(result.txid, 'fake-txid')
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
it('should catch, report, and throw an error', async () => {
|
|
81
|
+
try {
|
|
82
|
+
await uut.getTxData()
|
|
83
|
+
|
|
84
|
+
assert.fail('Unexpected result')
|
|
85
|
+
} catch (err) {
|
|
86
|
+
// console.log('err.message: ', err.message)
|
|
87
|
+
assert.include(err.message, 'txid (transactoin ID) required')
|
|
88
|
+
}
|
|
89
|
+
})
|
|
90
|
+
})
|
|
91
|
+
})
|