@stacks/encryption
Advanced tools
Comparing version 2.0.0-beta.0 to 2.0.0
@@ -6,2 +6,15 @@ # Change Log | ||
# [2.0.0](https://github.com/blockstack/blockstack.js/compare/v2.0.0-beta.1...v2.0.0) (2021-07-19) | ||
### Bug Fixes | ||
* remove console.log ([1a13af8](https://github.com/blockstack/blockstack.js/commit/1a13af8c0e00851be9ee27a53e67efdf589f5919)) | ||
* remove unused const ([709bd33](https://github.com/blockstack/blockstack.js/commit/709bd33966563cdefa186615ab221dc94efa2f7f)) | ||
* verify that the public key is a secp256k1 point ([cef1d5a](https://github.com/blockstack/blockstack.js/commit/cef1d5ab3bc61a172b65abc1cb5bf0865a34f7d9)) | ||
## [1.2.3](https://github.com/blockstack/blockstack.js/compare/v1.2.2...v1.2.3) (2021-02-25) | ||
@@ -8,0 +21,0 @@ |
@@ -17,2 +17,6 @@ /// <reference types="node" /> | ||
}; | ||
export declare enum InvalidPublicKeyReason { | ||
InvalidFormat = "InvalidFormat", | ||
IsNotPoint = "IsNotPoint" | ||
} | ||
export declare function aes256CbcEncrypt(iv: Buffer, key: Buffer, plaintext: Buffer): Promise<Buffer>; | ||
@@ -19,0 +23,0 @@ export declare function getHexFromBN(bnInput: BN): string; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.verifyECDSA = exports.signECDSA = exports.decryptECIES = exports.encryptECIES = exports.eciesGetJsonStringLength = exports.getSignedCipherObjectWrapper = exports.getCipherObjectWrapper = exports.getBufferFromBN = exports.getHexFromBN = exports.aes256CbcEncrypt = void 0; | ||
exports.verifyECDSA = exports.signECDSA = exports.decryptECIES = exports.encryptECIES = exports.eciesGetJsonStringLength = exports.getSignedCipherObjectWrapper = exports.getCipherObjectWrapper = exports.getBufferFromBN = exports.getHexFromBN = exports.aes256CbcEncrypt = exports.InvalidPublicKeyReason = void 0; | ||
const common_1 = require("@stacks/common"); | ||
@@ -14,2 +14,7 @@ const elliptic_1 = require("elliptic"); | ||
const ecurve = new elliptic_1.ec('secp256k1'); | ||
var InvalidPublicKeyReason; | ||
(function (InvalidPublicKeyReason) { | ||
InvalidPublicKeyReason["InvalidFormat"] = "InvalidFormat"; | ||
InvalidPublicKeyReason["IsNotPoint"] = "IsNotPoint"; | ||
})(InvalidPublicKeyReason = exports.InvalidPublicKeyReason || (exports.InvalidPublicKeyReason = {})); | ||
async function aes256CbcEncrypt(iv, key, plaintext) { | ||
@@ -47,2 +52,39 @@ const cipher = await aesCipher_1.createCipher(); | ||
} | ||
function allHexChars(maybe) { | ||
return maybe.match(/^[0-9a-f]+$/i) !== null; | ||
} | ||
function isValidPublicKey(pub) { | ||
const invalidFormat = { | ||
result: false, | ||
reason_data: 'Invalid public key format', | ||
reason: InvalidPublicKeyReason.InvalidFormat, | ||
}; | ||
const invalidPoint = { | ||
result: false, | ||
reason_data: 'Public key is not a point', | ||
reason: InvalidPublicKeyReason.IsNotPoint, | ||
}; | ||
if (pub.length !== 66 && pub.length !== 130) | ||
return invalidFormat; | ||
const firstByte = pub.slice(0, 2); | ||
if (pub.length === 130 && firstByte !== '04') | ||
return invalidFormat; | ||
if (pub.length === 66 && firstByte !== '02' && firstByte !== '03') | ||
return invalidFormat; | ||
if (!allHexChars(pub)) | ||
return invalidFormat; | ||
const secp256k1 = new elliptic_1.ec('secp256k1'); | ||
try { | ||
const keyPair = secp256k1.keyFromPublic(common_1.Buffer.from(pub, 'hex')); | ||
const result = keyPair.validate(); | ||
return { | ||
result: result.result, | ||
reason_data: result.reason, | ||
reason: result.result ? null : InvalidPublicKeyReason.IsNotPoint, | ||
}; | ||
} | ||
catch (e) { | ||
return invalidPoint; | ||
} | ||
} | ||
function getHexFromBN(bnInput) { | ||
@@ -130,2 +172,6 @@ const hexOut = bnInput.toString('hex', 64); | ||
async function encryptECIES(publicKey, content, wasString, cipherTextEncoding) { | ||
const validity = isValidPublicKey(publicKey); | ||
if (!validity.result) { | ||
throw validity; | ||
} | ||
const ecPK = ecurve.keyFromPublic(publicKey, 'hex').getPublic(); | ||
@@ -132,0 +178,0 @@ const ephemeralSK = ecurve.genKeyPair(); |
@@ -17,2 +17,6 @@ /// <reference types="node" /> | ||
}; | ||
export declare enum InvalidPublicKeyReason { | ||
InvalidFormat = "InvalidFormat", | ||
IsNotPoint = "IsNotPoint" | ||
} | ||
export declare function aes256CbcEncrypt(iv: Buffer, key: Buffer, plaintext: Buffer): Promise<Buffer>; | ||
@@ -19,0 +23,0 @@ export declare function getHexFromBN(bnInput: BN): string; |
@@ -11,2 +11,7 @@ import { Buffer } from '@stacks/common'; | ||
const ecurve = new EllipticCurve('secp256k1'); | ||
export var InvalidPublicKeyReason; | ||
(function (InvalidPublicKeyReason) { | ||
InvalidPublicKeyReason["InvalidFormat"] = "InvalidFormat"; | ||
InvalidPublicKeyReason["IsNotPoint"] = "IsNotPoint"; | ||
})(InvalidPublicKeyReason || (InvalidPublicKeyReason = {})); | ||
export async function aes256CbcEncrypt(iv, key, plaintext) { | ||
@@ -43,2 +48,39 @@ const cipher = await createCipher(); | ||
} | ||
function allHexChars(maybe) { | ||
return maybe.match(/^[0-9a-f]+$/i) !== null; | ||
} | ||
function isValidPublicKey(pub) { | ||
const invalidFormat = { | ||
result: false, | ||
reason_data: 'Invalid public key format', | ||
reason: InvalidPublicKeyReason.InvalidFormat, | ||
}; | ||
const invalidPoint = { | ||
result: false, | ||
reason_data: 'Public key is not a point', | ||
reason: InvalidPublicKeyReason.IsNotPoint, | ||
}; | ||
if (pub.length !== 66 && pub.length !== 130) | ||
return invalidFormat; | ||
const firstByte = pub.slice(0, 2); | ||
if (pub.length === 130 && firstByte !== '04') | ||
return invalidFormat; | ||
if (pub.length === 66 && firstByte !== '02' && firstByte !== '03') | ||
return invalidFormat; | ||
if (!allHexChars(pub)) | ||
return invalidFormat; | ||
const secp256k1 = new EllipticCurve('secp256k1'); | ||
try { | ||
const keyPair = secp256k1.keyFromPublic(Buffer.from(pub, 'hex')); | ||
const result = keyPair.validate(); | ||
return { | ||
result: result.result, | ||
reason_data: result.reason, | ||
reason: result.result ? null : InvalidPublicKeyReason.IsNotPoint, | ||
}; | ||
} | ||
catch (e) { | ||
return invalidPoint; | ||
} | ||
} | ||
export function getHexFromBN(bnInput) { | ||
@@ -121,2 +163,6 @@ const hexOut = bnInput.toString('hex', 64); | ||
export async function encryptECIES(publicKey, content, wasString, cipherTextEncoding) { | ||
const validity = isValidPublicKey(publicKey); | ||
if (!validity.result) { | ||
throw validity; | ||
} | ||
const ecPK = ecurve.keyFromPublic(publicKey, 'hex').getPublic(); | ||
@@ -123,0 +169,0 @@ const ephemeralSK = ecurve.genKeyPair(); |
@@ -26,5 +26,2 @@ import { Buffer } from '@stacks/common'; | ||
: Buffer.from(privateKey, 'hex'); | ||
const realBuffer = require('buffer').Buffer; | ||
const isBuffer1 = realBuffer.isBuffer(privateKeyBuffer); | ||
console.log(isBuffer1); | ||
const keyPair = ECPair.fromPrivateKey(privateKeyBuffer); | ||
@@ -31,0 +28,0 @@ return keyPair.publicKey.toString('hex'); |
@@ -32,5 +32,2 @@ "use strict"; | ||
: common_1.Buffer.from(privateKey, 'hex'); | ||
const realBuffer = require('buffer').Buffer; | ||
const isBuffer1 = realBuffer.isBuffer(privateKeyBuffer); | ||
console.log(isBuffer1); | ||
const keyPair = bitcoinjs_lib_1.ECPair.fromPrivateKey(privateKeyBuffer); | ||
@@ -37,0 +34,0 @@ return keyPair.publicKey.toString('hex'); |
{ | ||
"name": "@stacks/encryption", | ||
"version": "2.0.0-beta.0", | ||
"version": "2.0.0", | ||
"description": "Encryption utilities for Stacks", | ||
@@ -33,3 +33,3 @@ "author": "yknl <yukanliao@gmail.com>", | ||
"dependencies": { | ||
"@stacks/common": "^2.0.0-beta.0", | ||
"@stacks/common": "^2.0.0", | ||
"@types/bn.js": "^4.11.6", | ||
@@ -70,3 +70,3 @@ "@types/node": "^14.14.43", | ||
"unpkg": "dist/index.umd.js", | ||
"gitHead": "6d58c4273399a3644351d0fa822d6b94091679c1" | ||
"gitHead": "f1dbba6c23466cdad67386cac3e60f0d5e36e290" | ||
} |
@@ -51,2 +51,10 @@ import { Buffer } from '@stacks/common'; | ||
*/ | ||
export enum InvalidPublicKeyReason { | ||
InvalidFormat = 'InvalidFormat', | ||
IsNotPoint = 'IsNotPoint', | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
export async function aes256CbcEncrypt( | ||
@@ -106,2 +114,54 @@ iv: Buffer, | ||
/** | ||
* @ignore | ||
*/ | ||
function allHexChars(maybe: string): boolean { | ||
return maybe.match(/^[0-9a-f]+$/i) !== null; | ||
} | ||
/** | ||
* @ignore | ||
*/ | ||
function isValidPublicKey(pub: string): { | ||
result: boolean; | ||
reason: string | null; | ||
reason_data: string | null; | ||
} { | ||
const invalidFormat = { | ||
result: false, | ||
reason_data: 'Invalid public key format', | ||
reason: InvalidPublicKeyReason.InvalidFormat, | ||
}; | ||
const invalidPoint = { | ||
result: false, | ||
reason_data: 'Public key is not a point', | ||
reason: InvalidPublicKeyReason.IsNotPoint, | ||
}; | ||
if (pub.length !== 66 && pub.length !== 130) return invalidFormat; | ||
const firstByte = pub.slice(0, 2); | ||
// uncompressed public key | ||
if (pub.length === 130 && firstByte !== '04') return invalidFormat; | ||
// compressed public key | ||
if (pub.length === 66 && firstByte !== '02' && firstByte !== '03') return invalidFormat; | ||
if (!allHexChars(pub)) return invalidFormat; | ||
// validate the public key | ||
const secp256k1 = new EllipticCurve('secp256k1'); | ||
try { | ||
const keyPair = secp256k1.keyFromPublic(Buffer.from(pub, 'hex')); | ||
const result = keyPair.validate(); | ||
return { | ||
result: result.result, | ||
reason_data: result.reason, | ||
reason: result.result ? null : InvalidPublicKeyReason.IsNotPoint, | ||
}; | ||
} catch (e) { | ||
return invalidPoint; | ||
} | ||
} | ||
/** | ||
* Hex encodes a 32-byte BN.js instance. | ||
@@ -180,5 +240,3 @@ * The result string is zero padded and always 64 characters in length. | ||
*/ | ||
export function getSignedCipherObjectWrapper( | ||
payloadShell: string | ||
): { | ||
export function getSignedCipherObjectWrapper(payloadShell: string): { | ||
/** The stringified JSON string of an empty `SignedCipherObject`. */ | ||
@@ -238,5 +296,4 @@ signedPayloadValuesLength: number; | ||
// Get the signed version of the JSON envelope | ||
const { signedPayloadShell, signedPayloadValuesLength } = getSignedCipherObjectWrapper( | ||
payloadShell | ||
); | ||
const { signedPayloadShell, signedPayloadValuesLength } = | ||
getSignedCipherObjectWrapper(payloadShell); | ||
// Add length of the JSON envelope, ciphertext length, and length of the const values. | ||
@@ -271,2 +328,6 @@ return ( | ||
): Promise<CipherObject> { | ||
const validity = isValidPublicKey(publicKey); | ||
if (!validity.result) { | ||
throw validity; | ||
} | ||
const ecPK = ecurve.keyFromPublic(publicKey, 'hex').getPublic(); | ||
@@ -273,0 +334,0 @@ const ephemeralSK = ecurve.genKeyPair(); |
@@ -45,6 +45,2 @@ import { Buffer } from '@stacks/common'; | ||
: Buffer.from(privateKey, 'hex'); | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const realBuffer = require('buffer').Buffer; | ||
const isBuffer1 = realBuffer.isBuffer(privateKeyBuffer); | ||
console.log(isBuffer1); | ||
const keyPair = ECPair.fromPrivateKey(privateKeyBuffer); | ||
@@ -51,0 +47,0 @@ return keyPair.publicKey.toString('hex'); |
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
202497
3671
0
1
Updated@stacks/common@^2.0.0