Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
kieled committed Dec 19, 2023
0 parents commit 7726676
Show file tree
Hide file tree
Showing 27 changed files with 15,702 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
src
package-lock.json
Binary file added bun.lockb
Binary file not shown.
72 changes: 72 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
{
"name": "bellhdw",
"version": "0.2.1",
"description": "Client-side Bitcoin JavaScript library",
"main": "./src/index.js",
"types": "./src/index.d.ts",
"scripts": {
"audit": "better-npm-audit audit -l high",
"build": "npm run clean && tsc -p ./tsconfig.json && npm run formatjs",
"build:tests": "npm run clean:jstests && tsc -p ./tests/tsconfig.json",
"clean": "rimraf src",
"clean:jstests": "rimraf 'tests/**/!(ts-node-register)*.js'",
"coverage-report": "npm run build && npm run nobuild:coverage-report",
"coverage-html": "npm run build && npm run nobuild:coverage-html",
"coverage": "npm run build && npm run nobuild:coverage",
"format": "npm run prettier -- --write",
"formatjs": "npm run prettierjs -- --write",
"format:ci": "npm run prettier -- --check && npm run prettierjs -- --check",
"gitdiff:ci": "npm run build && git diff --exit-code",
"integration": "npm run build && npm run nobuild:integration",
"lint": "eslint ts_src/** src/**/*.js",
"lint:tests": "eslint tests/**/*.spec.ts",
"mocha:ts": "mocha --recursive --require tests/ts-node-register",
"nobuild:coverage-report": "nyc report --reporter=lcov",
"nobuild:coverage-html": "nyc report --reporter=html",
"nobuild:coverage": "npm run build:tests && nyc --check-coverage --branches 85 --functions 90 --lines 90 mocha && npm run clean:jstests",
"nobuild:integration": "npm run mocha:ts -- --timeout 50000 'tests/integration/*.ts'",
"test": "npm run mocha:ts -- 'tests/*.ts'",
"prettier": "prettier \"ts_src/**/*.ts\" \"tests/**/*.ts\" --ignore-path ./.prettierignore",
"prettierjs": "prettier \"src/**/*.js\" --ignore-path ./.prettierignore"
},
"repository": {
"type": "git"
},
"files": [
"src"
],
"dependencies": {
"@noble/hashes": "^1.3.2",
"@types/bitcore-lib": "^0.15.6",
"@types/hdkey": "^2.0.3",
"belcoinjs-lib": "^0.0.3",
"bells-secp256k1": "^0.0.5",
"belpair": "^0.0.3",
"bip39": "^3.1.0",
"bitcore-lib": "^10.0.21",
"bn.js": "^5.2.1",
"hdkey": "^2.1.0"
},
"devDependencies": {
"@types/bn.js": "^5.1.1",
"@types/bs58": "^4.0.0",
"@types/bs58check": "^2.1.0",
"@types/chai": "^4.3.6",
"@types/mocha": "^5.2.7",
"@types/node": "^20.5.9",
"@types/proxyquire": "^1.3.28",
"@types/randombytes": "^2.0.0",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"better-npm-audit": "^3.7.3",
"chai": "^4.3.8",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"prettier": "^2.8.0",
"randombytes": "^2.1.0",
"rimraf": "^2.6.3",
"ts-node": "^8.3.0",
"typescript": "^4.4.4"
},
"license": "MIT"
}
23 changes: 23 additions & 0 deletions ts_src/hd/base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { payments } from "belcoinjs-lib";
import { AddressType } from "./types";

export class BaseWallet {
addressType?: AddressType;

getAddress(publicKey: Uint8Array) {
if (this.addressType === undefined)
throw new Error("addressType of keyring is not specified");
switch (this.addressType) {
case AddressType.P2WPKH:
return payments.p2wpkh({ pubkey: Buffer.from(publicKey) }).address;
case AddressType.P2SH_P2WPKH:
return payments.p2sh({
redeem: payments.p2wpkh({ pubkey: Buffer.from(publicKey) }),
}).address;
case AddressType.P2PKH as any:
return payments.p2pkh({ pubkey: Buffer.from(publicKey) }).address;
default:
throw new Error("Invalid AddressType");
}
}
}
17 changes: 17 additions & 0 deletions ts_src/hd/common.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ripemd160 } from "@noble/hashes/ripemd160";
import { PRIVKEY_SIZE, PUBKEY_SIZE } from "../protocol/policy";
import { sha256 } from "@noble/hashes/sha256";
import { utf8ToBytes } from "@noble/hashes/utils";

export const MIN_ENTROPY: number = 128;
export const MAX_ENTROPY: number = 512;
export const ZERO_KEY = Buffer.allocUnsafe(PUBKEY_SIZE);
export const ZERO_PRIVKEY = Buffer.allocUnsafe(PRIVKEY_SIZE);
export const SEED_SALT = utf8ToBytes("Tidecoin seed");

export const hash160 = (value: string | Uint8Array) => ripemd160(sha256(value));
export const hash256 = (value: string | Uint8Array) => sha256(sha256(value));
export const assert = (exp: boolean | number, message?: string) => {
if (exp) return true;
throw new Error(message);
};
17 changes: 17 additions & 0 deletions ts_src/hd/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import HDPrivateKey from "./private";
import SimpleKey from "./simple";
import { AddressType, Keyring } from "./types";


export async function fromMnemonic(mnemonic: string): Promise<HDPrivateKey> {
return await HDPrivateKey.fromMnemonic(mnemonic);
}

export function fromPrivateKey(privateKey: Uint8Array): SimpleKey {
return new SimpleKey(privateKey);
}

export { HDPrivateKey, SimpleKey };
export * as types from "./types";
export { default as englishWords } from "./words/english";
export { AddressType, Keyring };
Loading

0 comments on commit 7726676

Please sign in to comment.