webpack bundle and changed requires to imports

This commit is contained in:
Miro Metsänheimo 2019-10-14 22:17:38 +03:00
commit 759492dcdf
10 changed files with 4374 additions and 38 deletions

View file

@ -1,8 +1,10 @@
.git/ .git/
node_modules/ node_modules/
test/ test/
src/
.gitignore .gitignore
.editorconfig .editorconfig
.travis.yml .travis.yml
src webpack.config.json
tsconfig.json tsconfig.json
package-lock.json

View file

@ -1,5 +1,7 @@
# nanocurrency-web # nanocurrency-web
[![Build Status](https://travis-ci.org/numsu/nanocurrency-web-js.svg?branch=master)](https://travis-ci.org/numsu/nanocurrency-web-js) [![Build Status](https://travis-ci.org/numsu/nanocurrency-web-js.svg?branch=master)](https://travis-ci.org/numsu/nanocurrency-web-js)
[![GitHub license](https://img.shields.io/github/license/numsu/nanocurrency-web-js)](https://github.com/numsu/nanocurrency-web-js/blob/master/LICENSE)
[![npm version](https://badge.fury.io/js/nanocurrency-web.svg)](https://badge.fury.io/js/nanocurrency-web)
Toolkit for Nano cryptocurrency client side offline implementations allowing you to build web- and mobile applications using Nano without compromising the user's keys by sending them out of their own device. Toolkit for Nano cryptocurrency client side offline implementations allowing you to build web- and mobile applications using Nano without compromising the user's keys by sending them out of their own device.

View file

@ -1,6 +1,7 @@
import { Convert } from './util/convert' import { Convert } from './util/convert'
const CryptoJS = require('crypto-js') //@ts-ignore
import { enc, algo } from 'crypto-js'
const ED25519_CURVE = 'ed25519 seed' const ED25519_CURVE = 'ed25519 seed'
const HARDENED_OFFSET = 0x80000000 const HARDENED_OFFSET = 0x80000000
@ -30,8 +31,8 @@ export default class Bip32KeyDerivation {
private getKeyFromSeed = (): Chain => { private getKeyFromSeed = (): Chain => {
return this.derive( return this.derive(
CryptoJS.enc.Hex.parse(this.seed), enc.Hex.parse(this.seed),
CryptoJS.enc.Utf8.parse(ED25519_CURVE)) enc.Utf8.parse(ED25519_CURVE))
} }
private CKDPriv = ({ key, chainCode }: Chain, index: number) => { private CKDPriv = ({ key, chainCode }: Chain, index: number) => {
@ -43,12 +44,12 @@ export default class Bip32KeyDerivation {
const data = '00' + key + Convert.ab2hex(new Uint8Array(ib).buffer) const data = '00' + key + Convert.ab2hex(new Uint8Array(ib).buffer)
return this.derive( return this.derive(
CryptoJS.enc.Hex.parse(data), enc.Hex.parse(data),
CryptoJS.enc.Hex.parse(chainCode)) enc.Hex.parse(chainCode))
} }
private derive = (data: string, base: string): Chain => { private derive = (data: string, base: string): Chain => {
const hmac = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA512, base) const hmac = algo.HMAC.create(algo.SHA512, base)
const I = hmac.update(data).finalize().toString() const I = hmac.update(data).finalize().toString()
const IL = I.slice(0, I.length / 2) const IL = I.slice(0, I.length / 2)
const IR = I.slice(I.length / 2) const IR = I.slice(I.length / 2)

View file

@ -2,7 +2,8 @@ import words from './words'
import { Util } from './util/util' import { Util } from './util/util'
import { Convert } from './util/convert' import { Convert } from './util/convert'
const CryptoJS = require('crypto-js') //@ts-ignore
import { algo, enc, lib, PBKDF2, SHA256 } from 'crypto-js'
export default class Bip39Mnemonic { export default class Bip39Mnemonic {
@ -82,23 +83,23 @@ export default class Bip39Mnemonic {
const normalizedMnemonic = Util.normalizeUTF8(mnemonic) const normalizedMnemonic = Util.normalizeUTF8(mnemonic)
const normalizedPassword = 'mnemonic' + Util.normalizeUTF8(this.password) const normalizedPassword = 'mnemonic' + Util.normalizeUTF8(this.password)
return CryptoJS.PBKDF2( return PBKDF2(
normalizedMnemonic, normalizedMnemonic,
normalizedPassword, normalizedPassword,
{ {
keySize: 512 / 32, keySize: 512 / 32,
iterations: 2048, iterations: 2048,
hasher: CryptoJS.algo.SHA512, hasher: algo.SHA512,
}) })
.toString(CryptoJS.enc.Hex) .toString(enc.Hex)
} }
private randomHex = (length: number): string => { private randomHex = (length: number): string => {
return CryptoJS.lib.WordArray.random(length / 2).toString() return lib.WordArray.random(length / 2).toString()
} }
private calculateChecksum = (entropyHex: string): string => { private calculateChecksum = (entropyHex: string): string => {
const entropySha256 = CryptoJS.SHA256(CryptoJS.enc.Hex.parse(entropyHex)).toString() const entropySha256 = SHA256(enc.Hex.parse(entropyHex)).toString()
return entropySha256.substr(0, entropySha256.length / 32) return entropySha256.substr(0, entropySha256.length / 32)
} }

View file

@ -4,7 +4,8 @@ import { NanoAddress } from './nano-address'
import NanoConverter from './nano-converter' import NanoConverter from './nano-converter'
import { Convert } from './util/convert' import { Convert } from './util/convert'
const blake = require('blakejs') //@ts-ignore
import { blake2b, blake2bInit, blake2bUpdate, blake2bFinal } from 'blakejs'
export default class BlockSigner { export default class BlockSigner {
@ -44,14 +45,14 @@ export default class BlockSigner {
} }
private generateHash(preamble: string, account: string, previous: string, representative: string, balance: string, link: string) { private generateHash(preamble: string, account: string, previous: string, representative: string, balance: string, link: string) {
const ctx = blake.blake2bInit(32, undefined) const ctx = blake2bInit(32, undefined)
blake.blake2bUpdate(ctx, Convert.hex2ab(preamble)) blake2bUpdate(ctx, Convert.hex2ab(preamble))
blake.blake2bUpdate(ctx, Convert.hex2ab(account)) blake2bUpdate(ctx, Convert.hex2ab(account))
blake.blake2bUpdate(ctx, Convert.hex2ab(previous)) blake2bUpdate(ctx, Convert.hex2ab(previous))
blake.blake2bUpdate(ctx, Convert.hex2ab(representative)) blake2bUpdate(ctx, Convert.hex2ab(representative))
blake.blake2bUpdate(ctx, Convert.hex2ab(balance)) blake2bUpdate(ctx, Convert.hex2ab(balance))
blake.blake2bUpdate(ctx, Convert.hex2ab(link)) blake2bUpdate(ctx, Convert.hex2ab(link))
return blake.blake2bFinal(ctx) return blake2bFinal(ctx)
} }
private nanoAddressToHexString(addr: string): string { private nanoAddressToHexString(addr: string): string {
@ -60,7 +61,7 @@ export default class BlockSigner {
if (isValid) { if (isValid) {
const keyBytes = this.nanoAddress.decodeNanoBase32(addr.substring(0, 52)) const keyBytes = this.nanoAddress.decodeNanoBase32(addr.substring(0, 52))
const hashBytes = this.nanoAddress.decodeNanoBase32(addr.substring(52, 60)) const hashBytes = this.nanoAddress.decodeNanoBase32(addr.substring(52, 60))
const blakeHash = blake.blake2b(keyBytes, undefined, 5).reverse() const blakeHash = blake2b(keyBytes, undefined, 5).reverse()
if (Convert.ab2hex(hashBytes) == Convert.ab2hex(blakeHash)) { if (Convert.ab2hex(hashBytes) == Convert.ab2hex(blakeHash)) {
const key = Convert.ab2hex(keyBytes).toUpperCase() const key = Convert.ab2hex(keyBytes).toUpperCase()
return key return key

View file

@ -1,7 +1,8 @@
import { Convert } from './util/convert' import { Convert } from './util/convert'
import { Curve25519 } from './util/curve25519' import { Curve25519 } from './util/curve25519'
const blake = require('blakejs') //@ts-ignore
import { blake2b } from 'blakejs'
export class Ed25519 { export class Ed25519 {
@ -101,9 +102,7 @@ export class Ed25519 {
generateKeys(seed: string): KeyPair { generateKeys(seed: string): KeyPair {
const pk = new Uint8Array(32) const pk = new Uint8Array(32)
const p = [this.curve.gf(), this.curve.gf(), this.curve.gf(), this.curve.gf()] const p = [this.curve.gf(), this.curve.gf(), this.curve.gf(), this.curve.gf()]
const h = blake const h = blake2b(Convert.hex2ab(seed), undefined, 64).slice(0, 32)
.blake2b(Convert.hex2ab(seed), undefined, 64)
.slice(0, 32)
h[0] &= 0xf8 h[0] &= 0xf8
h[31] &= 0x7f h[31] &= 0x7f
@ -210,7 +209,7 @@ export class Ed25519 {
input[i] = m[i] input[i] = m[i]
} }
const hash = blake.blake2b(input) const hash = blake2b(input)
for (let i = 0; i < 64; ++i) { for (let i = 0; i < 64; ++i) {
out[i] = hash[i] out[i] = hash[i]
} }

View file

@ -1,6 +1,7 @@
import { Convert } from './util/convert' import { Convert } from './util/convert'
const blake = require('blakejs') //@ts-ignore
import { blake2b } from 'blakejs'
export class NanoAddress { export class NanoAddress {
@ -9,10 +10,7 @@ export class NanoAddress {
deriveAddress = (publicKey: string): string => { deriveAddress = (publicKey: string): string => {
const publicKeyBytes = Convert.hex2ab(publicKey) const publicKeyBytes = Convert.hex2ab(publicKey)
const checksum = blake const checksum = blake2b(publicKeyBytes, undefined, 5).reverse()
.blake2b(publicKeyBytes, undefined, 5)
.reverse()
const encoded = this.encodeNanoBase32(publicKeyBytes) const encoded = this.encodeNanoBase32(publicKeyBytes)
const encodedChecksum = this.encodeNanoBase32(checksum) const encodedChecksum = this.encodeNanoBase32(checksum)

4300
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -1,6 +1,6 @@
{ {
"name": "nanocurrency-web", "name": "nanocurrency-web",
"version": "1.0.1", "version": "1.0.2",
"description": "Toolset for Nano cryptocurrency client side offline integrations", "description": "Toolset for Nano cryptocurrency client side offline integrations",
"author": "Miro Metsänheimo <miro@metsanheimo.fi>", "author": "Miro Metsänheimo <miro@metsanheimo.fi>",
"license": "MIT", "license": "MIT",
@ -12,10 +12,18 @@
"bugs": { "bugs": {
"url": "https://github.com/numsu/nanocurrency-web-js/issues" "url": "https://github.com/numsu/nanocurrency-web-js/issues"
}, },
"keywords": [
"nano",
"currency",
"mnemonic",
"crypto"
],
"main": "dist/index.js", "main": "dist/index.js",
"types": "dist/index.d.ts", "types": "dist/index.d.ts",
"unpkg": "dist/index.min.js",
"scripts": { "scripts": {
"build": "tsc", "build": "tsc && npm run build:webpack",
"build:webpack": "webpack",
"test": "mocha --reporter spec" "test": "mocha --reporter spec"
}, },
"dependencies": { "dependencies": {
@ -27,6 +35,9 @@
"@types/node": "12.7.12", "@types/node": "12.7.12",
"chai": "4.2.0", "chai": "4.2.0",
"mocha": "6.2.1", "mocha": "6.2.1",
"typescript": "3.6.3" "ts-loader": "6.2.0",
"typescript": "3.6.3",
"webpack": "4.41.1",
"webpack-cli": "3.3.9"
} }
} }

23
webpack.config.js Normal file
View file

@ -0,0 +1,23 @@
const path = require('path')
module.exports = {
entry: './index.ts',
mode: 'production',
module: {
rules: [
{
test: /\.ts?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve: {
extensions: ['.ts', '.js', '.json'],
},
output: {
filename: 'index.min.js',
path: path.resolve(__dirname, 'dist'),
libraryTarget: 'commonjs2',
},
}