@psf/bch-js 5.3.2 → 6.1.0
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/.on-save.json +8 -0
- package/LICENSE.md +1 -2
- package/package.json +8 -10
- package/src/bch-js.js +0 -2
- package/src/psf-slp-indexer.js +9 -9
- package/src/slp/utils.js +0 -1241
- package/src/utxo.js +0 -270
- package/test/integration/chains/bchn/utxo-integration.js +9 -124
- package/test/integration/slp.js +0 -470
- package/test/unit/slp-utils.js +0 -724
- package/test/unit/utxo-unit.js +0 -131
- package/src/ipfs.js +0 -454
- package/test/integration/chains/abc/slp.js +0 -226
- package/test/integration/chains/bchn-no-wl-slpdb/rawtransaction.js +0 -71
- package/test/integration/chains/bchn-no-wl-slpdb/slp.js +0 -273
- package/test/integration/chains/bchn-no-wl-slpdb/utxo-integration.js +0 -105
- package/test/unit/ipfs.js +0 -443
package/test/unit/utxo-unit.js
CHANGED
|
@@ -15,137 +15,6 @@ describe('#utxo', () => {
|
|
|
15
15
|
beforeEach(() => (sandbox = sinon.createSandbox()))
|
|
16
16
|
afterEach(() => sandbox.restore())
|
|
17
17
|
|
|
18
|
-
describe('#getOld', () => {
|
|
19
|
-
it('should get hydrated and filtered UTXOs for an address', async () => {
|
|
20
|
-
// Mock dependencies.
|
|
21
|
-
sandbox.stub(bchjs.Utxo.electrumx, 'utxo').resolves(mockData.mockUtxoData)
|
|
22
|
-
sandbox
|
|
23
|
-
.stub(bchjs.Utxo.slp.Utils, 'hydrateUtxos')
|
|
24
|
-
.resolves(mockData.mockHydratedUtxos)
|
|
25
|
-
|
|
26
|
-
const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
|
|
27
|
-
|
|
28
|
-
const result = await bchjs.Utxo.getOld(addr)
|
|
29
|
-
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
30
|
-
|
|
31
|
-
assert.isArray(result)
|
|
32
|
-
assert.property(result[0], 'address')
|
|
33
|
-
assert.property(result[0], 'bchUtxos')
|
|
34
|
-
assert.property(result[0], 'nullUtxos')
|
|
35
|
-
assert.property(result[0], 'slpUtxos')
|
|
36
|
-
assert.isArray(result[0].bchUtxos)
|
|
37
|
-
assert.isArray(result[0].nullUtxos)
|
|
38
|
-
})
|
|
39
|
-
|
|
40
|
-
it('should catch and throw an error', async () => {
|
|
41
|
-
try {
|
|
42
|
-
// Force an error
|
|
43
|
-
sandbox
|
|
44
|
-
.stub(bchjs.Utxo.electrumx, 'utxo')
|
|
45
|
-
.rejects(new Error('test error'))
|
|
46
|
-
|
|
47
|
-
const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
|
|
48
|
-
|
|
49
|
-
await bchjs.Utxo.getOld(addr)
|
|
50
|
-
|
|
51
|
-
assert.fail('Unexpected code path')
|
|
52
|
-
} catch (err) {
|
|
53
|
-
assert.include(err.message, 'test error')
|
|
54
|
-
}
|
|
55
|
-
})
|
|
56
|
-
|
|
57
|
-
it('should handle an array of addresses', async () => {
|
|
58
|
-
// Mock dependencies.
|
|
59
|
-
sandbox.stub(bchjs.Utxo.electrumx, 'utxo').resolves({ utxos: {} })
|
|
60
|
-
sandbox
|
|
61
|
-
.stub(bchjs.Utxo.slp.Utils, 'hydrateUtxos')
|
|
62
|
-
.resolves(mockData.mockTwoHydratedAddrs)
|
|
63
|
-
|
|
64
|
-
const addr = [
|
|
65
|
-
'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9',
|
|
66
|
-
'bitcoincash:qqh793x9au6ehvh7r2zflzguanlme760wuzehgzjh9'
|
|
67
|
-
]
|
|
68
|
-
|
|
69
|
-
const result = await bchjs.Utxo.getOld(addr)
|
|
70
|
-
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
71
|
-
|
|
72
|
-
assert.isArray(result)
|
|
73
|
-
assert.equal(result.length, 2)
|
|
74
|
-
assert.property(result[0], 'address')
|
|
75
|
-
assert.property(result[0], 'bchUtxos')
|
|
76
|
-
assert.property(result[0], 'nullUtxos')
|
|
77
|
-
assert.property(result[0], 'slpUtxos')
|
|
78
|
-
assert.isArray(result[0].bchUtxos)
|
|
79
|
-
assert.isArray(result[0].nullUtxos)
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
it('should throw an error for array of 21 elements', async () => {
|
|
83
|
-
try {
|
|
84
|
-
const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
|
|
85
|
-
|
|
86
|
-
const addrs = []
|
|
87
|
-
for (let i = 0; i < 21; i++) {
|
|
88
|
-
addrs.push(addr)
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
await bchjs.Utxo.getOld(addrs)
|
|
92
|
-
|
|
93
|
-
assert.fail('Unexpected code path')
|
|
94
|
-
} catch (err) {
|
|
95
|
-
assert.include(err.message, 'Too many elements, 20 max.')
|
|
96
|
-
}
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
it('should handle NFTs and minting batons', async () => {
|
|
100
|
-
// Mock dependencies.
|
|
101
|
-
sandbox.stub(bchjs.Utxo.electrumx, 'utxo').resolves({ utxos: {} })
|
|
102
|
-
sandbox
|
|
103
|
-
.stub(bchjs.Utxo.slp.Utils, 'hydrateUtxos')
|
|
104
|
-
.resolves(mockData.mockEveryUtxoType)
|
|
105
|
-
|
|
106
|
-
const addr = 'simpleledger:qrm0c67wwqh0w7wjxua2gdt2xggnm90xwsr5k22euj'
|
|
107
|
-
|
|
108
|
-
const result = await bchjs.Utxo.getOld(addr)
|
|
109
|
-
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
110
|
-
|
|
111
|
-
assert.isArray(result)
|
|
112
|
-
assert.property(result[0], 'address')
|
|
113
|
-
assert.property(result[0], 'bchUtxos')
|
|
114
|
-
assert.property(result[0], 'nullUtxos')
|
|
115
|
-
assert.property(result[0], 'slpUtxos')
|
|
116
|
-
assert.isArray(result[0].bchUtxos)
|
|
117
|
-
assert.isArray(result[0].nullUtxos)
|
|
118
|
-
|
|
119
|
-
assert.isArray(result[0].slpUtxos.type1.mintBatons)
|
|
120
|
-
assert.isArray(result[0].slpUtxos.type1.tokens)
|
|
121
|
-
assert.isArray(result[0].slpUtxos.nft.groupMintBatons)
|
|
122
|
-
assert.isArray(result[0].slpUtxos.nft.groupTokens)
|
|
123
|
-
assert.isArray(result[0].slpUtxos.nft.tokens)
|
|
124
|
-
})
|
|
125
|
-
|
|
126
|
-
it('should use the whitelist when flag is set', async () => {
|
|
127
|
-
// Mock dependencies.
|
|
128
|
-
sandbox.stub(bchjs.Utxo.electrumx, 'utxo').resolves(mockData.mockUtxoData)
|
|
129
|
-
sandbox
|
|
130
|
-
.stub(bchjs.Utxo.slp.Utils, 'hydrateUtxosWL')
|
|
131
|
-
.resolves(mockData.mockHydratedUtxos)
|
|
132
|
-
|
|
133
|
-
const addr = 'simpleledger:qzv3zz2trz0xgp6a96lu4m6vp2nkwag0kvyucjzqt9'
|
|
134
|
-
const useWhitelist = true
|
|
135
|
-
|
|
136
|
-
const result = await bchjs.Utxo.getOld(addr, useWhitelist)
|
|
137
|
-
// console.log(`result: ${JSON.stringify(result, null, 2)}`)
|
|
138
|
-
|
|
139
|
-
assert.isArray(result)
|
|
140
|
-
assert.property(result[0], 'address')
|
|
141
|
-
assert.property(result[0], 'bchUtxos')
|
|
142
|
-
assert.property(result[0], 'nullUtxos')
|
|
143
|
-
assert.property(result[0], 'slpUtxos')
|
|
144
|
-
assert.isArray(result[0].bchUtxos)
|
|
145
|
-
assert.isArray(result[0].nullUtxos)
|
|
146
|
-
})
|
|
147
|
-
})
|
|
148
|
-
|
|
149
18
|
describe('#findBiggestUtxo', () => {
|
|
150
19
|
it('should throw error for non-array input', async () => {
|
|
151
20
|
try {
|
package/src/ipfs.js
DELETED
|
@@ -1,454 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
This library interacts with the IPFS REST API at FullStack.cash for uploading
|
|
3
|
-
and downloading data from the IPFS network.
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
const axios = require('axios')
|
|
7
|
-
const Uppy = require('@uppy/core')
|
|
8
|
-
const Tus = require('@uppy/tus')
|
|
9
|
-
const fs = require('fs')
|
|
10
|
-
|
|
11
|
-
let _this
|
|
12
|
-
|
|
13
|
-
class IPFS {
|
|
14
|
-
constructor (config) {
|
|
15
|
-
this.IPFS_API = process.env.IPFS_API
|
|
16
|
-
? process.env.IPFS_API
|
|
17
|
-
: 'https://ipfs-file-upload.fullstackcash.nl'
|
|
18
|
-
// : `http://localhost:5001`
|
|
19
|
-
|
|
20
|
-
// Default options when calling axios.
|
|
21
|
-
this.axiosOptions = {
|
|
22
|
-
headers: {
|
|
23
|
-
// authorization: `Token ${this.apiToken}`,
|
|
24
|
-
timeout: 15000 // Timeout if the server does not respond in time.
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
_this = this
|
|
29
|
-
|
|
30
|
-
_this.axios = axios
|
|
31
|
-
_this.fs = fs
|
|
32
|
-
|
|
33
|
-
// Initialize Uppy
|
|
34
|
-
_this.uppy = _this.initUppy()
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Initializes Uppy, which is used for file uploads.
|
|
38
|
-
initUppy () {
|
|
39
|
-
const uppy = Uppy({
|
|
40
|
-
allowMultipleUploads: false,
|
|
41
|
-
meta: { test: 'avatar' },
|
|
42
|
-
debug: false,
|
|
43
|
-
restrictions: {
|
|
44
|
-
maxFileSize: null,
|
|
45
|
-
maxNumberOfFiles: 1,
|
|
46
|
-
minNumberOfFiles: 1,
|
|
47
|
-
allowedFileTypes: null // type of files allowed to load
|
|
48
|
-
}
|
|
49
|
-
})
|
|
50
|
-
uppy.use(Tus, { endpoint: `${_this.IPFS_API}/uppy-files` })
|
|
51
|
-
|
|
52
|
-
return uppy
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* @api IPFS.createFileModelServer() createFileModelServer()
|
|
57
|
-
* @apiName createFileModelServer()
|
|
58
|
-
* @apiGroup IPFS
|
|
59
|
-
* @apiDescription Creates a new model on the server. If successful, will
|
|
60
|
-
* return an object with file data. That object contains a BCH address,
|
|
61
|
-
* payment amount, and _id required to be able to upload a file.
|
|
62
|
-
*
|
|
63
|
-
* @apiExample Example usage:
|
|
64
|
-
* (async () => {
|
|
65
|
-
* try {
|
|
66
|
-
* const path = `${__dirname}/ipfs.js`
|
|
67
|
-
* let fileData = await bchjs.IPFS.createFileModelServer(path);
|
|
68
|
-
* console.log(fileData);
|
|
69
|
-
* } catch(error) {
|
|
70
|
-
* console.error(error)
|
|
71
|
-
* }
|
|
72
|
-
* })()
|
|
73
|
-
*
|
|
74
|
-
* {
|
|
75
|
-
* "success": true,
|
|
76
|
-
* "hostingCostBCH": 0.00004197,
|
|
77
|
-
* "hostingCostUSD": 0.01,
|
|
78
|
-
* "file": {
|
|
79
|
-
* "payloadLink": "",
|
|
80
|
-
* "hasBeenPaid": false,
|
|
81
|
-
* "_id": "5ec562319bfacc745e8d8a52",
|
|
82
|
-
* "schemaVersion": 1,
|
|
83
|
-
* "size": 4458,
|
|
84
|
-
* "fileName": "ipfs.js",
|
|
85
|
-
* "fileExtension": "js",
|
|
86
|
-
* "createdTimestamp": "1589994033.655",
|
|
87
|
-
* "hostingCost": 4196,
|
|
88
|
-
* "walletIndex": 49,
|
|
89
|
-
* "bchAddr": "bchtest:qzrpkevu7h2ayfa4rjx08r5elvpfu72dg567x3mh3c",
|
|
90
|
-
* "__v": 0
|
|
91
|
-
* }
|
|
92
|
-
*/
|
|
93
|
-
async createFileModelServer (path) {
|
|
94
|
-
try {
|
|
95
|
-
// Ensure the file exists.
|
|
96
|
-
if (!_this.fs.existsSync(path)) {
|
|
97
|
-
throw new Error(`Could not find this file: ${path}`)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Read in the file.
|
|
101
|
-
const fileBuf = _this.fs.readFileSync(path)
|
|
102
|
-
// console.log(`fileBuf: `, fileBuf)
|
|
103
|
-
|
|
104
|
-
// console.log(`Buffer length: ${fileBuf.length}`)
|
|
105
|
-
|
|
106
|
-
// Get the file name from the path.
|
|
107
|
-
const splitPath = path.split('/')
|
|
108
|
-
const fileName = splitPath[splitPath.length - 1]
|
|
109
|
-
|
|
110
|
-
// Get the file extension.
|
|
111
|
-
const splitExt = fileName.split('.')
|
|
112
|
-
const fileExt = splitExt[splitExt.length - 1]
|
|
113
|
-
|
|
114
|
-
const fileObj = {
|
|
115
|
-
schemaVersion: 1,
|
|
116
|
-
size: fileBuf.length,
|
|
117
|
-
fileName: fileName,
|
|
118
|
-
fileExtension: fileExt
|
|
119
|
-
}
|
|
120
|
-
// console.log(`fileObj: ${JSON.stringify(fileObj, null, 2)}`)
|
|
121
|
-
|
|
122
|
-
const fileData = await _this.axios.post(`${this.IPFS_API}/files`, {
|
|
123
|
-
file: fileObj
|
|
124
|
-
})
|
|
125
|
-
// console.log(`fileData.data: ${JSON.stringify(fileData.data, null, 2)}`)
|
|
126
|
-
|
|
127
|
-
return fileData.data
|
|
128
|
-
} catch (err) {
|
|
129
|
-
console.error('Error in createFileModel()')
|
|
130
|
-
throw err
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
/**
|
|
135
|
-
* @api IPFS.uploadFileServer() uploadFileServer()
|
|
136
|
-
* @apiName uploadFile()
|
|
137
|
-
* @apiGroup IPFS
|
|
138
|
-
* @apiDescription Upload a file to the FullStack.cash IPFS server. If
|
|
139
|
-
* successful, it will return an object with an ID, a BCH address, and an
|
|
140
|
-
* amount of BCH to pay.
|
|
141
|
-
*
|
|
142
|
-
* @apiExample Example usage:
|
|
143
|
-
* (async () => {
|
|
144
|
-
* try {
|
|
145
|
-
* const path = `${__dirname}/ipfs.js`
|
|
146
|
-
* let fileData = await bchjs.IPFS.uploadFileServer(path, "5ec562319bfacc745e8d8a52");
|
|
147
|
-
* console.log(fileData);
|
|
148
|
-
* } catch(error) {
|
|
149
|
-
* console.error(error)
|
|
150
|
-
* }
|
|
151
|
-
* })()
|
|
152
|
-
*
|
|
153
|
-
* {
|
|
154
|
-
* "schemaVersion": 1,
|
|
155
|
-
* "size": 2374,
|
|
156
|
-
* "fileId": "5ec562319bfacc745e8d8a52",
|
|
157
|
-
* "fileName": "ipfs.js",
|
|
158
|
-
* "fileExtension": "js"
|
|
159
|
-
* }
|
|
160
|
-
*/
|
|
161
|
-
async uploadFileServer (path, modelId) {
|
|
162
|
-
try {
|
|
163
|
-
// Ensure the file exists.
|
|
164
|
-
if (!_this.fs.existsSync(path)) {
|
|
165
|
-
throw new Error(`Could not find this file: ${path}`)
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
if (!modelId) {
|
|
169
|
-
throw new Error(
|
|
170
|
-
'Must include a file model ID in order to upload a file.'
|
|
171
|
-
)
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
// Read in the file.
|
|
175
|
-
const fileBuf = _this.fs.readFileSync(path)
|
|
176
|
-
|
|
177
|
-
// Get the file name from the path.
|
|
178
|
-
const splitPath = path.split('/')
|
|
179
|
-
const fileName = splitPath[splitPath.length - 1]
|
|
180
|
-
|
|
181
|
-
// Prepare the upload object. Get a file ID from Uppy.
|
|
182
|
-
const id = _this.uppy.addFile({
|
|
183
|
-
name: fileName,
|
|
184
|
-
data: fileBuf,
|
|
185
|
-
source: 'Local',
|
|
186
|
-
isRemote: false
|
|
187
|
-
})
|
|
188
|
-
// console.log(`id: ${JSON.stringify(id, null, 2)}`)
|
|
189
|
-
|
|
190
|
-
// Add the model ID, required to be allowed to upload the file.
|
|
191
|
-
_this.uppy.setFileMeta(id, { fileModelId: modelId })
|
|
192
|
-
|
|
193
|
-
// Upload the file to the server.
|
|
194
|
-
const upData = await _this.uppy.upload()
|
|
195
|
-
|
|
196
|
-
if (upData.failed.length) {
|
|
197
|
-
throw new Error('The file could not be uploaded')
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
if (upData.successful.length) {
|
|
201
|
-
delete upData.successful[0].data
|
|
202
|
-
// console.log(`upData: ${JSON.stringify(upData, null, 2)}`)
|
|
203
|
-
|
|
204
|
-
const fileObj = {
|
|
205
|
-
schemaVersion: 1,
|
|
206
|
-
size: upData.successful[0].progress.bytesTotal,
|
|
207
|
-
fileId: upData.successful[0].meta.fileModelId,
|
|
208
|
-
fileName: upData.successful[0].name,
|
|
209
|
-
fileExtension: upData.successful[0].extension
|
|
210
|
-
}
|
|
211
|
-
// console.log(`fileObj: ${JSON.stringify(fileObj, null, 2)}`)
|
|
212
|
-
|
|
213
|
-
return fileObj
|
|
214
|
-
// return true
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
return false
|
|
218
|
-
} catch (err) {
|
|
219
|
-
console.error('Error in bch-js/src/ipfs.js/upload(): ')
|
|
220
|
-
throw err
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* @api IPFS.getStatus() getStatus()
|
|
226
|
-
* @apiName getStatus()
|
|
227
|
-
* @apiGroup IPFS
|
|
228
|
-
* @apiDescription Gets the status of the uploaded file. Will return an object
|
|
229
|
-
* that indicates the payment status. If the file is paid, it should contain
|
|
230
|
-
* an IPFS hash.
|
|
231
|
-
*
|
|
232
|
-
* @apiExample Example usage:
|
|
233
|
-
* (async () => {
|
|
234
|
-
* try {
|
|
235
|
-
* const modelId = "5ec7392c2acfe57aa62e945a"
|
|
236
|
-
* let fileData = await bchjs.IPFS.getStatus(modelId)
|
|
237
|
-
* console.log(fileData)
|
|
238
|
-
* } catch(error) {
|
|
239
|
-
* console.error(error)
|
|
240
|
-
* }
|
|
241
|
-
* })()
|
|
242
|
-
*
|
|
243
|
-
* {
|
|
244
|
-
* "hasBeenPaid": true,
|
|
245
|
-
* "satCost": 4403,
|
|
246
|
-
* "bchAddr": "bchtest:qz5z82u0suqh80x5tfx4ht8kdrkkw664vcy44uz0wk",
|
|
247
|
-
* "ipfsHash": "QmRDHPhY5hCNVRMVQvS2H9uty8P1skdwgLaHpUAkEvsjcE",
|
|
248
|
-
* "fileId": "5ec7392c2acfe57aa62e945a",
|
|
249
|
-
* "fileName": "ipfs-e2e.js"
|
|
250
|
-
* }
|
|
251
|
-
*/
|
|
252
|
-
async getStatus (modelId) {
|
|
253
|
-
try {
|
|
254
|
-
if (!modelId) throw new Error('Must include a file model ID.')
|
|
255
|
-
|
|
256
|
-
const fileData = await _this.axios.get(
|
|
257
|
-
`${this.IPFS_API}/files/${modelId}`
|
|
258
|
-
)
|
|
259
|
-
// console.log(`fileData.data: ${JSON.stringify(fileData.data, null, 2)}`)
|
|
260
|
-
|
|
261
|
-
const fileObj = {
|
|
262
|
-
hasBeenPaid: fileData.data.file.hasBeenPaid,
|
|
263
|
-
satCost: fileData.data.file.hostingCost,
|
|
264
|
-
bchAddr: fileData.data.file.bchAddr,
|
|
265
|
-
ipfsHash: fileData.data.file.payloadLink,
|
|
266
|
-
fileId: fileData.data.file._id,
|
|
267
|
-
fileName: fileData.data.file.fileName
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
return fileObj
|
|
271
|
-
} catch (err) {
|
|
272
|
-
console.error('Error in getStatus()')
|
|
273
|
-
throw err
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/**
|
|
278
|
-
* @api IPFS.createFileModelWeb() createFileModelWeb()
|
|
279
|
-
* @apiName createFileModelWeb()
|
|
280
|
-
* @apiGroup IPFS
|
|
281
|
-
* @apiDescription Creates a new model on the server. If successful, will
|
|
282
|
-
* return an object with file data. That object contains a BCH address,
|
|
283
|
-
* payment amount, and _id required to be able to upload a file.
|
|
284
|
-
*
|
|
285
|
-
* @apiExample Example usage:
|
|
286
|
-
* (async () => {
|
|
287
|
-
* try {
|
|
288
|
-
* const content = ['PSF']
|
|
289
|
-
* const name = 'psf.txt' // Content can be Array , ArrayBuffer , Blob
|
|
290
|
-
* const options = { type: "text/plain" }
|
|
291
|
-
* const file = new File(content,name,options)
|
|
292
|
-
* let fileData = await bchjs.IPFS.createFileModelWeb(file);
|
|
293
|
-
* console.log(fileData);
|
|
294
|
-
* } catch(error) {
|
|
295
|
-
* console.error(error)
|
|
296
|
-
* }
|
|
297
|
-
* })()
|
|
298
|
-
*
|
|
299
|
-
* {
|
|
300
|
-
* "success": true,
|
|
301
|
-
* "hostingCostBCH": 0.00004197,
|
|
302
|
-
* "hostingCostUSD": 0.01,
|
|
303
|
-
* "file": {
|
|
304
|
-
* "payloadLink": "",
|
|
305
|
-
* "hasBeenPaid": false,
|
|
306
|
-
* "_id": "5ec562319bfacc745e8d8a52",
|
|
307
|
-
* "schemaVersion": 1,
|
|
308
|
-
* "size": 4458,
|
|
309
|
-
* "fileName": "ipfs.js",
|
|
310
|
-
* "fileExtension": "js",
|
|
311
|
-
* "createdTimestamp": "1589994033.655",
|
|
312
|
-
* "hostingCost": 4196,
|
|
313
|
-
* "walletIndex": 49,
|
|
314
|
-
* "bchAddr": "bchtest:qzrpkevu7h2ayfa4rjx08r5elvpfu72dg567x3mh3c",
|
|
315
|
-
* "__v": 0
|
|
316
|
-
* }
|
|
317
|
-
*/
|
|
318
|
-
async createFileModelWeb (file) {
|
|
319
|
-
try {
|
|
320
|
-
if (!file) throw new Error('File is required')
|
|
321
|
-
|
|
322
|
-
const { name, size } = file
|
|
323
|
-
|
|
324
|
-
if (!name || typeof name !== 'string') {
|
|
325
|
-
throw new Error("File should have the property 'name' of string type")
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
if (!size || typeof size !== 'number') {
|
|
329
|
-
throw new Error("File should have the property 'size' of number type")
|
|
330
|
-
}
|
|
331
|
-
|
|
332
|
-
// Get the file extension.
|
|
333
|
-
const splitExt = name.split('.')
|
|
334
|
-
const fileExt = splitExt[splitExt.length - 1]
|
|
335
|
-
|
|
336
|
-
const fileObj = {
|
|
337
|
-
schemaVersion: 1,
|
|
338
|
-
size: size,
|
|
339
|
-
fileName: name,
|
|
340
|
-
fileExtension: fileExt
|
|
341
|
-
}
|
|
342
|
-
// console.log(`fileObj: ${JSON.stringify(fileObj, null, 2)}`)
|
|
343
|
-
|
|
344
|
-
const fileData = await _this.axios.post(`${this.IPFS_API}/files`, {
|
|
345
|
-
file: fileObj
|
|
346
|
-
})
|
|
347
|
-
// console.log(`fileData.data: ${JSON.stringify(fileData.data, null, 2)}`)
|
|
348
|
-
|
|
349
|
-
return fileData.data
|
|
350
|
-
} catch (err) {
|
|
351
|
-
console.error('Error in createFileModelWeb()')
|
|
352
|
-
throw err
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* @api IPFS.uploadFileWeb() uploadFileWeb()
|
|
358
|
-
* @apiName uploadFileWeb()
|
|
359
|
-
* @apiGroup IPFS
|
|
360
|
-
* @apiDescription Upload a file to the FullStack.cash IPFS server. If
|
|
361
|
-
* successful, it will return an object with an ID, a BCH address, and an
|
|
362
|
-
* amount of BCH to pay.
|
|
363
|
-
*
|
|
364
|
-
* @apiExample Example usage:
|
|
365
|
-
* (async () => {
|
|
366
|
-
* try {
|
|
367
|
-
* * const content = ['PSF']
|
|
368
|
-
* const name = 'psf.txt' // Content can be Array , ArrayBuffer , Blob
|
|
369
|
-
* const options = { type: "text/plain" }
|
|
370
|
-
* const file = new File(content,name,options)
|
|
371
|
-
* let fileData = await bchjs.IPFS.uploadFile(file, "5ec562319bfacc745e8d8a52");
|
|
372
|
-
* console.log(fileData);
|
|
373
|
-
* } catch(error) {
|
|
374
|
-
* console.error(error)
|
|
375
|
-
* }
|
|
376
|
-
* })()
|
|
377
|
-
*
|
|
378
|
-
* {
|
|
379
|
-
* "schemaVersion": 1,
|
|
380
|
-
* "size": 2374,
|
|
381
|
-
* "fileId": "5ec562319bfacc745e8d8a52",
|
|
382
|
-
* "fileName": "ipfs.js",
|
|
383
|
-
* "fileExtension": "js"
|
|
384
|
-
* }
|
|
385
|
-
*/
|
|
386
|
-
async uploadFileWeb (file, modelId) {
|
|
387
|
-
try {
|
|
388
|
-
if (!file) throw new Error('File is required')
|
|
389
|
-
|
|
390
|
-
const { name, type, size } = file
|
|
391
|
-
|
|
392
|
-
if (!name || typeof name !== 'string') {
|
|
393
|
-
throw new Error("File should have the property 'name' of string type")
|
|
394
|
-
}
|
|
395
|
-
|
|
396
|
-
if (!type || typeof type !== 'string') {
|
|
397
|
-
throw new Error("File should have the property 'type' of string type")
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
if (!size || typeof size !== 'number') {
|
|
401
|
-
throw new Error("File should have the property 'size' of number type")
|
|
402
|
-
}
|
|
403
|
-
|
|
404
|
-
if (!modelId) {
|
|
405
|
-
throw new Error(
|
|
406
|
-
'Must include a file model ID in order to upload a file.'
|
|
407
|
-
)
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
// Prepare the upload object. Get a file ID from Uppy.
|
|
411
|
-
const id = _this.uppy.addFile({
|
|
412
|
-
name: name,
|
|
413
|
-
data: file,
|
|
414
|
-
source: 'Local',
|
|
415
|
-
isRemote: false
|
|
416
|
-
})
|
|
417
|
-
// console.log(`id: ${JSON.stringify(id, null, 2)}`)
|
|
418
|
-
|
|
419
|
-
// Add the model ID, required to be allowed to upload the file.
|
|
420
|
-
_this.uppy.setFileMeta(id, { fileModelId: modelId })
|
|
421
|
-
|
|
422
|
-
// Upload the file to the server.
|
|
423
|
-
const upData = await _this.uppy.upload()
|
|
424
|
-
|
|
425
|
-
if (upData.failed.length) {
|
|
426
|
-
throw new Error('The file could not be uploaded')
|
|
427
|
-
}
|
|
428
|
-
|
|
429
|
-
if (upData.successful.length) {
|
|
430
|
-
delete upData.successful[0].data
|
|
431
|
-
// console.log(`upData: ${JSON.stringify(upData, null, 2)}`)
|
|
432
|
-
|
|
433
|
-
const fileObj = {
|
|
434
|
-
schemaVersion: 1,
|
|
435
|
-
size: upData.successful[0].progress.bytesTotal,
|
|
436
|
-
fileId: upData.successful[0].meta.fileModelId,
|
|
437
|
-
fileName: upData.successful[0].name,
|
|
438
|
-
fileExtension: upData.successful[0].extension
|
|
439
|
-
}
|
|
440
|
-
// console.log(`fileObj: ${JSON.stringify(fileObj, null, 2)}`)
|
|
441
|
-
|
|
442
|
-
return fileObj
|
|
443
|
-
// return true
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
return false
|
|
447
|
-
} catch (err) {
|
|
448
|
-
console.error('Error in bch-js/src/ipfs.js/uploadFileWeb(): ')
|
|
449
|
-
throw err
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
module.exports = IPFS
|