Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
Merge pull request #2 from rnsdomains/npm
Browse files Browse the repository at this point in the history
Npm
  • Loading branch information
ilanolkies authored Dec 26, 2019
2 parents 61f1a8a + d4d539e commit fca118d
Show file tree
Hide file tree
Showing 19 changed files with 990 additions and 1 deletion.
22 changes: 22 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sol linguist-language=Solidity
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
build/
44 changes: 43 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
10 changes: 10 additions & 0 deletions contracts/AbstractAddrResolver.sol
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions contracts/AbstractMultiChainResolver.sol
Original file line number Diff line number Diff line change
@@ -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);
}
10 changes: 10 additions & 0 deletions contracts/AbstractPublicResolver.sol
Original file line number Diff line number Diff line change
@@ -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);
}
86 changes: 86 additions & 0 deletions contracts/AddressUtil.sol
Original file line number Diff line number Diff line change
@@ -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();
}
}
6 changes: 6 additions & 0 deletions contracts/Dummy.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pragma solidity ^0.5.0;

import "@rsksmart/rns-registry/contracts/RNS.sol";

contract Dummy {
}
23 changes: 23 additions & 0 deletions contracts/Migrations.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
112 changes: 112 additions & 0 deletions contracts/MultiChainResolver.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading

0 comments on commit fca118d

Please sign in to comment.