Update ipfs-http-client (for node >= 14)

Updates the client library and API usage to work with the latest
version, and more importantly, with current node.js LTS.
This commit is contained in:
Basti 2022-04-28 19:19:58 +02:00
parent 16d5704173
commit 53fafc1c78
Signed by untrusted user: basti
GPG Key ID: 9F88009D31D99C72
5 changed files with 19954 additions and 5600 deletions

View File

@ -1,8 +1,8 @@
{ {
"1337": { "1337": {
"Contributor": "0xB0D4afd8879eD9F52b28595d31B441D079B2Ca07", "Contributor": "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0",
"Contribution": "0x922D6956C99E12DFeB3224DEA977D0939758A1Fe", "Contribution": "0xDc64a140Aa3E981100a9becA4E685f962f0cF6C9",
"Token": "0x5081a39b8A5f0E35a8D959395a630b68B74Dd30f", "Token": "0x0165878A594ca255338adfa4d48449f69242Eb8F",
"Reimbursement": "0x1fA02b2d6A771842690194Cf62D91bdd92BfE28d" "Reimbursement": "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6"
} }
} }

View File

@ -14,15 +14,6 @@ class Base {
return this.contract.address; return this.contract.address;
} }
get ipfs () {
if (!this._ipfsAPI) { throw new Error('IPFS API not configured; please set an ipfs instance'); }
return this._ipfsAPI;
}
set ipfs (ipfsAPI) {
this._ipfsAPI = ipfsAPI;
}
on (type, callback) { on (type, callback) {
return this.contract.on(type, callback); return this.contract.on(type, callback);
} }

View File

@ -8,10 +8,10 @@ class IPFS {
config = { host: 'localhost', port: '5001', protocol: 'http' }; config = { host: 'localhost', port: '5001', protocol: 'http' };
} }
this._config = config; this._config = config;
this._ipfsAPI = ipfsClient(config); this._ipfsAPI = ipfsClient.create(config);
} }
catAndMerge (contractData, deserialize) { async catAndMerge (contractData, deserialize) {
let data = {...contractData}; // data from ethers.js is not extensible. this copy the attributes in a new object let data = {...contractData}; // data from ethers.js is not extensible. this copy the attributes in a new object
// if no hash details are found simply return the data; nothing to merge // if no hash details are found simply return the data; nothing to merge
if (!data.hashSize || data.hashSize === 0) { if (!data.hashSize || data.hashSize === 0) {
@ -22,20 +22,19 @@ class IPFS {
return this.cat(data.ipfsHash) return this.cat(data.ipfsHash)
.then(deserialize) .then(deserialize)
.then((attributes) => { .then(attributes => {
return Object.assign({}, data, attributes); return Object.assign({}, data, attributes);
}); });
} }
add (data) { async add (data) {
return this._ipfsAPI return this._ipfsAPI.add(data)
.add(ipfsClient.Buffer.from(data)) .then(res => {
.then((res) => { return this.decodeHash(res.path);
return this.decodeHash(res[0].hash);
}); });
} }
cat (hashData) { async cat (hashData) {
let ipfsHash = hashData; // default - if it is a string let ipfsHash = hashData; // default - if it is a string
if (Object.prototype.hasOwnProperty.call(hashData, 'hashSize')) { if (Object.prototype.hasOwnProperty.call(hashData, 'hashSize')) {
ipfsHash = this.encodeHash(hashData); ipfsHash = this.encodeHash(hashData);
@ -43,7 +42,12 @@ class IPFS {
if (this._config['gatewayUrl']) { if (this._config['gatewayUrl']) {
return fetch(`${this._config['gatewayUrl']}/${ipfsHash}`).then(r => r.text()); return fetch(`${this._config['gatewayUrl']}/${ipfsHash}`).then(r => r.text());
} else { } else {
return this._ipfsAPI.cat(ipfsHash); const res = this._ipfsAPI.cat(ipfsHash);
let str = '';
for await (const buffer of res) {
str += buffer.toString();
}
return Promise.resolve(str);
} }
} }
@ -66,7 +70,7 @@ class IPFS {
} }
encodeHash (hashData) { encodeHash (hashData) {
let digest = ipfsClient.Buffer.from(hashData.hashDigest.slice(2), 'hex'); const digest = Buffer.from(hashData.hashDigest.slice(2), 'hex');
return multihashes.encode(digest, hashData.hashFunction, hashData.hashSize); return multihashes.encode(digest, hashData.hashFunction, hashData.hashSize);
} }
} }

25508
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -44,6 +44,7 @@
"@openzeppelin/hardhat-upgrades": "^1.10.0", "@openzeppelin/hardhat-upgrades": "^1.10.0",
"async-each-series": "^1.1.0", "async-each-series": "^1.1.0",
"cli-table": "^0.3.1", "cli-table": "^0.3.1",
"colors": "^1.0.3",
"eslint": "^8.14.0", "eslint": "^8.14.0",
"eslint-plugin-import": "^2.20.2", "eslint-plugin-import": "^2.20.2",
"eslint-plugin-node": "^11.1.0", "eslint-plugin-node": "^11.1.0",
@ -64,7 +65,7 @@
"dependencies": { "dependencies": {
"@kosmos/schemas": "^3.1.0", "@kosmos/schemas": "^3.1.0",
"ethers": "^5.4.7", "ethers": "^5.4.7",
"ipfs-http-client": "^41.0.1", "ipfs-http-client": "^56.0.3",
"multihashes": "^4.0.3", "multihashes": "^4.0.3",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"tv4": "^1.3.0" "tv4": "^1.3.0"