@helium/crypto-react-native
Advanced tools
Comparing version 3.45.0 to 3.50.0
/// <reference types="node" /> | ||
import { KeyType } from './KeyType'; | ||
import { NetType } from './NetType'; | ||
export default class Address { | ||
version: number; | ||
netType: NetType; | ||
keyType: KeyType; | ||
publicKey: Uint8Array; | ||
constructor(version: number, keyType: KeyType, publicKey: Uint8Array); | ||
constructor(version: number, netType: NetType, keyType: KeyType, publicKey: Uint8Array); | ||
get bin(): Buffer; | ||
@@ -9,0 +11,0 @@ get b58(): string; |
@@ -5,11 +5,16 @@ "use strict"; | ||
const KeyType_1 = require("./KeyType"); | ||
const NetType_1 = require("./NetType"); | ||
class Address { | ||
constructor(version, keyType, publicKey) { | ||
constructor(version, netType, keyType, publicKey) { | ||
if (version !== 0) { | ||
throw new Error('unsupported version'); | ||
} | ||
if (!NetType_1.SUPPORTED_NET_TYPES.includes(netType)) { | ||
throw new Error('unsupported net type'); | ||
} | ||
if (!KeyType_1.SUPPORTED_KEY_TYPES.includes(keyType)) { | ||
throw new Error('unsupported key type'); | ||
} | ||
if (version !== 0) { | ||
throw new Error('unsupported version'); | ||
} | ||
this.version = version; | ||
this.netType = netType; | ||
this.keyType = keyType; | ||
@@ -20,3 +25,4 @@ this.publicKey = publicKey; | ||
return Buffer.concat([ | ||
Buffer.from([this.keyType]), | ||
// eslint-disable-next-line no-bitwise | ||
Buffer.from([this.netType | this.keyType]), | ||
Buffer.from(this.publicKey), | ||
@@ -30,12 +36,14 @@ ]); | ||
const version = utils_1.bs58Version(b58); | ||
const netType = utils_1.bs58NetType(b58); | ||
const keyType = utils_1.bs58KeyType(b58); | ||
const bin = utils_1.bs58ToBin(b58); | ||
const publicKey = Buffer.from(bin).slice(1); | ||
return new Address(version, keyType, publicKey); | ||
const publicKey = utils_1.bs58PublicKey(b58); | ||
return new Address(version, netType, keyType, publicKey); | ||
} | ||
static fromBin(bin) { | ||
const version = 0; | ||
const keyType = bin[0]; | ||
const byte = bin[0]; | ||
const netType = utils_1.byteToNetType(byte); | ||
const keyType = utils_1.byteToKeyType(byte); | ||
const publicKey = bin.slice(1, bin.length); | ||
return new Address(version, keyType, publicKey); | ||
return new Address(version, netType, keyType, publicKey); | ||
} | ||
@@ -42,0 +50,0 @@ static isValid(b58) { |
@@ -9,3 +9,5 @@ /** | ||
export { default as Address } from './Address'; | ||
export * as NetType from './NetType'; | ||
export * as KeyType from './KeyType'; | ||
export * as utils from './utils'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -30,3 +30,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.utils = exports.Address = exports.Keypair = exports.Mnemonic = void 0; | ||
exports.utils = exports.KeyType = exports.NetType = exports.Address = exports.Keypair = exports.Mnemonic = void 0; | ||
var Mnemonic_1 = require("./Mnemonic"); | ||
@@ -38,4 +38,6 @@ Object.defineProperty(exports, "Mnemonic", { enumerable: true, get: function () { return __importDefault(Mnemonic_1).default; } }); | ||
Object.defineProperty(exports, "Address", { enumerable: true, get: function () { return __importDefault(Address_1).default; } }); | ||
exports.NetType = __importStar(require("./NetType")); | ||
exports.KeyType = __importStar(require("./KeyType")); | ||
exports.utils = __importStar(require("./utils")); | ||
global.Buffer = require('safe-buffer').Buffer; | ||
//# sourceMappingURL=index.js.map |
/// <reference types="node" /> | ||
import Mnemonic from './Mnemonic'; | ||
import Address from './Address'; | ||
import { KeyType } from './KeyType'; | ||
import { NetType } from './NetType'; | ||
interface SodiumKeyPair { | ||
keyType?: KeyType; | ||
sk: string; | ||
@@ -12,8 +15,10 @@ pk: string; | ||
privateKey: Buffer; | ||
constructor(keypair: SodiumKeyPair); | ||
keyType: KeyType; | ||
netType: NetType; | ||
constructor(keypair: SodiumKeyPair, netType?: NetType); | ||
get address(): Address; | ||
static makeRandom(): Promise<Keypair>; | ||
static fromWords(words: Array<string>): Promise<Keypair>; | ||
static fromMnemonic(mnenomic: Mnemonic): Promise<Keypair>; | ||
static fromEntropy(entropy: Uint8Array | Buffer): Promise<Keypair>; | ||
static makeRandom(netType?: NetType): Promise<Keypair>; | ||
static fromWords(words: Array<string>, netType?: NetType): Promise<Keypair>; | ||
static fromMnemonic(mnenomic: Mnemonic, netType?: NetType): Promise<Keypair>; | ||
static fromEntropy(entropy: Uint8Array | Buffer, netType?: NetType): Promise<Keypair>; | ||
sign(message: string | Uint8Array): Promise<Buffer>; | ||
@@ -20,0 +25,0 @@ } |
@@ -10,27 +10,30 @@ "use strict"; | ||
const KeyType_1 = require("./KeyType"); | ||
const NetType_1 = require("./NetType"); | ||
// extend SodiumKeyPair? | ||
class Keypair { | ||
constructor(keypair) { | ||
constructor(keypair, netType) { | ||
this.keypair = keypair; | ||
this.publicKey = Buffer.from(keypair.pk, 'base64'); | ||
this.privateKey = Buffer.from(keypair.sk, 'base64'); | ||
this.keyType = keypair.keyType || KeyType_1.ED25519_KEY_TYPE; | ||
this.netType = netType || NetType_1.MAINNET; | ||
} | ||
get address() { | ||
return new Address_1.default(0, KeyType_1.ED25519_KEY_TYPE, this.publicKey); | ||
return new Address_1.default(0, this.netType, KeyType_1.ED25519_KEY_TYPE, this.publicKey); | ||
} | ||
static async makeRandom() { | ||
static async makeRandom(netType) { | ||
const keypair = await react_native_sodium_1.default.crypto_sign_keypair(); | ||
return new Keypair(keypair); | ||
return new Keypair(keypair, netType); | ||
} | ||
static async fromWords(words) { | ||
static async fromWords(words, netType) { | ||
const mnenomic = new Mnemonic_1.default(words); | ||
const keypair = await this.fromMnemonic(mnenomic); | ||
const keypair = await this.fromMnemonic(mnenomic, netType); | ||
return keypair; | ||
} | ||
static async fromMnemonic(mnenomic) { | ||
static async fromMnemonic(mnenomic, netType) { | ||
const entropy = mnenomic.toEntropy(); | ||
const seed = entropy.length === 16 ? Buffer.concat([entropy, entropy]) : entropy; | ||
return Keypair.fromEntropy(seed); | ||
return Keypair.fromEntropy(seed, netType); | ||
} | ||
static async fromEntropy(entropy) { | ||
static async fromEntropy(entropy, netType) { | ||
const entropyBuffer = Buffer.from(entropy); | ||
@@ -41,3 +44,3 @@ if (Buffer.byteLength(entropyBuffer) !== 32) { | ||
const keypair = await react_native_sodium_1.default.crypto_sign_seed_keypair(entropyBuffer.toString('base64')); | ||
return new Keypair(keypair); | ||
return new Keypair(keypair, netType); | ||
} | ||
@@ -44,0 +47,0 @@ async sign(message) { |
/// <reference types="node" /> | ||
import { NetType } from './NetType'; | ||
import { KeyType } from './KeyType'; | ||
export declare const randomBytes: (n: number) => Promise<Buffer>; | ||
@@ -9,4 +11,8 @@ export declare const lpad: (str: string | any[], padString: string, length: number) => string | any[]; | ||
export declare const bs58ToBin: (bs58Address: string) => Buffer; | ||
export declare const bs58KeyType: (bs58Address: string) => number; | ||
export declare const byteToNetType: (byte: number) => NetType; | ||
export declare const byteToKeyType: (byte: number) => KeyType; | ||
export declare const bs58NetType: (bs58Address: string) => NetType; | ||
export declare const bs58KeyType: (bs58Address: string) => KeyType; | ||
export declare const bs58Version: (bs58Address: string) => number; | ||
export declare const bs58PublicKey: (bs58Address: string) => Buffer; | ||
//# sourceMappingURL=utils.d.ts.map |
@@ -6,3 +6,4 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.bs58Version = exports.bs58KeyType = exports.bs58ToBin = exports.bs58CheckEncode = exports.deriveChecksumBits = exports.binaryToByte = exports.bytesToBinary = exports.lpad = exports.randomBytes = void 0; | ||
exports.bs58PublicKey = exports.bs58Version = exports.bs58KeyType = exports.bs58NetType = exports.byteToKeyType = exports.byteToNetType = exports.bs58ToBin = exports.bs58CheckEncode = exports.deriveChecksumBits = exports.binaryToByte = exports.bytesToBinary = exports.lpad = exports.randomBytes = void 0; | ||
/* eslint-disable no-bitwise */ | ||
const react_native_sodium_1 = __importDefault(require("react-native-sodium")); | ||
@@ -67,6 +68,16 @@ const js_sha256_1 = require("js-sha256"); | ||
exports.bs58ToBin = bs58ToBin; | ||
const byteToNetType = (byte) => byte & 0xf0; | ||
exports.byteToNetType = byteToNetType; | ||
const byteToKeyType = (byte) => byte & 0x0f; | ||
exports.byteToKeyType = byteToKeyType; | ||
const bs58NetType = (bs58Address) => { | ||
const bin = exports.bs58ToBin(bs58Address); | ||
const byte = Buffer.from(bin).slice(0, 1)[0]; | ||
return exports.byteToNetType(byte); | ||
}; | ||
exports.bs58NetType = bs58NetType; | ||
const bs58KeyType = (bs58Address) => { | ||
const bin = exports.bs58ToBin(bs58Address); | ||
const keyType = Buffer.from(bin).slice(0, 1)[0]; | ||
return keyType; | ||
const byte = Buffer.from(bin).slice(0, 1)[0]; | ||
return exports.byteToKeyType(byte); | ||
}; | ||
@@ -80,2 +91,8 @@ exports.bs58KeyType = bs58KeyType; | ||
exports.bs58Version = bs58Version; | ||
const bs58PublicKey = (bs58Address) => { | ||
const bin = exports.bs58ToBin(bs58Address); | ||
const publicKey = Buffer.from(bin).slice(1); | ||
return publicKey; | ||
}; | ||
exports.bs58PublicKey = bs58PublicKey; | ||
//# sourceMappingURL=utils.js.map |
{ | ||
"name": "@helium/crypto-react-native", | ||
"version": "3.45.0", | ||
"version": "3.50.0", | ||
"description": "Cryptography utilities including mnemonics, keypairs and base58-check encoding for React Native", | ||
@@ -39,3 +39,3 @@ "keywords": [ | ||
}, | ||
"gitHead": "4884146d82acb1ed66d995099e60c16872eaaf6a" | ||
"gitHead": "92f545728b81e03c3cf238b4b98e743e87c003e0" | ||
} |
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
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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
71667
32
2457