Socket
Socket
Sign inDemoInstall

@near-js/crypto

Package Overview
Dependencies
Maintainers
2
Versions
30
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@near-js/crypto - npm Package Compare versions

Comparing version 1.2.4 to 1.3.0-next.0

lib/key_pair_secp256k1.d.ts

11

lib/constants.d.ts
/** All supported key types */
export declare enum KeyType {
ED25519 = 0
ED25519 = 0,
SECP256K1 = 1
}
export declare enum KeySize {
SECRET_KEY = 32
}
export declare const KeySize: {
SECRET_KEY: number;
ED25519_PUBLIC_KEY: number;
SECP256k1_PUBLIC_KEY: number;
};
//# sourceMappingURL=constants.d.ts.map

@@ -1,12 +0,11 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeySize = exports.KeyType = void 0;
/** All supported key types */
var KeyType;
export var KeyType;
(function (KeyType) {
KeyType[KeyType["ED25519"] = 0] = "ED25519";
})(KeyType = exports.KeyType || (exports.KeyType = {}));
var KeySize;
(function (KeySize) {
KeySize[KeySize["SECRET_KEY"] = 32] = "SECRET_KEY";
})(KeySize = exports.KeySize || (exports.KeySize = {}));
KeyType[KeyType["SECP256K1"] = 1] = "SECP256K1";
})(KeyType || (KeyType = {}));
export const KeySize = {
SECRET_KEY: 32,
ED25519_PUBLIC_KEY: 32,
SECP256k1_PUBLIC_KEY: 64,
};
export { KeyType } from './constants';
export { KeyPair } from './key_pair';
export { KeyPair, KeyPairString } from './key_pair';
export { Signature } from './key_pair_base';
export { KeyPairEd25519 } from './key_pair_ed25519';
export { KeyPairSecp256k1 } from './key_pair_secp256k1';
export { PublicKey } from './public_key';
//# sourceMappingURL=index.d.ts.map

@@ -1,11 +0,5 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublicKey = exports.KeyPairEd25519 = exports.KeyPair = exports.KeyType = void 0;
var constants_1 = require("./constants");
Object.defineProperty(exports, "KeyType", { enumerable: true, get: function () { return constants_1.KeyType; } });
var key_pair_1 = require("./key_pair");
Object.defineProperty(exports, "KeyPair", { enumerable: true, get: function () { return key_pair_1.KeyPair; } });
var key_pair_ed25519_1 = require("./key_pair_ed25519");
Object.defineProperty(exports, "KeyPairEd25519", { enumerable: true, get: function () { return key_pair_ed25519_1.KeyPairEd25519; } });
var public_key_1 = require("./public_key");
Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return public_key_1.PublicKey; } });
export { KeyType } from './constants';
export { KeyPair } from './key_pair';
export { KeyPairEd25519 } from './key_pair_ed25519';
export { KeyPairSecp256k1 } from './key_pair_secp256k1';
export { PublicKey } from './public_key';

@@ -0,1 +1,2 @@

import { KeyPairString } from './key_pair';
import { PublicKey } from './public_key';

@@ -9,5 +10,5 @@ export interface Signature {

abstract verify(message: Uint8Array, signature: Uint8Array): boolean;
abstract toString(): string;
abstract toString(): KeyPairString;
abstract getPublicKey(): PublicKey;
}
//# sourceMappingURL=key_pair_base.d.ts.map

