Skip to content

Commit

Permalink
Merge pull request #1 from aquiladev/swarm-ens
Browse files Browse the repository at this point in the history
Swarm ENS support
  • Loading branch information
aquiladev authored Mar 28, 2020
2 parents a386145 + d32eae0 commit dbacdcc
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 19 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,17 @@ Parameter |Required |Description
`mnemonic` |Yes |Mnemonic phrase for wallet recovery. Plain PrivateKey can be used as well.
`rpc` |Yes |Url of RPC APIs.
`name` |Yes |Distributed domain name. Currently it supports ENS, CNS (.eth, .crypto) names. (eg `ddns-action.eth`, `ddns-action.crypto`)
`contentHash` |Yes |Hash of content. Currently it supports **IPFS** hash only.
`contentHash` |Yes |Hash of content..
`contentType` |No |Type of content. Supported types [`ipfs-ns`, `swarm-ns`]. Default `ipfs-ns`
`dryRun` |No |Execution emulation without setting new content hash. Default `false`
`verbose` |No |Level of verbosity [`false` - quiet, `true` - verbose]. Default `false`

## Content type support map
Provider |ipfs-ns |swarm-ns
--- |--- |---
ENS |Yes |Yes
CNS |Yes |No

## Example usage

```
Expand Down
10 changes: 9 additions & 1 deletion action.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@ inputs:
description: 'Distributed domain name. Currently it supports ENS, CNS (.eth, .crypto) names. (eg `ddns-action.eth`)'
required: true
contentHash:
description: 'Hash of content. Currently it supports IPFS hash only.'
description: 'Hash of content.'
required: true
contentType:
description: 'Type of content. Supported types [ipfs-ns, swarm-ns]'
required: false
default: 'ipfs-ns'
dryRun:
description: 'Execution emulation without setting new content hash.'
required: false
default: false
verbose:
description: 'Level of verbosity'
required: false
Expand Down
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ async function run() {
const rpc = core.getInput('rpc');
const name = core.getInput('name');
const contentHash = core.getInput('contentHash');
const contentType = core.getInput('contentType');
const dryrun = core.getInput('dryRun');
const verbose = (core.getInput('verbose') === 'true');

await updater.update({ mnemonic, rpc, name, contentHash, verbose })
await updater.update({ mnemonic, rpc, name, contentHash, contentType, dryrun, verbose })
.catch(error => { throw error });

if (verbose) {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ddns-action",
"version": "0.1.0",
"version": "0.1.2",
"description": "DDNS(Distributed Domain Name System) update action",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -30,4 +30,4 @@
"eslintIgnore": [
"*.test.js"
]
}
}
10 changes: 9 additions & 1 deletion updater/cns/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ function CNS(options) {
}

this.getContenthash = async () => {
if (verbose) {
console.log('Getting content...')
}

const tokenId = namehash(name);
const resolverContract = await getResolverContract(tokenId);

return resolverContract.methods.get(ipfsKey, tokenId)
.call({ from: provider.addresses[0] });
}

this.setContenthash = async ({ contentHash }) => {
this.setContenthash = async ({ contentHash, contentType }) => {
if (contentType !== 'ipfs-ns') {
throw new Error('ContentType is not supported. CNS supports only ipfs-ns');
}

const tokenId = namehash(name);
const resolverContract = await getResolverContract(tokenId);

Expand Down
6 changes: 3 additions & 3 deletions updater/ens/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ const Web3 = require('web3');
const Updater = require('@triplespeeder/ens-updater/lib');

module.exports = async (options) => {
const { mnemonic, rpc, name, verbose } = options;
const { mnemonic, rpc, name, dryrun, verbose } = options;
const provider = new HDWalletProvider(mnemonic, rpc);
const web3 = new Web3(provider);

const updaterOptions = {
web3,
ensName: name,
controllerAddress: provider.addresses[0],
verbose: verbose || false,
dryrun: false
dryrun,
verbose: verbose || false
};
const updater = new Updater();
await updater.setup(updaterOptions);
Expand Down
20 changes: 13 additions & 7 deletions updater/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ const core = require('@actions/core');
const ensFactory = require('./ens');
const CNS = require('./cns');

const supportedTypes = ['ipfs-ns', 'swarm-ns'];

const tldMap = [
{ name: '.eth', factory: ensFactory },
{ name: '.crypto', factory: (options) => { return new CNS(options) } }
]

function validate({ name, contentHash }) {
function validate({ name, contentHash, contentType }) {
if (!name) {
throw new Error('Name is unknown or empty');
}
Expand All @@ -20,27 +22,31 @@ function validate({ name, contentHash }) {
if (!contentHash) {
throw new Error('ContentHash is unknown or empty');
}

if (!supportedTypes.find(type => type === contentType)) {
throw new Error('ContentType is not supported');
}
}

module.exports = {
async update(options) {
validate(options);

const { name, contentHash } = options;
const { name, contentHash, contentType } = options;
const { factory } = tldMap.find(tld => name.endsWith(tld.name));
const updater = await factory(options);

let currentContenthash;
let current;
try {
currentContenthash = await updater.getContenthash();
if (currentContenthash.hash === contentHash) {
console.log(`IPFS hash is up to date, update is not needed ${currentContenthash.hash}`);
current = await updater.getContenthash();
if (current.hash === contentHash) {
console.log(`Content hash is up to date. [#${current.hash}]`);
return;
}
} catch (error) {
core.warning(error);
}

return updater.setContenthash({ contentType: 'ipfs-ns', contentHash });
return updater.setContenthash({ contentType, contentHash });
}
}
23 changes: 21 additions & 2 deletions updater/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,32 @@ test('throws when contentHash is empty', async () => {
.rejects.toThrow('ContentHash is unknown or empty');
});

describe.only('Updater: Integration tests', () => {
test('test', async () => {
test('throws when contentType is not supported', async () => {
await expect(updater.update({ name: 'test.eth', contentHash: 'qwerty', contentType: 'q' }))
.rejects.toThrow('ContentType is not supported');
});

describe.skip('Updater: Integration tests', () => {
test('Upload IPSF content hash to ETH name', async () => {
await updater.update({
mnemonic: process.env.DEV_PKEY,
rpc: `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`,
name: 'ddns-action.eth',
contentHash: 'QmRJFpRntf1EMgmC5Tm3Rzc438PRrYCMYZPb6nD4DeaNH6',
contentType: 'ipfs-ns',
dryrun: true,
verbose: true
});
});

test('Upload Swarm content hash to ETH name', async () => {
await updater.update({
mnemonic: process.env.DEV_PKEY,
rpc: `https://mainnet.infura.io/v3/${process.env.INFURA_API_KEY}`,
name: 'ddns-action.eth',
contentHash: 'd1de9994b4d039f6548d191eb26786769f580809256b4685ef316805265ea162',
contentType: 'swarm-ns',
dryrun: true,
verbose: true
});
});
Expand Down

0 comments on commit dbacdcc

Please sign in to comment.