Skip to content

Commit

Permalink
Build 0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
emiliorizzo committed May 14, 2020
1 parent 0fb7c1c commit 7352104
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 14 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ Search network info of checksummed address
- [networks] <code>Array</code> - chainId list
see: https://chainid.network/chains.json

<a name="isZeroAddress"></a>

### isZeroAddress(address, chainId) ⇒ <code>Boolean</code>
Checks if is zero address.

**Kind**: global function
**Params**

- address <code>String</code>
- chainId <code>Integer</code> | <code>String</code>


## Arrays
<a name="arrayIntersection"></a>
Expand Down Expand Up @@ -113,6 +124,29 @@ Check if an array contains all the searched items
- needle <code>Array</code>


## Bytes
<a name="toBuffer"></a>

### toBuffer(value, [encoding]) ⇒ <code>Buffer</code>
Converts strings or numbers to buffer

**Kind**: global function
**Params**

- value <code>string</code> | <code>number</code> | <code>buffer</code>
- [encoding] <code>String</code> <code> = &#x27;hex&#x27;</code>

<a name="bufferToHex"></a>

### bufferToHex(buffer) ⇒ <code>String</code>
Converts buffer to hex string

**Kind**: global function
**Params**

- buffer <code>Buffer</code>


## Hashes
<a name="keccak256"></a>

Expand Down
27 changes: 22 additions & 5 deletions dist/addresses.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.isAddress = isAddress;exports.toChecksumAddress = toChecksumAddress;exports.isValidChecksumAddress = isValidChecksumAddress;exports.isValidAddress = isValidAddress;exports.searchChecksummedNetworks = searchChecksummedNetworks;var _hashes = require("./hashes");
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.zeroAddress = zeroAddress;exports.isAddress = isAddress;exports.toChecksumAddress = toChecksumAddress;exports.isValidChecksumAddress = isValidChecksumAddress;exports.isValidAddress = isValidAddress;exports.searchChecksummedNetworks = searchChecksummedNetworks;exports.isZeroAddress = isZeroAddress;exports.ZERO_ADDRESS = void 0;var _hashes = require("./hashes");
var _strings = require("./strings");
var _bytes = require("./bytes");
var _networks = _interopRequireDefault(require("./networks.json"));function _interopRequireDefault(obj) {return obj && obj.__esModule ? obj : { default: obj };}

function zeroAddress() {
return (0, _strings.add0x)((0, _bytes.bufferToHex)(Buffer.allocUnsafe(20).fill(0)));
}

const ZERO_ADDRESS = zeroAddress();

/**
* @description Check if a string is an address
* @param {String} address
* @returns {Boolean}
*/
* @description Check if a string is an address
* @param {String} address
* @returns {Boolean}
*/exports.ZERO_ADDRESS = ZERO_ADDRESS;
function isAddress(address) {
return /^(0x)?[0-9a-fA-F]{40}$/.test(address);
}
Expand Down Expand Up @@ -59,4 +66,14 @@ function isValidAddress(address, chainId) {
function searchChecksummedNetworks(address, networks) {
networks = networks || _networks.default;
return networks.filter(net => toChecksumAddress(address, net.chainId) === address);
}

/**
* @description Checks if is zero address.
* @param {String} address
* @param {Integer|String} chainId
* @returns {Boolean}
*/
function isZeroAddress(address) {
return address === ZERO_ADDRESS;
}
23 changes: 16 additions & 7 deletions dist/bytes.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.toBuffer = toBuffer;var _strings = require("./strings");






"use strict";Object.defineProperty(exports, "__esModule", { value: true });exports.toBuffer = toBuffer;exports.bufferToHex = bufferToHex;var _strings = require("./strings");

/**
* @description Converts strings or numbers to buffer
* @param {string|number|buffer} value
* @param {String} [encoding='hex']
* @returns {Buffer}
*/
function toBuffer(value, encoding = 'hex') {
if (Buffer.isBuffer(value)) return value;
if (typeof value === 'number') value = value.toString();
value = (0, _strings.remove0x)(value);
if (!encoding && (0, _strings.isHexString)(value)) encoding = 'hex';
return Buffer.from(value, encoding);
}

/**
* @description Converts buffer to hex string
* @param {Buffer} buffer
* @returns {String}
*/
function bufferToHex(buffer) {
return (0, _strings.add0x)(buffer.toString('hex'));
}
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.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rsk-utils",
"version": "0.0.2",
"version": "0.0.3",
"description": "A collection of JS utility functions for RSK",
"main": "dist/index.js",
"scripts": {
Expand Down

0 comments on commit 7352104

Please sign in to comment.