@@ -1,6 +0,2 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyPairBase = void 0;
class KeyPairBase {
export class KeyPairBase {
}
exports.KeyPairBase = KeyPairBase;
import { KeyPairBase, Signature } from './key_pair_base';
import { PublicKey } from './public_key';
import { KeyPairString } from './key_pair';
/**

@@ -45,3 +46,3 @@ * This class provides key pair functionality for Ed25519 curve:

*/
toString(): string;
toString(): KeyPairString;
/**

@@ -48,0 +49,0 @@ * Retrieves the public key associated with the key pair.

@@ -1,13 +0,7 @@

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyPairEd25519 = void 0;
const utils_1 = require("@near-js/utils");
const ed25519_1 = require("@noble/curves/ed25519");
const randombytes_1 = __importDefault(require("randombytes"));
const constants_1 = require("./constants");
const key_pair_base_1 = require("./key_pair_base");
const public_key_1 = require("./public_key");
import { baseEncode, baseDecode } from '@near-js/utils';
import { ed25519 } from '@noble/curves/ed25519';
import randombytes from 'randombytes';
import { KeySize, KeyType } from './constants';
import { KeyPairBase } from './key_pair_base';
import { PublicKey } from './public_key';
/**

@@ -17,3 +11,6 @@ * This class provides key pair functionality for Ed25519 curve:

*/
class KeyPairEd25519 extends key_pair_base_1.KeyPairBase {
export class KeyPairEd25519 extends KeyPairBase {
publicKey;
secretKey;
extendedSecretKey;
/**

@@ -26,7 +23,7 @@ * Construct an instance of key pair given a secret key.

super();
const decoded = (0, utils_1.baseDecode)(extendedSecretKey);
const secretKey = new Uint8Array(decoded.slice(0, constants_1.KeySize.SECRET_KEY));
const publicKey = ed25519_1.ed25519.getPublicKey(new Uint8Array(secretKey));
this.publicKey = new public_key_1.PublicKey({ keyType: constants_1.KeyType.ED25519, data: publicKey });
this.secretKey = (0, utils_1.baseEncode)(secretKey);
const decoded = baseDecode(extendedSecretKey);
const secretKey = new Uint8Array(decoded.slice(0, KeySize.SECRET_KEY));
const publicKey = ed25519.getPublicKey(new Uint8Array(secretKey));
this.publicKey = new PublicKey({ keyType: KeyType.ED25519, data: publicKey });
this.secretKey = baseEncode(secretKey);
this.extendedSecretKey = extendedSecretKey;

@@ -45,6 +42,6 @@ }

static fromRandom() {
const secretKey = (0, randombytes_1.default)(constants_1.KeySize.SECRET_KEY);
const publicKey = ed25519_1.ed25519.getPublicKey(new Uint8Array(secretKey));
const secretKey = randombytes(KeySize.SECRET_KEY);
const publicKey = ed25519.getPublicKey(new Uint8Array(secretKey));
const extendedSecretKey = new Uint8Array([...secretKey, ...publicKey]);
return new KeyPairEd25519((0, utils_1.baseEncode)(extendedSecretKey));
return new KeyPairEd25519(baseEncode(extendedSecretKey));
}

@@ -57,3 +54,3 @@ /**

sign(message) {
const signature = ed25519_1.ed25519.sign(message, (0, utils_1.baseDecode)(this.secretKey));
const signature = ed25519.sign(message, baseDecode(this.secretKey));
return { signature, publicKey: this.publicKey };

@@ -85,2 +82,1 @@ }

}
exports.KeyPairEd25519 = KeyPairEd25519;
import { KeyPairBase } from './key_pair_base';
export type KeyPairString = `ed25519:${string}` | `secp256k1:${string}`;
export declare abstract class KeyPair extends KeyPairBase {

@@ -7,3 +8,3 @@ /**

*/
static fromRandom(curve: string): KeyPair;
static fromRandom(curve: 'ed25519' | 'secp256k1'): KeyPair;
/**

@@ -14,4 +15,4 @@ * Creates a key pair from an encoded key string.

*/
static fromString(encodedKey: string): KeyPair;
static fromString(encodedKey: KeyPairString): KeyPair;
}
//# sourceMappingURL=key_pair.d.ts.map

@@ -1,7 +0,5 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.KeyPair = void 0;
const key_pair_base_1 = require("./key_pair_base");
const key_pair_ed25519_1 = require("./key_pair_ed25519");
class KeyPair extends key_pair_base_1.KeyPairBase {
import { KeyPairBase } from './key_pair_base';
import { KeyPairEd25519 } from './key_pair_ed25519';
import { KeyPairSecp256k1 } from './key_pair_secp256k1';
export class KeyPair extends KeyPairBase {
/**

@@ -13,3 +11,4 @@ * @param curve Name of elliptical curve, case-insensitive

switch (curve.toUpperCase()) {
case 'ED25519': return key_pair_ed25519_1.KeyPairEd25519.fromRandom();
case 'ED25519': return KeyPairEd25519.fromRandom();
case 'SECP256K1': return KeyPairSecp256k1.fromRandom();
default: throw new Error(`Unknown curve ${curve}`);

@@ -25,8 +24,6 @@ }

const parts = encodedKey.split(':');
if (parts.length === 1) {
return new key_pair_ed25519_1.KeyPairEd25519(parts[0]);
}
else if (parts.length === 2) {
if (parts.length === 2) {
switch (parts[0].toUpperCase()) {
case 'ED25519': return new key_pair_ed25519_1.KeyPairEd25519(parts[1]);
case 'ED25519': return new KeyPairEd25519(parts[1]);
case 'SECP256K1': return new KeyPairSecp256k1(parts[1]);
default: throw new Error(`Unknown curve: ${parts[0]}`);

@@ -40,2 +37,1 @@ }

}
exports.KeyPair = KeyPair;

@@ -1,9 +0,22 @@

import { Assignable } from '@near-js/types';
import { Enum } from '@near-js/types';
import { KeyType } from './constants';
declare class ED25519PublicKey {
keyType: KeyType;
data: Uint8Array;
}
declare class SECP256K1PublicKey {
keyType: KeyType;
data: Uint8Array;
}
/**
* PublicKey representation that has type and bytes of the key.
*/
export declare class PublicKey extends Assignable {
keyType: KeyType;
data: Uint8Array;
export declare class PublicKey extends Enum {
enum: string;
ed25519Key?: ED25519PublicKey;
secp256k1Key?: SECP256K1PublicKey;
constructor(publicKey: {
keyType: KeyType;
data: Uint8Array;
});
/**

@@ -33,3 +46,7 @@ * Creates a PublicKey instance from a string or an existing PublicKey instance.

verify(message: Uint8Array, signature: Uint8Array): boolean;
get keyPair(): ED25519PublicKey | SECP256K1PublicKey;
get keyType(): KeyType;
get data(): Uint8Array;
}
export {};
//# sourceMappingURL=public_key.d.ts.map

@@ -1,11 +0,10 @@

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.PublicKey = void 0;
const types_1 = require("@near-js/types");
const utils_1 = require("@near-js/utils");
const ed25519_1 = require("@noble/curves/ed25519");
const constants_1 = require("./constants");
import { baseEncode, baseDecode } from '@near-js/utils';
import { Enum } from '@near-js/types';
import { ed25519 } from '@noble/curves/ed25519';
import secp256k1 from 'secp256k1';
import { KeySize, KeyType } from './constants';
function key_type_to_str(keyType) {
switch (keyType) {
case constants_1.KeyType.ED25519: return 'ed25519';
case KeyType.ED25519: return 'ed25519';
case KeyType.SECP256K1: return 'secp256k1';
default: throw new Error(`Unknown key type ${keyType}`);

@@ -16,10 +15,41 @@ }

switch (keyType.toLowerCase()) {
case 'ed25519': return constants_1.KeyType.ED25519;
case 'ed25519': return KeyType.ED25519;
case 'secp256k1': return KeyType.SECP256K1;
default: throw new Error(`Unknown key type ${keyType}`);
}
}
class ED25519PublicKey {
keyType = KeyType.ED25519;
data;
}
class SECP256K1PublicKey {
keyType = KeyType.SECP256K1;
data;
}
function resolveEnumKeyName(keyType) {
switch (keyType) {
case KeyType.ED25519: {
return 'ed25519Key';
}
case KeyType.SECP256K1: {
return 'secp256k1Key';
}
default: {
throw Error(`unknown type ${keyType}`);
}
}
}
/**
* PublicKey representation that has type and bytes of the key.
*/
class PublicKey extends types_1.Assignable {
export class PublicKey extends Enum {
enum;
ed25519Key;
secp256k1Key;
constructor(publicKey) {
const keyName = resolveEnumKeyName(publicKey.keyType);
super({ [keyName]: publicKey });
this[keyName] = publicKey;
this.enum = keyName;
}
/**

@@ -44,3 +74,3 @@ * Creates a PublicKey instance from a string or an existing PublicKey instance.

let publicKey;
let keyType = constants_1.KeyType.ED25519;
let keyType;
if (parts.length === 1) {

@@ -56,6 +86,10 @@ publicKey = parts[0];

}
const decodedPublicKey = (0, utils_1.baseDecode)(publicKey);
if (decodedPublicKey.length !== constants_1.KeySize.SECRET_KEY) {
throw new Error(`Invalid public key size (${decodedPublicKey.length}), must be ${constants_1.KeySize.SECRET_KEY}`);
const decodedPublicKey = baseDecode(publicKey);
if (!keyType) {
keyType = decodedPublicKey.length === KeySize.SECP256k1_PUBLIC_KEY ? KeyType.SECP256K1 : KeyType.ED25519;
}
const keySize = keyType === KeyType.ED25519 ? KeySize.ED25519_PUBLIC_KEY : KeySize.SECP256k1_PUBLIC_KEY;
if (decodedPublicKey.length !== keySize) {
throw new Error(`Invalid public key size (${decodedPublicKey.length}), must be ${keySize}`);
}
return new PublicKey({ keyType, data: decodedPublicKey });

@@ -68,3 +102,4 @@ }

toString() {
return `${key_type_to_str(this.keyType)}:${(0, utils_1.baseEncode)(this.data)}`;
const encodedKey = baseEncode(this.data);
return `${key_type_to_str(this.keyType)}:${encodedKey}`;
}

@@ -78,8 +113,22 @@ /**

verify(message, signature) {
switch (this.keyType) {
case constants_1.KeyType.ED25519: return ed25519_1.ed25519.verify(signature, message, this.data);
default: throw new Error(`Unknown key type ${this.keyType}`);
const keyType = this.keyType;
const data = this.data;
switch (keyType) {
case KeyType.ED25519:
return ed25519.verify(signature, message, data);
case KeyType.SECP256K1:
return secp256k1.ecdsaVerify(signature.subarray(0, 64), message, new Uint8Array([0x04, ...data]));
default:
throw new Error(`Unknown key type: ${keyType}`);
}
}
get keyPair() {
return this.ed25519Key || this.secp256k1Key;
}
get keyType() {
return this.keyPair.keyType;
}
get data() {
return this.keyPair.data;
}
}
exports.PublicKey = PublicKey;
{
"name": "@near-js/crypto",
"version": "1.2.4",
"version": "1.3.0-next.0",
"description": "Abstractions around NEAR-compatible elliptical curves and cryptographic keys",
"main": "lib/index.js",
"type": "module",
"keywords": [],

@@ -10,13 +11,17 @@ "author": "",

"dependencies": {
"@noble/curves": "1.2.0",
"borsh": "1.0.0",
"@noble/curves": "1.2.0",
"randombytes": "2.1.0",
"@near-js/types": "0.2.1",
"@near-js/utils": "0.2.2"
"secp256k1": "5.0.0",
"@near-js/types": "0.3.0-next.0",
"@near-js/utils": "0.3.0-next.0"
},
"devDependencies": {
"@types/node": "18.11.18",
"jest": "26.0.1",
"ts-jest": "26.5.6",
"typescript": "4.9.4"
"@jest/globals": "^29.7.0",
"@noble/hashes": "^1.4.0",
"@types/node": "20.0.0",
"jest": "29.7.0",
"ts-jest": "29.1.5",
"typescript": "5.4.5",
"tsconfig": "0.0.0"
},

@@ -29,8 +34,6 @@ "files": [

"compile": "tsc -p tsconfig.json",
"lint:js": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc",
"lint:js:fix": "eslint -c ../../.eslintrc.js.yml test/**/*.js --no-eslintrc --fix",
"lint:ts": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc",
"lint:ts:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts --no-eslintrc --fix",
"test": "jest test"
"lint": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc",
"lint:fix": "eslint -c ../../.eslintrc.ts.yml src/**/*.ts test/**/*.ts --no-eslintrc --fix",
"test": "jest"
}
}

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc