@zilliqa-js/crypto
Advanced tools
Comparing version 0.5.0 to 0.6.0
@@ -17,2 +17,3 @@ /// <reference types="node" /> | ||
export * from './signature'; | ||
export * from './bech32'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -29,2 +29,3 @@ "use strict"; | ||
tslib_1.__exportStar(require("./signature"), exports); | ||
tslib_1.__exportStar(require("./bech32"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -57,2 +57,16 @@ /** | ||
/** | ||
* encodeBase58 | ||
* | ||
* @param {string} hex - base 16 encoded string | ||
* @returns {string} - big endian base 58 encoded string | ||
*/ | ||
export declare const encodeBase58: (hex: string) => string; | ||
/** | ||
* decodeBase58 | ||
* | ||
* @param {string} raw - base 58 string | ||
* @returns {string} - big endian base 16 string | ||
*/ | ||
export declare const decodeBase58: (raw: string) => string; | ||
/** | ||
* verifyPrivateKey | ||
@@ -59,0 +73,0 @@ * |
@@ -105,2 +105,71 @@ "use strict"; | ||
/** | ||
* encodeBase58 | ||
* | ||
* @param {string} hex - base 16 encoded string | ||
* @returns {string} - big endian base 58 encoded string | ||
*/ | ||
exports.encodeBase58 = function (hex) { | ||
var clean = hex.toLowerCase().replace('0x', ''); | ||
var tbl = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | ||
var base = new util_1.BN(58); | ||
var zero = new util_1.BN(0); | ||
var x = new util_1.BN(clean, 16); | ||
var res = ''; | ||
while (x.gt(zero)) { | ||
var rem = x.mod(base).toNumber(); // safe, always < 58 | ||
// big endian | ||
res = tbl[rem] + res; | ||
// quotient, remainders thrown away in integer division | ||
x = x.div(base); | ||
} | ||
// convert to big endian in case the input hex is little endian | ||
var hexBE = x.toString('hex', clean.length); | ||
for (var i = 0; i < hexBE.length; i += 2) { | ||
if (hex[i] === '0' && hex[i + 1] === '0') { | ||
res = tbl[0] + res; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
return res; | ||
}; | ||
/** | ||
* decodeBase58 | ||
* | ||
* @param {string} raw - base 58 string | ||
* @returns {string} - big endian base 16 string | ||
*/ | ||
exports.decodeBase58 = function (raw) { | ||
var tbl = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | ||
var base = new util_1.BN(58); | ||
var zero = new util_1.BN(0); | ||
var isBreak = false; | ||
var n = new util_1.BN(0); | ||
var leader = ''; | ||
for (var i = 0; i < raw.length; i++) { | ||
var char = raw.charAt(i); | ||
var weight = new util_1.BN(tbl.indexOf(char)); | ||
n = n.mul(base).add(weight); | ||
// check if padding required | ||
if (!isBreak) { | ||
if (i - 1 > 0 && raw[i - 1] !== '1') { | ||
isBreak = true; | ||
continue; | ||
} | ||
if (char === '1') { | ||
leader += '00'; | ||
} | ||
} | ||
} | ||
if (n.eq(zero)) { | ||
return leader; | ||
} | ||
var res = leader + n.toString('hex'); | ||
if (res.length % 2 !== 0) { | ||
res = '0' + res; | ||
} | ||
return res; | ||
}; | ||
/** | ||
* verifyPrivateKey | ||
@@ -107,0 +176,0 @@ * |
{ | ||
"name": "@zilliqa-js/crypto", | ||
"version": "0.5.0", | ||
"version": "0.6.0", | ||
"description": "Core crypto utilities for signing/verification/hashing Zilliqa transactions.", | ||
@@ -21,3 +21,3 @@ "main": "dist/index.js", | ||
"dependencies": { | ||
"@zilliqa-js/util": "0.5.0", | ||
"@zilliqa-js/util": "0.6.0", | ||
"aes-js": "^3.1.1", | ||
@@ -33,3 +33,3 @@ "bsert": "^0.0.4", | ||
}, | ||
"gitHead": "c9ac41a9624e4797e8a94f8c1d7558bf96f92d25" | ||
"gitHead": "36aca1d0308845a83903e83de88eac3a8d92f1ed" | ||
} |
@@ -129,2 +129,50 @@ # @zilliqa-js/crypto | ||
**Parameters** | ||
`passphrase`: `string` - the passphrase to be used to encrypt the private key. | ||
`keystore`: `KeystoreV3` - the object containing the deserialised JSON obtained from `encryptPrivateKey`. | ||
**Returns** | ||
`Promise<string>` - the hex-encoded private key. | ||
### `toBech32Address(address: string): string` | ||
Encodes a 20-byte hex encoded address as a bech32 address. Non-hex-encoded | ||
strings will cause an error to be thrown. | ||
**Parameters** | ||
`address`: `string` - the 20-byte hex-encoded address. `0x` prefix optional. | ||
**Returns** | ||
`string` - the bech32 encoded address. It is always prefixed by `zil1`, where | ||
`1` is a separator. | ||
**Example** | ||
``` | ||
0x1d19918a737306218b5cbb3241fcdcbd998c3a72 (hex) -> zil1r5verznnwvrzrz6uhveyrlxuhkvccwnju4aehf (bech32) | ||
``` | ||
### `fromBech32Address(address: string): string` | ||
Encodes a a bech32 address as a hex-encoded string. Invalid bech32 addresses | ||
will cause an error to be thrown. | ||
**Parameters** | ||
`address`: `string` - the 42-character bech32 address. | ||
**Returns** | ||
`string` - the checksum 20-byte hex-encoded address. | ||
**Example** | ||
``` | ||
zil1r5verznnwvrzrz6uhveyrlxuhkvccwnju4aehf (bech32) -> 0x1d19918a737306218b5cbb3241fcdcbd998c3a72 (hex) | ||
``` | ||
**Interfaces** | ||
@@ -165,11 +213,1 @@ | ||
``` | ||
**Parameters** | ||
`passphrase`: `string` - the passphrase to be used to encrypt the private key. | ||
`keystore`: `KeystoreV3` - the object containing the deserialised JSON obtained from `encryptPrivateKey`. | ||
**Returns** | ||
`Promise<string>` - the hex-encoded private key. | ||
@@ -39,1 +39,2 @@ import * as schnorr from './schnorr'; | ||
export * from './signature'; | ||
export * from './bech32'; |
@@ -7,2 +7,3 @@ import elliptic from 'elliptic'; | ||
const secp256k1 = elliptic.ec('secp256k1'); | ||
/** | ||
@@ -115,2 +116,79 @@ * getAddressFromPrivateKey | ||
/** | ||
* encodeBase58 | ||
* | ||
* @param {string} hex - base 16 encoded string | ||
* @returns {string} - big endian base 58 encoded string | ||
*/ | ||
export const encodeBase58 = (hex: string): string => { | ||
const clean = hex.toLowerCase().replace('0x', ''); | ||
const tbl = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | ||
const base = new BN(58); | ||
const zero = new BN(0); | ||
let x = new BN(clean, 16); | ||
let res = ''; | ||
while (x.gt(zero)) { | ||
const rem = x.mod(base).toNumber(); // safe, always < 58 | ||
// big endian | ||
res = tbl[rem] + res; | ||
// quotient, remainders thrown away in integer division | ||
x = x.div(base); | ||
} | ||
// convert to big endian in case the input hex is little endian | ||
const hexBE = x.toString('hex', clean.length); | ||
for (let i = 0; i < hexBE.length; i += 2) { | ||
if (hex[i] === '0' && hex[i + 1] === '0') { | ||
res = tbl[0] + res; | ||
} else { | ||
break; | ||
} | ||
} | ||
return res; | ||
}; | ||
/** | ||
* decodeBase58 | ||
* | ||
* @param {string} raw - base 58 string | ||
* @returns {string} - big endian base 16 string | ||
*/ | ||
export const decodeBase58 = (raw: string): string => { | ||
const tbl = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'; | ||
const base = new BN(58); | ||
const zero = new BN(0); | ||
let isBreak = false; | ||
let n = new BN(0); | ||
let leader = ''; | ||
for (let i = 0; i < raw.length; i++) { | ||
const char = raw.charAt(i); | ||
const weight = new BN(tbl.indexOf(char)); | ||
n = n.mul(base).add(weight); | ||
// check if padding required | ||
if (!isBreak) { | ||
if (i - 1 > 0 && raw[i - 1] !== '1') { | ||
isBreak = true; | ||
continue; | ||
} | ||
if (char === '1') { | ||
leader += '00'; | ||
} | ||
} | ||
} | ||
if (n.eq(zero)) { | ||
return leader; | ||
} | ||
let res = leader + n.toString('hex'); | ||
if (res.length % 2 !== 0) { | ||
res = '0' + res; | ||
} | ||
return res; | ||
}; | ||
/** | ||
* verifyPrivateKey | ||
@@ -117,0 +195,0 @@ * |
import { addresses } from './address.fixtures'; | ||
import bech32Tests from './bech32.fixtures.json'; | ||
import { checksummedStore } from './checksum.fixtures'; | ||
@@ -39,2 +40,10 @@ import * as crypto from '../src/index'; | ||
}); | ||
it('should encode and decode to and from bech32', () => { | ||
bech32Tests.forEach(({ b16, b32 }) => { | ||
expect(crypto.fromBech32Address(b32)).toEqual( | ||
crypto.toChecksumAddress(b16), | ||
); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
5004534
62
45677
212
+ Added@zilliqa-js/util@0.6.0(transitive)
- Removed@zilliqa-js/util@0.5.0(transitive)
Updated@zilliqa-js/util@0.6.0