-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '202-add-ipfs-api-to-wasm' into 'develop'
Resolve "Add IPFS API to WASM" Closes #202 See merge request in3/c/in3-core!183
- Loading branch information
Showing
11 changed files
with
203 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
class IpfsAPI { | ||
constructor(client) { | ||
this.client = client | ||
this.encoding = 'base64' | ||
} | ||
|
||
/** | ||
* retrieves the content for a hash from IPFS. | ||
* @param multihash the IPFS-hash to fetch | ||
* | ||
*/ | ||
get(multihash) { | ||
return this.client.sendRPC('ipfs_get', [multihash, this.encoding]) | ||
.then(response => response || Promise.reject(new Error(response.error || 'Hash not found'))) | ||
.then(result => this.client.util.base64Decode(result)) | ||
.then(result => this.client.util.toBuffer(result)) | ||
} | ||
|
||
/** | ||
* stores the data on ipfs and returns the IPFS-Hash. | ||
* @param content puts a IPFS content | ||
*/ | ||
put(data) { | ||
let encoded = this.client.util.base64Encode(this.client.util.toBuffer(data)) | ||
return this.client.sendRPC('ipfs_put', [encoded, this.encoding]) | ||
.then(response => response || Promise.reject(new Error(response.error || 'Hash not found'))) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"content": { | ||
"id": 1, | ||
"result": "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQ=", | ||
"jsonrpc": "2.0", | ||
"in3": { | ||
"lastValidatorChange": 0, | ||
"lastNodeList": 8525528, | ||
"execTime": 14, | ||
"rpcTime": 0, | ||
"rpcCount": 0, | ||
"currentBlock": 17091918 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"multihash": { | ||
"id": 1, | ||
"result": "QmbGySCLuGxu2GxVLYWeqJW9XeyjGFvpoZAhGhXDGEUQu8", | ||
"jsonrpc": "2.0", | ||
"in3": { | ||
"lastValidatorChange": 0, | ||
"lastNodeList": 8525528, | ||
"execTime": 23, | ||
"rpcTime": 0, | ||
"rpcCount": 0, | ||
"currentBlock": 17091918 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require('mocha') | ||
const { assert } = require('chai') | ||
const { createClient, mockResponse, IN3, beforeTest } = require('./util/mocker') | ||
|
||
describe('API-Tests', () => { | ||
beforeEach(beforeTest) | ||
afterEach(IN3.freeAll) | ||
|
||
it('ipfs_put', async () => { | ||
mockResponse('ipfs_put', 'multihash') | ||
const multihash = await createClient({ chainId: '0x7d0', proof: 'none' }).ipfs.put("Lorem ipsum dolor sit amet") | ||
assert.equal(multihash, 'QmbGySCLuGxu2GxVLYWeqJW9XeyjGFvpoZAhGhXDGEUQu8') | ||
}) | ||
|
||
it('ipfs_get', async () => { | ||
mockResponse('ipfs_get', 'content') | ||
const content = await createClient({ chainId: '0x7d0' }).ipfs.get('QmbGySCLuGxu2GxVLYWeqJW9XeyjGFvpoZAhGhXDGEUQu8') | ||
assert.equal(IN3.util.toUtf8(content), 'Lorem ipsum dolor sit amet') | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters