Skip to content

Commit

Permalink
Add microbundle dependency to build the package for production
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim-tolo committed Mar 7, 2020
1 parent e9d627b commit a92e155
Show file tree
Hide file tree
Showing 7 changed files with 8,491 additions and 5,112 deletions.
13,472 changes: 8,409 additions & 5,063 deletions package-lock.json

Large diffs are not rendered by default.

7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -2,10 +2,12 @@
"name": "@ensdomains/address-encoder",
"version": "0.1.3",
"description": "Encodes and decodes address formats for various cryptocurrencies",
"source": "src/index.ts",
"main": "lib/index.js",
"module": "lib/index.module.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc",
"build": "microbundle",
"format": "prettier --write \"src/**/*.ts\" \"src/**/*.js\"",
"lint": "tslint -p tsconfig.json",
"prepare": "npm run build",
Expand All @@ -30,6 +32,7 @@
"@types/node": "^12.7.12",
"browserify": "^16.5.0",
"jest": "^24.9.0",
"microbundle": "^0.12.0-next.8",
"prettier": "^1.18.2",
"ts-jest": "^24.1.0",
"tslint": "^5.20.0",
Expand All @@ -40,8 +43,10 @@
"@types/cashaddrjs": "^0.3.0",
"bech32": "^1.1.3",
"blakejs": "^1.1.0",
"bs58": "^4.0.1",
"cashaddrjs": "^0.3.8",
"crypto-addr-codec": "^0.1.4",
"sha.js": "^2.4.11",
"tsify": "^4.0.1"
}
}
5 changes: 5 additions & 0 deletions src/@types/bs58/index.d.ts
@@ -0,0 +1,5 @@
declare module 'bs58' {
export function encode(data: Buffer): string;
export function decode(data: string): Buffer;
export function decodeUnsafe(data: string): Buffer;
}
7 changes: 7 additions & 0 deletions src/@types/sha.js/index.d.ts
@@ -0,0 +1,7 @@
declare module 'sha.js/sha256' {
import { Hash } from 'crypto';

export default class Sha256 {
public update(buffer: Buffer): Hash;
}
}
46 changes: 0 additions & 46 deletions src/bs58.js

This file was deleted.

62 changes: 62 additions & 0 deletions src/bs58.ts
@@ -0,0 +1,62 @@
import base58 from 'bs58';
import Sha256 from 'sha.js/sha256';

function sha256x2(buffer: Buffer): Buffer {
const tmp = new Sha256().update(buffer).digest();

return new Sha256().update(tmp).digest();
}

const bs58Check = (checksumFn: (buffer: Buffer) => Buffer) => {
function decodeRaw(buffer: Buffer): Buffer | undefined {
const payload = buffer.slice(0, -4);
const checksum = buffer.slice(-4);
const newChecksum = checksumFn(payload);
/* tslint:disable:no-bitwise */
if (
(checksum[0] ^ newChecksum[0]) |
(checksum[1] ^ newChecksum[1]) |
(checksum[2] ^ newChecksum[2]) |
(checksum[3] ^ newChecksum[3])
) {
return;
}

return payload;
}

return {
// Encode a buffer as a base58-check encoded string
encode(payload: Buffer): string {
const checksum = checksumFn(payload);

return base58.encode(Buffer.concat([payload, checksum], payload.length + 4));
},
// Decode a base58-check encoded string to a buffer, no result if checksum is wrong
decodeUnsafe(data: string): Buffer | undefined {
const buffer = base58.decodeUnsafe(data);

if (!buffer) {
return;
}

return decodeRaw(buffer);
},
decode(data: string): Buffer {
const buffer = base58.decode(data);
const payload = decodeRaw(buffer);

if (!payload) {
throw new Error('Invalid checksum');
}

return payload;
},
};
};

const bs58 = bs58Check(sha256x2);

export default bs58;

export const { encode, decode, decodeUnsafe } = bs58;
4 changes: 2 additions & 2 deletions tsconfig.json
@@ -1,7 +1,7 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"module": "ES2015",
"declaration": true,
"outDir": "./lib",
"strict": true,
Expand All @@ -10,4 +10,4 @@
},
"include": ["src"],
"exclude": ["node_modules", "**/__tests__/*"]
}
}

0 comments on commit a92e155

Please sign in to comment.