diff --git a/dist/index.js b/dist/index.js index 9a1ecbb..4803715 100644 --- a/dist/index.js +++ b/dist/index.js @@ -37,6 +37,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) { }; exports.__esModule = true; exports.putIpfsJson = exports.getIpfsJson = exports.Dapp = exports.PackBuilder = exports.builder = exports.load = void 0; +var fs_1 = require("fs"); +var jams_js_1 = require("jams.js"); var builder_1 = require("./src/builder"); exports.PackBuilder = builder_1.PackBuilder; var dapp_1 = require("./src/dapp"); @@ -49,26 +51,23 @@ var load = function (arg, _ethers, _signer) { if (_ethers === void 0) { _ethers = undefined; } if (_signer === void 0) { _signer = undefined; } return __awaiter(void 0, void 0, void 0, function () { - var _a; - return __generator(this, function (_b) { - switch (_b.label) { + return __generator(this, function (_a) { + switch (_a.label) { case 0: - if (!(typeof arg === 'string')) return [3 /*break*/, 4]; - if (!((0, ipfs_util_1.isCid)(arg))) return [3 /*break*/, 2]; - return [4 /*yield*/, (0, ipfs_util_1.getIpfsJson)(arg)]; - case 1: - _a = _b.sent(); - return [3 /*break*/, 3]; - case 2: - _a = require(arg); - _b.label = 3; - case 3: - arg = _a; - _b.label = 4; - case 4: + if (typeof arg === 'string') { + if ((0, ipfs_util_1.isCid)(arg)) { + arg = (0, ipfs_util_1.getIpfsJson)(arg); + } + else if (arg.split('.').pop() === 'jams') { + arg = (0, jams_js_1.jams)((0, fs_1.readFileSync)(arg)); + } + else { + arg = require(arg); + } + } (0, util_1.need)(typeof arg === 'object' && Object.keys(arg).length, 'Could not find a pack from provided source.'); return [4 /*yield*/, dapp_1.Dapp.loadFromPack(arg, _ethers, _signer)]; - case 5: return [2 /*return*/, _b.sent()]; + case 1: return [2 /*return*/, _a.sent()]; } }); }); diff --git a/index.ts b/index.ts index 1be2844..26a158c 100644 --- a/index.ts +++ b/index.ts @@ -1,3 +1,5 @@ +import { readFileSync } from 'fs' +import { jams } from 'jams.js' import { PackBuilder } from './src/builder' import { Dapp } from './src/dapp' import { getIpfsJson, putIpfsJson, isCid } from './src/ipfs-util' @@ -5,7 +7,13 @@ import { need } from './src/util' export const load = async (arg, _ethers = undefined, _signer = undefined) => { if (typeof arg === 'string') { - arg = (isCid(arg)) ? await getIpfsJson(arg) : require(arg) + if (isCid(arg)) { + arg = getIpfsJson(arg) + } else if (arg.split('.').pop() === 'jams') { + arg = jams(readFileSync(arg)) + } else { + arg = require(arg) + } } need(typeof arg === 'object' && Object.keys(arg).length, 'Could not find a pack from provided source.') return await Dapp.loadFromPack(arg, _ethers, _signer) diff --git a/package.json b/package.json index 1703c42..aa84c90 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "debug": "^4.3.2", "ethers": "^5.4.5", "ipfs-http-client": "^55.0.0", + "jams.js": "^0.0.7", "multiformats": "^9.6.4" }, "devDependencies": { diff --git a/test/data/weth_ropsten.dpack.jams b/test/data/weth_ropsten.dpack.jams new file mode 100644 index 0000000..d7fc634 --- /dev/null +++ b/test/data/weth_ropsten.dpack.jams @@ -0,0 +1,24 @@ +{ + format dpack-1 + network ropsten + types { + WETH9 { + typename WETH9 + artifact {/ bafkreidsszpx34yqnshrtuszx7n77zxttk2s54kc2m5cftjutaumxe67fa } + } + } + objects { + weth9 { + objectname weth + address 0xc778417E063141139Fce010982780140Aa0cD5Ab + typename WETH9 + artifact {/ bafkreidsszpx34yqnshrtuszx7n77zxttk2s54kc2m5cftjutaumxe67fa } + } + weth9 { + objectname weth9 + address 0xc778417E063141139Fce010982780140Aa0cD5Ab + typename WETH9 + artifact {/ bafkreidsszpx34yqnshrtuszx7n77zxttk2s54kc2m5cftjutaumxe67fa } + } + } +} \ No newline at end of file diff --git a/test/dpack-test.ts b/test/dpack-test.ts index e99e979..6d571c9 100644 --- a/test/dpack-test.ts +++ b/test/dpack-test.ts @@ -18,6 +18,7 @@ before(async () => { describe('end to end simple example', ()=>{ const packPath = path.join(__dirname, './data/weth_ropsten.dpack.json') + const jamsPath = path.join(__dirname, './data/weth_ropsten.dpack.jams') let cidStr it('create weth pack', async () => { @@ -55,10 +56,11 @@ describe('end to end simple example', ()=>{ // another network. Packs can be loaded from a CID string, a file path string, or a json object. const dappFromPack = await load(jsonObj, ethers, signer) const dappFromPath = await load(packPath, ethers, signer) + const dappFromJams = await load(jamsPath, ethers, signer) const dappFromCID = await load(cidStr, ethers, signer) // All methods give the same pack - let packStrings = [JSON.stringify(dappFromPack), JSON.stringify(dappFromPath), JSON.stringify(dappFromCID)]; + let packStrings = [JSON.stringify(dappFromPack), JSON.stringify(dappFromPath), JSON.stringify(dappFromJams), JSON.stringify(dappFromCID)]; want((new Set(packStrings)).size).to.equal(1) }) }); diff --git a/typings.d.ts b/typings.d.ts new file mode 100644 index 0000000..88930e8 --- /dev/null +++ b/typings.d.ts @@ -0,0 +1 @@ +declare module "jams.js" \ No newline at end of file