@stacks/encryption
Advanced tools
Comparing version 4.2.0-beta.4 to 4.2.0-beta.5
/// <reference types="node" /> | ||
export declare function makeECPrivateKey(): string; | ||
export declare function base58CheckDecode(btcAddress: string): { | ||
version: number; | ||
hash: Buffer; | ||
}; | ||
export declare function base58Encode(hash: Buffer): string; | ||
export declare function hashToBase58Check(hash: Buffer): string; | ||
export declare function base58CheckEncode(version: number, hash: Buffer): string; | ||
export declare function publicKeyToAddress(publicKey: string | Buffer): string; | ||
@@ -6,0 +10,0 @@ export declare function getPublicKeyFromPrivate(privateKey: string | Buffer): string; |
@@ -17,2 +17,19 @@ import { hmac } from '@noble/hashes/hmac'; | ||
} | ||
export function base58CheckDecode(btcAddress) { | ||
const buffer = Buffer.from(base58.decode(btcAddress)); | ||
const payload = buffer.slice(0, -4); | ||
const checksum = buffer.slice(-4); | ||
const newChecksum = sha256(sha256(payload)); | ||
if ((checksum[0] ^ newChecksum[0]) | | ||
(checksum[1] ^ newChecksum[1]) | | ||
(checksum[2] ^ newChecksum[2]) | | ||
(checksum[3] ^ newChecksum[3])) { | ||
throw new Error('Invalid checksum'); | ||
} | ||
if (payload.length !== 21) | ||
throw new TypeError('Invalid address length'); | ||
const version = payload.readUInt8(0); | ||
const hash = payload.slice(1); | ||
return { version, hash }; | ||
} | ||
export function base58Encode(hash) { | ||
@@ -22,4 +39,4 @@ const checksum = sha256(sha256(hash)); | ||
} | ||
export function hashToBase58Check(hash) { | ||
return base58Encode(Buffer.from([BITCOIN_PUBKEYHASH, ...hash].slice(0, 21))); | ||
export function base58CheckEncode(version, hash) { | ||
return base58Encode(Buffer.from([version, ...hash].slice(0, 21))); | ||
} | ||
@@ -29,3 +46,3 @@ export function publicKeyToAddress(publicKey) { | ||
const publicKeyHash160 = hashRipemd160(hashSha256Sync(publicKeyBuffer)); | ||
return hashToBase58Check(publicKeyHash160); | ||
return base58CheckEncode(BITCOIN_PUBKEYHASH, publicKeyHash160); | ||
} | ||
@@ -32,0 +49,0 @@ export function getPublicKeyFromPrivate(privateKey) { |
@@ -1,7 +0,7 @@ | ||
import { sha256 } from 'sha.js'; | ||
import { encode, decode, encodingLength } from 'varuint-bitcoin'; | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { Buffer } from '@stacks/common'; | ||
import { decode, encode, encodingLength } from 'varuint-bitcoin'; | ||
const chainPrefix = '\x18Stacks Message Signing:\n'; | ||
export function hashMessage(message) { | ||
return new sha256().update(encodeMessage(message)).digest(); | ||
return Buffer.from(sha256(encodeMessage(message))); | ||
} | ||
@@ -8,0 +8,0 @@ export function encodeMessage(message) { |
@@ -0,3 +1,4 @@ | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { sha512 } from '@noble/hashes/sha512'; | ||
import { Buffer } from '@stacks/common'; | ||
import { sha256, sha512 } from 'sha.js'; | ||
import { getCryptoLib } from './cryptoUtils'; | ||
@@ -56,11 +57,7 @@ export class NodeCryptoSha2Hash { | ||
export function hashSha256Sync(data) { | ||
const hash = new sha256(); | ||
hash.update(data); | ||
return hash.digest(); | ||
return Buffer.from(sha256(data)); | ||
} | ||
export function hashSha512Sync(data) { | ||
const hash = new sha512(); | ||
hash.update(data); | ||
return hash.digest(); | ||
return Buffer.from(sha512(data)); | ||
} | ||
//# sourceMappingURL=sha2Hash.js.map |
/// <reference types="node" /> | ||
export declare function makeECPrivateKey(): string; | ||
export declare function base58CheckDecode(btcAddress: string): { | ||
version: number; | ||
hash: Buffer; | ||
}; | ||
export declare function base58Encode(hash: Buffer): string; | ||
export declare function hashToBase58Check(hash: Buffer): string; | ||
export declare function base58CheckEncode(version: number, hash: Buffer): string; | ||
export declare function publicKeyToAddress(publicKey: string | Buffer): string; | ||
@@ -6,0 +10,0 @@ export declare function getPublicKeyFromPrivate(privateKey: string | Buffer): string; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.compressPrivateKey = exports.isValidPrivateKey = exports.ecPrivateKeyToHexString = exports.ecSign = exports.getPublicKeyFromPrivate = exports.publicKeyToAddress = exports.hashToBase58Check = exports.base58Encode = exports.makeECPrivateKey = void 0; | ||
exports.compressPrivateKey = exports.isValidPrivateKey = exports.ecPrivateKeyToHexString = exports.ecSign = exports.getPublicKeyFromPrivate = exports.publicKeyToAddress = exports.base58CheckEncode = exports.base58Encode = exports.base58CheckDecode = exports.makeECPrivateKey = void 0; | ||
const hmac_1 = require("@noble/hashes/hmac"); | ||
@@ -25,2 +25,20 @@ const sha256_1 = require("@noble/hashes/sha256"); | ||
exports.makeECPrivateKey = makeECPrivateKey; | ||
function base58CheckDecode(btcAddress) { | ||
const buffer = common_1.Buffer.from(bs58_1.default.decode(btcAddress)); | ||
const payload = buffer.slice(0, -4); | ||
const checksum = buffer.slice(-4); | ||
const newChecksum = (0, sha256_1.sha256)((0, sha256_1.sha256)(payload)); | ||
if ((checksum[0] ^ newChecksum[0]) | | ||
(checksum[1] ^ newChecksum[1]) | | ||
(checksum[2] ^ newChecksum[2]) | | ||
(checksum[3] ^ newChecksum[3])) { | ||
throw new Error('Invalid checksum'); | ||
} | ||
if (payload.length !== 21) | ||
throw new TypeError('Invalid address length'); | ||
const version = payload.readUInt8(0); | ||
const hash = payload.slice(1); | ||
return { version, hash }; | ||
} | ||
exports.base58CheckDecode = base58CheckDecode; | ||
function base58Encode(hash) { | ||
@@ -31,10 +49,10 @@ const checksum = (0, sha256_1.sha256)((0, sha256_1.sha256)(hash)); | ||
exports.base58Encode = base58Encode; | ||
function hashToBase58Check(hash) { | ||
return base58Encode(common_1.Buffer.from([BITCOIN_PUBKEYHASH, ...hash].slice(0, 21))); | ||
function base58CheckEncode(version, hash) { | ||
return base58Encode(common_1.Buffer.from([version, ...hash].slice(0, 21))); | ||
} | ||
exports.hashToBase58Check = hashToBase58Check; | ||
exports.base58CheckEncode = base58CheckEncode; | ||
function publicKeyToAddress(publicKey) { | ||
const publicKeyBuffer = common_1.Buffer.isBuffer(publicKey) ? publicKey : common_1.Buffer.from(publicKey, 'hex'); | ||
const publicKeyHash160 = (0, hashRipemd160_1.hashRipemd160)((0, sha2Hash_1.hashSha256Sync)(publicKeyBuffer)); | ||
return hashToBase58Check(publicKeyHash160); | ||
return base58CheckEncode(BITCOIN_PUBKEYHASH, publicKeyHash160); | ||
} | ||
@@ -41,0 +59,0 @@ exports.publicKeyToAddress = publicKeyToAddress; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.decodeMessage = exports.encodeMessage = exports.hashMessage = void 0; | ||
const sha_js_1 = require("sha.js"); | ||
const sha256_1 = require("@noble/hashes/sha256"); | ||
const common_1 = require("@stacks/common"); | ||
const varuint_bitcoin_1 = require("varuint-bitcoin"); | ||
const common_1 = require("@stacks/common"); | ||
const chainPrefix = '\x18Stacks Message Signing:\n'; | ||
function hashMessage(message) { | ||
return new sha_js_1.sha256().update(encodeMessage(message)).digest(); | ||
return common_1.Buffer.from((0, sha256_1.sha256)(encodeMessage(message))); | ||
} | ||
@@ -11,0 +11,0 @@ exports.hashMessage = hashMessage; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hashSha512Sync = exports.hashSha256Sync = exports.createSha2Hash = exports.WebCryptoSha2Hash = exports.NodeCryptoSha2Hash = void 0; | ||
const sha256_1 = require("@noble/hashes/sha256"); | ||
const sha512_1 = require("@noble/hashes/sha512"); | ||
const common_1 = require("@stacks/common"); | ||
const sha_js_1 = require("sha.js"); | ||
const cryptoUtils_1 = require("./cryptoUtils"); | ||
@@ -62,13 +63,9 @@ class NodeCryptoSha2Hash { | ||
function hashSha256Sync(data) { | ||
const hash = new sha_js_1.sha256(); | ||
hash.update(data); | ||
return hash.digest(); | ||
return common_1.Buffer.from((0, sha256_1.sha256)(data)); | ||
} | ||
exports.hashSha256Sync = hashSha256Sync; | ||
function hashSha512Sync(data) { | ||
const hash = new sha_js_1.sha512(); | ||
hash.update(data); | ||
return hash.digest(); | ||
return common_1.Buffer.from((0, sha512_1.sha512)(data)); | ||
} | ||
exports.hashSha512Sync = hashSha512Sync; | ||
//# sourceMappingURL=sha2Hash.js.map |
{ | ||
"name": "@stacks/encryption", | ||
"version": "4.2.0-beta.4", | ||
"version": "4.2.0-beta.5", | ||
"description": "Encryption utilities for Stacks", | ||
@@ -9,6 +9,5 @@ "license": "MIT", | ||
"scripts": { | ||
"build": "npm run clean && npm run build:cjs && npm run build:esm && npm run build:umd && npm run build:polyfill", | ||
"build": "npm run clean && npm run build:cjs && npm run build:esm && npm run build:umd", | ||
"build:cjs": "tsc -b tsconfig.build.json", | ||
"build:esm": "tsc -p tsconfig.build.json --module ES6 --outDir ./dist/esm", | ||
"build:polyfill": "NODE_OPTIONS=--max-old-space-size=8192 rollup -c ../../configs/rollup.config.js && rimraf dist/polyfill/dist", | ||
"build:umd": "NODE_OPTIONS=--max-old-space-size=8192 webpack --config webpack.config.js", | ||
@@ -32,3 +31,2 @@ "clean": "rimraf dist && tsc -b tsconfig.build.json --clean", | ||
"ripemd160-min": "^0.0.6", | ||
"sha.js": "^2.4.11", | ||
"varuint-bitcoin": "^1.1.2" | ||
@@ -66,3 +64,5 @@ }, | ||
"module": "dist/esm/index.js", | ||
"browser": "dist/polyfill/index.js", | ||
"browser": { | ||
"crypto": false | ||
}, | ||
"umd:main": "dist/umd/index.js", | ||
@@ -81,3 +81,3 @@ "unpkg": "dist/umd/index.js", | ||
}, | ||
"gitHead": "e5e9d2a43ea14439d3d76b15df5da07ca5209c0e" | ||
"gitHead": "b05e8a908bf48ff58f49765559d6b36cc87ef38f" | ||
} |
@@ -32,4 +32,34 @@ import { hmac } from '@noble/hashes/hmac'; | ||
/** | ||
* Based on bitcoinjs-lib MIT https://github.com/bitcoinjs/bs58check/blob/12b3e700f355c5c49d0be3f8fc29be6c66e753e9/base.js | ||
* @ignore | ||
*/ | ||
export function base58CheckDecode(btcAddress: string): { | ||
version: number; | ||
hash: Buffer; | ||
} { | ||
const buffer = Buffer.from(base58.decode(btcAddress)); | ||
const payload = buffer.slice(0, -4); | ||
const checksum = buffer.slice(-4); | ||
const newChecksum = sha256(sha256(payload)); | ||
if ( | ||
(checksum[0] ^ newChecksum[0]) | | ||
(checksum[1] ^ newChecksum[1]) | | ||
(checksum[2] ^ newChecksum[2]) | | ||
(checksum[3] ^ newChecksum[3]) | ||
) { | ||
throw new Error('Invalid checksum'); | ||
} | ||
if (payload.length !== 21) throw new TypeError('Invalid address length'); | ||
const version = payload.readUInt8(0); | ||
const hash = payload.slice(1); | ||
return { version, hash }; | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
export function base58Encode(hash: Buffer) { | ||
@@ -43,4 +73,4 @@ const checksum = sha256(sha256(hash)); | ||
*/ | ||
export function hashToBase58Check(hash: Buffer) { | ||
return base58Encode(Buffer.from([BITCOIN_PUBKEYHASH, ...hash].slice(0, 21))); | ||
export function base58CheckEncode(version: number, hash: Buffer) { | ||
return base58Encode(Buffer.from([version, ...hash].slice(0, 21))); | ||
} | ||
@@ -54,3 +84,3 @@ | ||
const publicKeyHash160 = hashRipemd160(hashSha256Sync(publicKeyBuffer)); | ||
return hashToBase58Check(publicKeyHash160); | ||
return base58CheckEncode(BITCOIN_PUBKEYHASH, publicKeyHash160); | ||
} | ||
@@ -57,0 +87,0 @@ |
@@ -1,4 +0,4 @@ | ||
import { sha256 } from 'sha.js'; | ||
import { encode, decode, encodingLength } from 'varuint-bitcoin'; | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { Buffer } from '@stacks/common'; | ||
import { decode, encode, encodingLength } from 'varuint-bitcoin'; | ||
@@ -9,4 +9,4 @@ // 'Stacks Message Signing:\n'.length // = 24 | ||
export function hashMessage(message: string) { | ||
return new sha256().update(encodeMessage(message)).digest(); | ||
export function hashMessage(message: string): Buffer { | ||
return Buffer.from(sha256(encodeMessage(message))); | ||
} | ||
@@ -13,0 +13,0 @@ |
@@ -0,3 +1,4 @@ | ||
import { sha256 } from '@noble/hashes/sha256'; | ||
import { sha512 } from '@noble/hashes/sha512'; | ||
import { Buffer } from '@stacks/common'; | ||
import { sha256, sha512 } from 'sha.js'; | ||
import { getCryptoLib } from './cryptoUtils'; | ||
@@ -71,11 +72,7 @@ | ||
export function hashSha256Sync(data: Buffer) { | ||
const hash = new sha256(); | ||
hash.update(data); | ||
return hash.digest(); | ||
return Buffer.from(sha256(data)); | ||
} | ||
export function hashSha512Sync(data: Buffer) { | ||
const hash = new sha512(); | ||
hash.update(data); | ||
return hash.digest(); | ||
return Buffer.from(sha512(data)); | ||
} |
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 too big to display
Sorry, the diff of this file is not supported yet
Long strings
Supply chain riskContains long string literals, which may be a sign of obfuscated or packed code.
Found 1 instance in 1 package
8
0
807605
104
4348
- Removedsha.js@^2.4.11
- Removedinherits@2.0.4(transitive)
- Removedsha.js@2.4.11(transitive)