From f1a8cb953b8bdcacf2c1a36db51ccb2d895a940b Mon Sep 17 00:00:00 2001 From: Ilan Olkies Date: Thu, 26 Dec 2019 17:51:07 -0300 Subject: [PATCH 1/2] Create npm package --- .circleci/config.yml | 22 ++ .gitattributes | 1 + .gitignore | 2 + contracts/AbstractAddrResolver.sol | 10 + contracts/AbstractMultiChainResolver.sol | 10 + contracts/AbstractPublicResolver.sol | 10 + contracts/AddressUtil.sol | 86 ++++++ contracts/Dummy.sol | 6 + contracts/Migrations.sol | 23 ++ contracts/MultiChainResolver.sol | 112 ++++++++ contracts/PublicResolver.sol | 96 +++++++ migrations/1_initial_migration.js | 5 + migrations/2_resolver.js | 15 + package-lock.json | 49 ++++ package.json | 34 +++ test/multi_chain_resolver.test.js | 334 +++++++++++++++++++++++ test/public_resolver.test.js | 33 +++ truffle-config.js | 99 +++++++ 18 files changed, 947 insertions(+) create mode 100644 .circleci/config.yml create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 contracts/AbstractAddrResolver.sol create mode 100644 contracts/AbstractMultiChainResolver.sol create mode 100644 contracts/AbstractPublicResolver.sol create mode 100644 contracts/AddressUtil.sol create mode 100644 contracts/Dummy.sol create mode 100644 contracts/Migrations.sol create mode 100644 contracts/MultiChainResolver.sol create mode 100644 contracts/PublicResolver.sol create mode 100644 migrations/1_initial_migration.js create mode 100644 migrations/2_resolver.js create mode 100644 package-lock.json create mode 100644 package.json create mode 100644 test/multi_chain_resolver.test.js create mode 100644 test/public_resolver.test.js create mode 100644 truffle-config.js diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..c840e79 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,22 @@ +version: 2.1 +jobs: + build: + working_directory: ~/tpm + + docker: + - image: circleci/node:10 + + steps: + - checkout + - run: + name: Prepare Truffle + command: sudo npm install -g truffle + + - run: + name: Install rns-registry + command: | + cd ~/tpm/ + npm install + - run: + name: Run Truffle Tests + command: npm test diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..52031de --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +*.sol linguist-language=Solidity diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b38db2f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +node_modules/ +build/ diff --git a/contracts/AbstractAddrResolver.sol b/contracts/AbstractAddrResolver.sol new file mode 100644 index 0000000..82bca26 --- /dev/null +++ b/contracts/AbstractAddrResolver.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.5.0; + +import "@openzeppelin/contracts/introspection/IERC165.sol"; + +contract AbstractAddrResolver is IERC165 { + function addr(bytes32 node) public view returns (address ret); + function setAddr(bytes32 node, address addrValue) public; + + event AddrChanged(bytes32 indexed node, address addr); +} diff --git a/contracts/AbstractMultiChainResolver.sol b/contracts/AbstractMultiChainResolver.sol new file mode 100644 index 0000000..a4f8f20 --- /dev/null +++ b/contracts/AbstractMultiChainResolver.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.5.0; + +import "./AbstractAddrResolver.sol"; + +contract AbstractMultiChainResolver is AbstractAddrResolver { + function chainAddr(bytes32 node, bytes4 chain) public view returns (string memory); + function setChainAddr(bytes32 node, bytes4 chain, string memory addrValue) public; + + event ChainAddrChanged(bytes32 indexed node, bytes4 chain, string addr); +} diff --git a/contracts/AbstractPublicResolver.sol b/contracts/AbstractPublicResolver.sol new file mode 100644 index 0000000..1c52260 --- /dev/null +++ b/contracts/AbstractPublicResolver.sol @@ -0,0 +1,10 @@ +pragma solidity ^0.5.0; + +import "./AbstractAddrResolver.sol"; + +contract AbstractPublicResolver is AbstractAddrResolver { + function content(bytes32 node) public view returns (bytes32 ret); + function setContent(bytes32 node, bytes32 hashValue) public; + + function has(bytes32 node, bytes32 kind) public view returns (bool); +} diff --git a/contracts/AddressUtil.sol b/contracts/AddressUtil.sol new file mode 100644 index 0000000..bca0770 --- /dev/null +++ b/contracts/AddressUtil.sol @@ -0,0 +1,86 @@ +pragma solidity ^0.5.0; + +contract AddressUtil { + function addressToString (address data) public pure returns (string memory) { + bytes memory s = new bytes(42); + s[0] = "0"; + s[1] = "x"; + for (uint i = 0; i < 20; i++) { + byte b = byte(uint8(uint(data) / (2**(8*(19 - i))))); + byte hi = byte(uint8(b) / 16); + byte lo = byte(uint8(b) - 16 * uint8(hi)); + s[2*i + 2] = char(hi); + s[2*i + 3] = char(lo); + } + return string(s); + } + + function char (byte b) internal pure returns (byte c) { + if (b < 0x0A) return byte(uint8(b) + 0x30); + else return byte(uint8(b) + 0x57); + } + + // source: https://github.com/riflabs/RIF-Token/blob/master/contracts/util/AddressHelper.sol + function stringToAddress(string memory s) public pure returns(address) { + bytes memory ss = bytes(s); + + // it should have 40 or 42 characters + if (ss.length != 40 && ss.length != 42) revert(); + + uint r = 0; + uint offset = 0; + + if (ss.length == 42) { + offset = 2; + + if (ss[0] != byte('0')) revert(); + if (ss[1] != byte('x') && ss[1] != byte('X')) revert(); + } + + uint i; + uint x; + uint v; + + // loads first 32 bytes from array, + // skipping array length (32 bytes to skip) + // offset == 0x20 + assembly { v := mload(add(0x20, ss)) } + + // converts the first 32 bytes, adding to result + for (i = offset; i < 32; ++i) { + assembly { x := byte(i, v) } + r = r * 16 + fromHexChar(x); + } + + // loads second 32 bytes from array, + // skipping array length (32 bytes to skip) + // and first 32 bytes + // offset == 0x40 + assembly { v := mload(add(0x40, ss)) } + + // converts the last 8 bytes, adding to result + for (i = 0; i < 8 + offset; ++i) { + assembly { x := byte(i, v) } + r = r * 16 + fromHexChar(x); + } + + return address(r); + } + + function fromHexChar(uint c) public pure returns (uint) { + if (c >= 0x30 && c <= 0x39) { + return c - 0x30; + } + + if (c >= 0x61 && c <= 0x66) { + return 10 + c - 0x61; + } + + if (c >= 0x41 && c <= 0x46) { + return 10 + c - 0x41; + } + + // Reaching this point means the ordinal is not for a hex char. + revert(); + } +} diff --git a/contracts/Dummy.sol b/contracts/Dummy.sol new file mode 100644 index 0000000..faf8305 --- /dev/null +++ b/contracts/Dummy.sol @@ -0,0 +1,6 @@ +pragma solidity ^0.5.0; + +import "@rsksmart/rns-registry/contracts/RNS.sol"; + +contract Dummy { +} diff --git a/contracts/Migrations.sol b/contracts/Migrations.sol new file mode 100644 index 0000000..d5114c1 --- /dev/null +++ b/contracts/Migrations.sol @@ -0,0 +1,23 @@ +pragma solidity ^0.5.0; + +contract Migrations { + address public owner; + uint public last_completed_migration; + + constructor() public { + owner = msg.sender; + } + + modifier restricted() { + if (msg.sender == owner) _; + } + + function setCompleted(uint completed) public restricted { + last_completed_migration = completed; + } + + function upgrade(address new_address) public restricted { + Migrations upgraded = Migrations(new_address); + upgraded.setCompleted(last_completed_migration); + } +} diff --git a/contracts/MultiChainResolver.sol b/contracts/MultiChainResolver.sol new file mode 100644 index 0000000..3cffaca --- /dev/null +++ b/contracts/MultiChainResolver.sol @@ -0,0 +1,112 @@ +pragma solidity ^0.5.0; + +import "@rsksmart/rns-registry/contracts/AbstractRNS.sol"; +import "./AbstractPublicResolver.sol"; +import "./AbstractMultiChainResolver.sol"; +import "./AddressUtil.sol"; + +contract MultiChainResolver is AbstractMultiChainResolver { + AbstractRNS rns; + AbstractPublicResolver publicResolver; + + AddressUtil addressHelper; + + mapping (bytes32 => bytes32) contents; + mapping (bytes32 => mapping (bytes8 => ChainAddress)) chainAddresses; + + bytes4 constant ADDR_SIGN = 0x3b3b57de; + bytes4 constant CONTENT_SIGN = 0x2dff6941; + bytes4 constant CHAIN_ADDR_SIGN = 0x8be4b5f6; + + bytes4 constant RSK_CHAIN_ID = 0x80000089; + + event ContentChanged (bytes32 node, bytes32 content); + event ChainMetadataChanged (bytes32 node, bytes4 chain, bytes32 metadata); + + struct ChainAddress { + bytes32 metadata; + string addr; + } + + modifier onlyOwner (bytes32 node) { + require(rns.owner(node) == msg.sender); + _; + } + + constructor (AbstractRNS _rns, AbstractPublicResolver _publicResolver) public { + rns = _rns; + publicResolver = _publicResolver; + addressHelper = new AddressUtil(); + } + + function () external { + revert(); + } + + function supportsInterface (bytes4 interfaceId) public view returns (bool) { + return ((interfaceId == ADDR_SIGN) || (interfaceId == CONTENT_SIGN) || interfaceId == (CHAIN_ADDR_SIGN)); + } + + function addr (bytes32 node) public view returns (address) { + string memory _addr = chainAddresses[node][RSK_CHAIN_ID].addr; + + if (bytes(_addr).length > 0) { + return addressHelper.stringToAddress(_addr); + } + + return publicResolver.addr(node); + } + + function setAddr (bytes32 node, address addrValue) public onlyOwner(node) { + chainAddresses[node][RSK_CHAIN_ID].addr = addressHelper.addressToString(addrValue); + emit AddrChanged(node, addrValue); + } + + function content (bytes32 node) public view returns (bytes32) { + bytes32 _content = contents[node]; + + if (_content != 0) { + return _content; + } + + return publicResolver.content(node); + } + + function setContent (bytes32 node, bytes32 contentValue) public onlyOwner(node) { + contents[node] = contentValue; + emit ContentChanged(node, contentValue); + } + + function chainAddr (bytes32 node, bytes4 chain) public view returns (string memory) { + return chainAddresses[node][chain].addr; + } + + function setChainAddr (bytes32 node, bytes4 chain, string memory addrValue) public onlyOwner(node) { + chainAddresses[node][chain].addr = addrValue; + if (chain == RSK_CHAIN_ID) { + address _addr = addressHelper.stringToAddress(addrValue); + emit AddrChanged(node, _addr); + } else { + emit ChainAddrChanged(node, chain, addrValue); + } + } + + function chainMetadata (bytes32 node, bytes4 chain) public view returns (bytes32) { + return chainAddresses[node][chain].metadata; + } + + function setChainMetadata (bytes32 node, bytes4 chain, bytes32 metadataValue) public onlyOwner(node) { + chainAddresses[node][chain].metadata = metadataValue; + emit ChainMetadataChanged(node, chain, metadataValue); + } + + function chainAddrAndMetadata (bytes32 node, bytes4 chain) public view returns (string memory, bytes32) { + ChainAddress storage chainAddress = chainAddresses[node][chain]; + return (chainAddress.addr, chainAddress.metadata); + } + + function setChainAddrWithMetadata (bytes32 node, bytes4 chain, string memory addrValue, bytes32 metadataValue) public onlyOwner(node) { + setChainAddr(node, chain, addrValue); + setChainMetadata(node, chain, metadataValue); + } +} diff --git a/contracts/PublicResolver.sol b/contracts/PublicResolver.sol new file mode 100644 index 0000000..58f0176 --- /dev/null +++ b/contracts/PublicResolver.sol @@ -0,0 +1,96 @@ +pragma solidity ^0.5.0; + +import "@rsksmart/rns-registry/contracts/AbstractRNS.sol"; + +/** + * A simple resolver anyone can use; only allows the owner of a node to set its + * address. + */ +contract PublicResolver { + AbstractRNS rns; + mapping(bytes32=>address) addresses; + mapping(bytes32=>bytes32) hashes; + + modifier only_owner(bytes32 node) { + require(rns.owner(node) == msg.sender); + _; + } + + /** + * Constructor. + * @param rnsAddr The RNS registrar contract. + */ + constructor(AbstractRNS rnsAddr) public { + rns = rnsAddr; + } + + /** + * Fallback function. + */ + function() payable external { + revert(); + } + + /** + * Returns true if the specified node has the specified record type. + * @param node The RNS node to query. + * @param kind The record type name, as specified in EIP137. + * @return True if this resolver has a record of the provided type on the + * provided node. + */ + function has(bytes32 node, bytes32 kind) public view returns (bool) { + return (kind == "addr" && addresses[node] != address(0)) || + (kind == "hash" && hashes[node] != 0); + } + + /** + * Returns true if the resolver implements the interface specified by the provided hash. + * @param interfaceID The ID of the interface to check for. + * @return True if the contract implements the requested interface. + */ + function supportsInterface(bytes4 interfaceID) public pure returns (bool) { + return interfaceID == 0x3b3b57de || interfaceID == 0xd8389dc5; + } + + /** + * Returns the address associated with an RNS node. + * @param node The RNS node to query. + * @return The associated address. + */ + function addr(bytes32 node) public view returns (address) { + return addresses[node]; + } + + /** + * Sets the address associated with an RNS node. + * May only be called by the owner of that node in the RNS registry. + * @param node The node to update. + * @param addrValue The address to set. + */ + function setAddr(bytes32 node, address addrValue) public only_owner(node) { + addresses[node] = addrValue; + } + + /** + * Returns the content hash associated with an RNS node. + * Note that this resource type is not standardized, and will likely change + * in future to a resource type based on multihash. + * @param node The RNS node to query. + * @return The associated content hash. + */ + function content(bytes32 node) public view returns (bytes32) { + return hashes[node]; + } + + /** + * Sets the content hash associated with an RNS node. + * May only be called by the owner of that node in the RNS registry. + * Note that this resource type is not standardized, and will likely change + * in future to a resource type based on multihash. + * @param node The node to update. + * @param hash The content hash to set + */ + function setContent(bytes32 node, bytes32 hash) public only_owner(node) { + hashes[node] = hash; + } +} diff --git a/migrations/1_initial_migration.js b/migrations/1_initial_migration.js new file mode 100644 index 0000000..d182530 --- /dev/null +++ b/migrations/1_initial_migration.js @@ -0,0 +1,5 @@ +const Migrations = artifacts.require('Migrations'); + +module.exports = function(deployer) { + deployer.deploy(Migrations); +}; diff --git a/migrations/2_resolver.js b/migrations/2_resolver.js new file mode 100644 index 0000000..0655ac5 --- /dev/null +++ b/migrations/2_resolver.js @@ -0,0 +1,15 @@ +const RNS = artifacts.require('RNS'); +const PublicResolver = artifacts.require('PublicResolver'); +const MultiChainResolver = artifacts.require('MultiChainResolver'); + +module.exports = (deployer) => { + let rns; + + deployer.deploy(RNS) + .then(_rns => { + rns = _rns; + return deployer.deploy(PublicResolver, RNS.address) + }) + .then(() => deployer.deploy(MultiChainResolver, RNS.address, PublicResolver.address)) + .then(() => rns.setDefaultResolver(MultiChainResolver.address)); +} diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..12ade2a --- /dev/null +++ b/package-lock.json @@ -0,0 +1,49 @@ +{ + "name": "@rsksmart/rns-resolver", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "@openzeppelin/contracts": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/@openzeppelin/contracts/-/contracts-2.4.0.tgz", + "integrity": "sha512-xeKP59REgow5TPBJh3S9BRIm7DDG+Rz3Nt4ANWGUkjk4305DHpyUD5CyMJ6nd2JMmZuFyx4mjvvlCtSJLRnN6w==" + }, + "@rsksmart/rns-registry": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@rsksmart/rns-registry/-/rns-registry-1.0.0.tgz", + "integrity": "sha512-rn68uGGvU90dtdNcyXXGGhbIPsek/W7cHGN8UqYLomhAXX3OFQd+JDSVkJUBOOY92Ou9V5TYhUh3NOzSv5/1+Q==" + }, + "eth-ens-namehash": { + "version": "2.0.8", + "resolved": "https://registry.npmjs.org/eth-ens-namehash/-/eth-ens-namehash-2.0.8.tgz", + "integrity": "sha1-IprEbsqG1S4MmR58sq74P/D2i88=", + "dev": true, + "requires": { + "idna-uts46-hx": "^2.3.1", + "js-sha3": "^0.5.7" + } + }, + "idna-uts46-hx": { + "version": "2.3.1", + "resolved": "https://registry.npmjs.org/idna-uts46-hx/-/idna-uts46-hx-2.3.1.tgz", + "integrity": "sha512-PWoF9Keq6laYdIRwwCdhTPl60xRqAloYNMQLiyUnG42VjT53oW07BXIRM+NK7eQjzXjAk2gUvX9caRxlnF9TAA==", + "dev": true, + "requires": { + "punycode": "2.1.0" + } + }, + "js-sha3": { + "version": "0.5.7", + "resolved": "https://registry.npmjs.org/js-sha3/-/js-sha3-0.5.7.tgz", + "integrity": "sha1-DU/9gALVMzqrr0oj7tL2N0yfKOc=", + "dev": true + }, + "punycode": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.0.tgz", + "integrity": "sha1-X4Y+3Im5bbCQdLrXlHvwkFbKTn0=", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..770765c --- /dev/null +++ b/package.json @@ -0,0 +1,34 @@ +{ + "name": "@rsksmart/rns-resolver", + "version": "1.0.0", + "description": "RNS Resolver", + "files": [ + "/contracts", + "!/contracts/Migrations.sol" + ], + "scripts": { + "test": "truffle test" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/rnsdomains/rns-resolver.git" + }, + "keywords": [ + "rsk", + "rif", + "rns" + ], + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/rnsdomains/rns-resolver/issues" + }, + "homepage": "https://github.com/rnsdomains/rns-resolver#readme", + "dependencies": { + "@openzeppelin/contracts": "^2.4.0", + "@rsksmart/rns-registry": "^1.0.0" + }, + "devDependencies": { + "eth-ens-namehash": "^2.0.8" + } +} diff --git a/test/multi_chain_resolver.test.js b/test/multi_chain_resolver.test.js new file mode 100644 index 0000000..280d8ab --- /dev/null +++ b/test/multi_chain_resolver.test.js @@ -0,0 +1,334 @@ +const RNS = artifacts.require('RNS'); +const PublicResolver = artifacts.require('PublicResolver'); +const MultiChainResolver = artifacts.require('MultiChainResolver'); + +const namehash = require('eth-ens-namehash').hash; + +contract('MultiChainResolver', async (accounts) => { + var publicResolver, multiChainResolver; + + const hash = namehash('rsk'); + + const chainId = { + rsk: '0x80000089', + eth: '0x8000003c', + btc: '0x80000000' + }; + + beforeEach(async () => { + const rns = await RNS.new(); + publicResolver = await PublicResolver.new(rns.address); + + await rns.setSubnodeOwner('0x00', web3.utils.sha3('rsk'), accounts[0]); + await rns.setResolver(hash, publicResolver.address); + + multiChainResolver = await MultiChainResolver.new(rns.address, publicResolver.address); + }); + + it('should create smart contracts', async () => { return }); + + it('should return public resolver address', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + + await publicResolver.setAddr(hash, addr); + + const actualAddr = await multiChainResolver.addr(hash); + + assert.equal(actualAddr, addr); + }); + + it('should return public resolver content', async () => { + const content = '0x524e5320544c4400000000000000000000000000000000000000000000000000'; // bytes for 'RNS TLD' + + await publicResolver.setContent(hash, content); + + const actualContent = await publicResolver.content(hash); + + assert.equal(actualContent, content); + }); + + it('should be able to set addr', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + + await multiChainResolver.setAddr(hash, addr); + + const actualAddr = await multiChainResolver.addr(hash); + + assert.equal(actualAddr, addr); + }); + + it('should be able to set content', async () => { + const content = '0x524e5320544c4400000000000000000000000000000000000000000000000000'; // bytes for 'RNS TLD' + + await multiChainResolver.setContent(hash, content); + + const actualContent = await multiChainResolver.content(hash); + + assert.equal(actualContent, content); + }); + + it('should allow only RNS owner to set addr', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + await multiChainResolver.setAddr(hash, addr); + + try { + const newAddr = '0x4444444444555555555566666666667777777777'; + await multiChainResolver.setAddr(hash, newAddr, { from: accounts[1] }); + } catch { + const actualAddr = await multiChainResolver.addr(hash); + assert.equal(actualAddr, addr); + return; + } + + assert.fail(); + }); + + it('should allow only RNS owner to set content', async () => { + const content = '0x524e5320544c4400000000000000000000000000000000000000000000000000'; // bytes for 'RNS TLD' + await multiChainResolver.setContent(hash, content); + + + try { + const newContent = '0x61747461636b0000000000000000000000000000000000000000000000000000'; // bytes for 'attack' + await multiChainResolver.setContent(hash, newContent, { from: accounts[1] }); + } catch { + const actualContent = await multiChainResolver.content(hash); + assert.equal(actualContent, content); + return; + } + + assert.fail(); + }); + + describe('RNSIP-02', async () => { + it('should implement supportsInterface method', async () => { + const addrSign = web3.utils.sha3('addr(bytes32)').slice(0, 10); + const contentSign = web3.utils.sha3('content(bytes32)').slice(0, 10); + const chainAddrSign = web3.utils.sha3('chainAddr(bytes32,bytes4)').slice(0, 10); + + const supportsAddr = await multiChainResolver.supportsInterface(addrSign); + const supportsContent = await multiChainResolver.supportsInterface(contentSign); + const supportsChainAddr = await multiChainResolver.supportsInterface(chainAddrSign); + + assert.ok(supportsAddr && supportsContent && supportsChainAddr); + }); + + it('should implement fallback function that throws', async () => { + try { + await web3.eth.sendTransaction({ from: accounts[0], to: multiChainResolver.address, value: web3.utils.toBN(1e18) }); + } catch (e) { + return; + } + assert.fail(); + }); + + it('should emit AddrChanged event', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + const tx = await multiChainResolver.setAddr(hash, addr); + + const addrChangedLog = tx.logs.find(log => log.event === 'AddrChanged'); + + assert(addrChangedLog); + assert.equal(addrChangedLog.args.node, hash); + assert.equal(addrChangedLog.args.addr, addr); + }); + + it('should emit ContentChanged event', async () => { + const content = '0x524e5320544c4400000000000000000000000000000000000000000000000000'; // bytes for 'RNS TLD' + const tx = await multiChainResolver.setContent(hash, content); + + const contentChangedLog = tx.logs.find(log => log.event === 'ContentChanged'); + + assert(contentChangedLog); + assert.equal(contentChangedLog.args.node, hash); + assert.equal(contentChangedLog.args.content, content); + }); + + it('should emit AddrChanged event setting rsk address', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + const tx = await multiChainResolver.setChainAddr(hash, chainId.rsk, addr); + + const addrChangedLog = tx.logs.find(log => log.event === 'AddrChanged'); + + assert(addrChangedLog); + assert.equal(addrChangedLog.args.node, hash); + assert.equal(addrChangedLog.args.addr, addr); + }); + }); + + describe('RNSIP-03', async () => { + it('should return rsk address', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + await multiChainResolver.setAddr(hash, addr); + + const actualAddr = await multiChainResolver.chainAddr(hash, chainId.rsk); + + assert.equal(actualAddr, addr); + }); + + it('should not return rsk address for other id', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + await multiChainResolver.setAddr(hash, addr); + + const actualAddr = await multiChainResolver.chainAddr(hash, '0x00000000'); + + assert.notEqual(actualAddr, addr); + }); + + it('should set rsk address with setChainAddr', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + await multiChainResolver.setChainAddr(hash, chainId.rsk, addr); + + const actualAddr = await multiChainResolver.chainAddr(hash, chainId.rsk); + + assert.equal(actualAddr, addr); + }); + + it('should allow only RNS owner to set rsk addr', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + await multiChainResolver.setAddr(hash, addr); + + try { + const newAddr = '0x4444444444555555555566666666667777777777'; + await multiChainResolver.setChainAddr(hash, chainId.rsk, newAddr, { from: accounts[1] }); + } catch { + const actualAddr = await multiChainResolver.addr(hash); + assert.equal(actualAddr, addr); + return; + } + + assert.fail(); + }); + + describe('supporting chains', async () => { + it('should store eth address', async () => { + const addr = '0x44444444445555555555666666666677777777777'; + + await multiChainResolver.setChainAddr(hash, chainId.eth, addr); + + const actualAddr = await multiChainResolver.chainAddr(hash, chainId.eth); + + assert.equal(actualAddr, addr); + }); + + it('should store btc address', async () => { + const addr = '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2'; + + await multiChainResolver.setChainAddr(hash, chainId.btc, addr); + + const actualAddr = await multiChainResolver.chainAddr(hash, chainId.btc); + + assert.equal(actualAddr, addr); + }); + + it('should store all addresses', async () => { + const rskAddr = '0x0000000000111111111122222222223333333333'; + const ethAddr = '0x44444444445555555555666666666677777777777'; + const btcAddr = '1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2'; + + await multiChainResolver.setChainAddr(hash, chainId.rsk, rskAddr); + await multiChainResolver.setChainAddr(hash, chainId.eth, ethAddr); + await multiChainResolver.setChainAddr(hash, chainId.btc, btcAddr); + + const actualRskAddr = await multiChainResolver.chainAddr(hash, chainId.rsk); + const actualEthAddr = await multiChainResolver.chainAddr(hash, chainId.eth); + const actualBtcAddr = await multiChainResolver.chainAddr(hash, chainId.btc); + + assert.equal(actualRskAddr, rskAddr); + assert.equal(actualEthAddr, ethAddr); + assert.equal(actualBtcAddr, btcAddr); + }); + }); + + it('should allow only RNS owner to set chian address', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + await multiChainResolver.setChainAddr(hash, chainId.eth, addr); + + try { + const newAddr = '0x4444444444555555555566666666667777777777'; + await multiChainResolver.setChainAddr(hash, chainId.eth, newAddr, { from: accounts[1] }); + } catch { + const actualAddr = await multiChainResolver.chainAddr(hash, chainId.eth); + assert.equal(actualAddr, addr); + return; + } + + assert.fail(); + }); + + it('should emit ChainAddrChanged event', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + const tx = await multiChainResolver.setChainAddr(hash, chainId.eth, addr); + + const addrChangedLog = tx.logs.find(log => log.event === 'ChainAddrChanged'); + + assert(addrChangedLog); + assert.equal(addrChangedLog.args.node, hash); + assert.equal(addrChangedLog.args.chain, chainId.eth); + assert.equal(addrChangedLog.args.addr, addr); + }); + + it('should not emit ChainAddrChanged event for rsk chain', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + const tx = await multiChainResolver.setChainAddr(hash, chainId.rsk, addr); + + const addrChangedLog = tx.logs.find(log => log.event === 'ChainAddrChanged'); + + assert(!addrChangedLog); + }); + }); + + it('should store metadata for each address', async () => { + const meta = '0x5032504b48000000000000000000000000000000000000000000000000000000' // 32 bytes for 'P2PKH' + + await multiChainResolver.setChainMetadata(hash, chainId.btc, meta); + + const actualMeta = await multiChainResolver.chainMetadata(hash, chainId.btc); + + assert.equal(actualMeta, meta); + }); + + it('should emit ChainMetadataChanged event', async () => { + const meta = '0x5032504b48000000000000000000000000000000000000000000000000000000' // 32 bytes for 'P2PKH' + + const tx = await multiChainResolver.setChainMetadata(hash, chainId.btc, meta); + + const addrChangedLog = tx.logs.find(log => log.event === 'ChainMetadataChanged'); + + assert(addrChangedLog); + assert.equal(addrChangedLog.args.node, hash); + assert.equal(addrChangedLog.args.chain, chainId.btc); + assert.equal(addrChangedLog.args.metadata, meta); + }); + + it('should set chain addr and metadata in one tx', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + const meta = '0x5032504b48000000000000000000000000000000000000000000000000000000' // 32 bytes for 'P2PKH' + + await multiChainResolver.setChainAddrWithMetadata(hash, chainId.btc, addr, meta); + + const actualAddr = await multiChainResolver.chainAddr(hash, chainId.btc); + const actualMeta = await multiChainResolver.chainMetadata(hash, chainId.btc); + + assert.equal(actualAddr, addr); + assert.equal(actualMeta, meta); + }); + + it('should get chain addr and metadata in one call', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + const meta = '0x5032504b48000000000000000000000000000000000000000000000000000000' // 32 bytes for 'P2PKH' + + await multiChainResolver.setChainAddrWithMetadata(hash, chainId.btc, addr, meta); + + const actualAddress = await multiChainResolver.chainAddrAndMetadata(hash, chainId.btc); + + assert.equal(actualAddress[0], addr); + assert.equal(actualAddress[1], meta); + }); + + it('should get null for an addr chain not set', async () => { + const addr = await multiChainResolver.chainAddr(hash, chainId.eth); + + assert(!addr); + }); +}); diff --git a/test/public_resolver.test.js b/test/public_resolver.test.js new file mode 100644 index 0000000..fa69caf --- /dev/null +++ b/test/public_resolver.test.js @@ -0,0 +1,33 @@ +const RNS = artifacts.require('RNS'); +const PublicResolver = artifacts.require('PublicResolver'); +const namehash = require('eth-ens-namehash').hash; + +contract('PublicResolver', async (accounts) => { + var rns, publicResolver; + const hash = namehash('rsk'); + + beforeEach(async () => { + rns = await RNS.new(); + publicResolver = await PublicResolver.new(rns.address); + await rns.setSubnodeOwner('0x00', web3.utils.sha3('rsk'), accounts[0]); + await rns.setResolver(hash, publicResolver.address); + }); + + it('should store addr', async () => { + const addr = '0x0000000000111111111122222222223333333333'; + await publicResolver.setAddr(hash, addr); + + const actualAddr = await publicResolver.addr(hash); + + assert.equal(actualAddr, addr); + }); + + it('should store content', async () => { + const content = '0x0000000000111111111122222222223333333333444444444455555555556666'; + await publicResolver.setContent(hash, content); + + const actualContent = await publicResolver.content(hash); + + assert.equal(actualContent, content); + }); +}); diff --git a/truffle-config.js b/truffle-config.js new file mode 100644 index 0000000..3865690 --- /dev/null +++ b/truffle-config.js @@ -0,0 +1,99 @@ +/** + * Use this file to configure your truffle project. It's seeded with some + * common settings for different networks and features like migrations, + * compilation and testing. Uncomment the ones you need or modify + * them to suit your project as necessary. + * + * More information about configuration can be found at: + * + * truffleframework.com/docs/advanced/configuration + * + * To deploy via Infura you'll need a wallet provider (like truffle-hdwallet-provider) + * to sign your transactions before they're sent to a remote public node. Infura accounts + * are available for free at: infura.io/register. + * + * You'll also need a mnemonic - the twelve word phrase the wallet uses to generate + * public/private key pairs. If you're publishing your code to GitHub make sure you load this + * phrase from a file you've .gitignored so it doesn't accidentally become public. + * + */ + +// const HDWalletProvider = require('truffle-hdwallet-provider'); +// const infuraKey = "fj4jll3k....."; +// +// const fs = require('fs'); +// const mnemonic = fs.readFileSync(".secret").toString().trim(); + +module.exports = { + /** + * Networks define how you connect to your ethereum client and let you set the + * defaults web3 uses to send transactions. If you don't specify one truffle + * will spin up a development blockchain for you on port 9545 when you + * run `develop` or `test`. You can ask a truffle command to use a specific + * network from the command line, e.g + * + * $ truffle test --network + */ + + networks: { + // Useful for testing. The `development` name is special - truffle uses it by default + // if it's defined here and no other network is specified at the command line. + // You should run a client (like ganache-cli, geth or parity) in a separate terminal + // tab if you use this network and you must also set the `host`, `port` and `network_id` + // options below to some value. + // + // development: { + // host: "127.0.0.1", // Localhost (default: none) + // port: 8545, // Standard Ethereum port (default: none) + // network_id: "*", // Any network (default: none) + // }, + + // Another network with more advanced options... + // advanced: { + // port: 8777, // Custom port + // network_id: 1342, // Custom network + // gas: 8500000, // Gas sent with each transaction (default: ~6700000) + // gasPrice: 20000000000, // 20 gwei (in wei) (default: 100 gwei) + // from:
, // Account to send txs from (default: accounts[0]) + // websockets: true // Enable EventEmitter interface for web3 (default: false) + // }, + + // Useful for deploying to a public network. + // NB: It's important to wrap the provider as a function. + // ropsten: { + // provider: () => new HDWalletProvider(mnemonic, `https://ropsten.infura.io/v3/YOUR-PROJECT-ID`), + // network_id: 3, // Ropsten's id + // gas: 5500000, // Ropsten has a lower block limit than mainnet + // confirmations: 2, // # of confs to wait between deployments. (default: 0) + // timeoutBlocks: 200, // # of blocks before a deployment times out (minimum/default: 50) + // skipDryRun: true // Skip dry run before migrations? (default: false for public nets ) + // }, + + // Useful for private networks + // private: { + // provider: () => new HDWalletProvider(mnemonic, `https://network.io`), + // network_id: 2111, // This network is yours, in the cloud. + // production: true // Treats this network as if it was a public net. (default: false) + // } + }, + + // Set default mocha options here, use special reporters etc. + mocha: { + // timeout: 100000 + }, + + // Configure your compilers + compilers: { + solc: { + // version: "0.5.1", // Fetch exact version from solc-bin (default: truffle's version) + // docker: true, // Use "0.5.1" you've installed locally with docker (default: false) + // settings: { // See the solidity docs for advice about optimization and evmVersion + // optimizer: { + // enabled: false, + // runs: 200 + // }, + // evmVersion: "byzantium" + // } + } + } +} From d4d539ed00c65aeb983aabdb74fda51801b0fb45 Mon Sep 17 00:00:00 2001 From: Ilan Olkies Date: Thu, 26 Dec 2019 17:59:37 -0300 Subject: [PATCH 2/2] Create readme --- README.md | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 88e902f..d03d69f 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,44 @@ # rns-resolver -RNS Resolver. + +RNS Resolver + +## Install + +``` +npm i @rsksmart/rns-resolver +``` + +## Usage + +```solidity +pragma solidity ^0.5.0; + +import "@rsksmart/rns-registry/contracts/AbstractRNS.sol"; + +contract RNSTransfer { + AbstractRNS rns; + + constructor(AbstractRNS _rns) public { + rns = _rns; + } + + function transfer(bytes32 node) public { + address resolver = AbstractAddrResolver(rns.resolver(node)); + address addr = resolver.addr(node); + + addr.transfer(msg.value); + } +} +``` + +--- + +- Public Resolver + - [Docs](https://developers.rsk.co/rif/rns/architecture/RSKResolver/) + - RSK Mainnet: [0x4efd25e3d348f8f25a14fb7655fba6f72edfe93a](https://explorer.rsk.co/address/0x4efd25e3d348f8f25a14fb7655fba6f72edfe93a) + - RSK Testnet: [0x1e7ae43e3503efb886104ace36051ea72b301cdf](https://explorer.testnet.rsk.co/address/0x1e7ae43e3503efb886104ace36051ea72b301cdf) + +- Multi Chain Resolver + - [Docs](https://developers.rsk.co/rif/rns/architecture/MultiCryptoResolver/) + - RSK Mainnet: [0x99a12be4C89CbF6CFD11d1F2c029904a7B644368](https://explorer.rsk.co/address/0x99a12be4C89CbF6CFD11d1F2c029904a7B644368) + - RSK Testnet: [0x404308f2a2eec2cdc3cb53d7d295af11c903414e](https://explorer.testnet.rsk.co/address/0x404308f2a2eec2cdc3cb53d7d295af11c903414e)