@dfinity/identity
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -21,4 +21,12 @@ import { DerEncodedPublicKey, KeyPair, PublicKey, Signature, SignIdentity } from '@dfinity/agent'; | ||
} | ||
/** | ||
* Ed25519KeyIdentity is an implementation of SignIdentity that uses Ed25519 keys. This class is used to sign and verify messages for an agent. | ||
*/ | ||
export declare class Ed25519KeyIdentity extends SignIdentity { | ||
#private; | ||
/** | ||
* Generate a new Ed25519KeyIdentity. | ||
* @param seed a 32-byte seed for the private key. If not provided, a random seed will be generated. | ||
* @returns Ed25519KeyIdentity | ||
*/ | ||
static generate(seed?: Uint8Array): Ed25519KeyIdentity; | ||
@@ -25,0 +33,0 @@ static fromParsedJson(obj: JsonnableEd25519KeyIdentity): Ed25519KeyIdentity; |
@@ -17,2 +17,3 @@ "use strict"; | ||
const agent_1 = require("@dfinity/agent"); | ||
const agent_2 = require("@dfinity/agent"); | ||
const ed25519_1 = require("@noble/curves/ed25519"); | ||
@@ -40,3 +41,3 @@ function isObject(value) { | ||
if (typeof maybeKey === 'string') { | ||
const key = (0, agent_1.fromHex)(maybeKey); | ||
const key = (0, agent_2.fromHex)(maybeKey); | ||
return this.fromRaw(key); | ||
@@ -51,3 +52,3 @@ } | ||
const view = key; | ||
return this.fromRaw((0, agent_1.bufFromBufLike)(view.buffer)); | ||
return this.fromRaw((0, agent_2.bufFromBufLike)(view.buffer)); | ||
} | ||
@@ -76,3 +77,3 @@ else if (key instanceof ArrayBuffer) { | ||
static derEncode(publicKey) { | ||
const key = (0, agent_1.wrapDER)(publicKey, agent_1.ED25519_OID).buffer; | ||
const key = (0, agent_2.wrapDER)(publicKey, agent_2.ED25519_OID).buffer; | ||
key.__derEncodedPublicKey__ = undefined; | ||
@@ -82,3 +83,3 @@ return key; | ||
static derDecode(key) { | ||
const unwrapped = (0, agent_1.unwrapDER)(key, agent_1.ED25519_OID); | ||
const unwrapped = (0, agent_2.unwrapDER)(key, agent_2.ED25519_OID); | ||
if (unwrapped.length !== this.RAW_KEY_LENGTH) { | ||
@@ -106,3 +107,6 @@ throw new Error('An Ed25519 public key must be exactly 32bytes long'); | ||
Ed25519PublicKey.RAW_KEY_LENGTH = 32; | ||
class Ed25519KeyIdentity extends agent_1.SignIdentity { | ||
/** | ||
* Ed25519KeyIdentity is an implementation of SignIdentity that uses Ed25519 keys. This class is used to sign and verify messages for an agent. | ||
*/ | ||
class Ed25519KeyIdentity extends agent_2.SignIdentity { | ||
// `fromRaw` and `fromDer` should be used for instantiation, not this constructor. | ||
@@ -116,3 +120,8 @@ constructor(publicKey, privateKey) { | ||
} | ||
static generate(seed = new Uint8Array(32)) { | ||
/** | ||
* Generate a new Ed25519KeyIdentity. | ||
* @param seed a 32-byte seed for the private key. If not provided, a random seed will be generated. | ||
* @returns Ed25519KeyIdentity | ||
*/ | ||
static generate(seed) { | ||
if (seed && seed.length !== 32) { | ||
@@ -123,2 +132,6 @@ throw new Error('Ed25519 Seed needs to be 32 bytes long.'); | ||
seed = ed25519_1.ed25519.utils.randomPrivateKey(); | ||
// Check if the seed is all zeros | ||
if ((0, agent_1.bufEquals)(seed, new Uint8Array(new Array(32).fill(0)))) { | ||
console.warn('Seed is all zeros. This is not a secure seed. Please provide a seed with sufficient entropy if this is a production environment.'); | ||
} | ||
const sk = new Uint8Array(32); | ||
@@ -132,3 +145,3 @@ for (let i = 0; i < 32; i++) | ||
const [publicKeyDer, privateKeyRaw] = obj; | ||
return new Ed25519KeyIdentity(Ed25519PublicKey.fromDer((0, agent_1.fromHex)(publicKeyDer)), (0, agent_1.fromHex)(privateKeyRaw)); | ||
return new Ed25519KeyIdentity(Ed25519PublicKey.fromDer((0, agent_2.fromHex)(publicKeyDer)), (0, agent_2.fromHex)(privateKeyRaw)); | ||
} | ||
@@ -158,3 +171,3 @@ static fromJSON(json) { | ||
toJSON() { | ||
return [(0, agent_1.toHex)(__classPrivateFieldGet(this, _Ed25519KeyIdentity_publicKey, "f").toDer()), (0, agent_1.toHex)(__classPrivateFieldGet(this, _Ed25519KeyIdentity_privateKey, "f"))]; | ||
return [(0, agent_2.toHex)(__classPrivateFieldGet(this, _Ed25519KeyIdentity_publicKey, "f").toDer()), (0, agent_2.toHex)(__classPrivateFieldGet(this, _Ed25519KeyIdentity_privateKey, "f"))]; | ||
} | ||
@@ -183,3 +196,3 @@ /** | ||
// Some implementations of Ed25519 private keys append a public key to the end of the private key. We only want the private key. | ||
const signature = (0, agent_1.uint8ToBuf)(ed25519_1.ed25519.sign(blob, __classPrivateFieldGet(this, _Ed25519KeyIdentity_privateKey, "f").slice(0, 32))); | ||
const signature = (0, agent_2.uint8ToBuf)(ed25519_1.ed25519.sign(blob, __classPrivateFieldGet(this, _Ed25519KeyIdentity_privateKey, "f").slice(0, 32))); | ||
// add { __signature__: void; } to the signature to make it compatible with the agent | ||
@@ -202,3 +215,3 @@ Object.defineProperty(signature, '__signature__', { | ||
if (typeof x === 'string') { | ||
x = (0, agent_1.fromHex)(x); | ||
x = (0, agent_2.fromHex)(x); | ||
} | ||
@@ -205,0 +218,0 @@ if (x instanceof Uint8Array) { |
@@ -21,4 +21,12 @@ import { DerEncodedPublicKey, KeyPair, PublicKey, Signature, SignIdentity } from '@dfinity/agent'; | ||
} | ||
/** | ||
* Ed25519KeyIdentity is an implementation of SignIdentity that uses Ed25519 keys. This class is used to sign and verify messages for an agent. | ||
*/ | ||
export declare class Ed25519KeyIdentity extends SignIdentity { | ||
#private; | ||
/** | ||
* Generate a new Ed25519KeyIdentity. | ||
* @param seed a 32-byte seed for the private key. If not provided, a random seed will be generated. | ||
* @returns Ed25519KeyIdentity | ||
*/ | ||
static generate(seed?: Uint8Array): Ed25519KeyIdentity; | ||
@@ -25,0 +33,0 @@ static fromParsedJson(obj: JsonnableEd25519KeyIdentity): Ed25519KeyIdentity; |
@@ -13,2 +13,3 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
var _Ed25519PublicKey_rawKey, _Ed25519PublicKey_derKey, _Ed25519KeyIdentity_publicKey, _Ed25519KeyIdentity_privateKey; | ||
import { bufEquals } from '@dfinity/agent'; | ||
import { SignIdentity, uint8ToBuf, ED25519_OID, unwrapDER, wrapDER, fromHex, toHex, bufFromBufLike, } from '@dfinity/agent'; | ||
@@ -98,2 +99,5 @@ import { ed25519 } from '@noble/curves/ed25519'; | ||
Ed25519PublicKey.RAW_KEY_LENGTH = 32; | ||
/** | ||
* Ed25519KeyIdentity is an implementation of SignIdentity that uses Ed25519 keys. This class is used to sign and verify messages for an agent. | ||
*/ | ||
export class Ed25519KeyIdentity extends SignIdentity { | ||
@@ -108,3 +112,8 @@ // `fromRaw` and `fromDer` should be used for instantiation, not this constructor. | ||
} | ||
static generate(seed = new Uint8Array(32)) { | ||
/** | ||
* Generate a new Ed25519KeyIdentity. | ||
* @param seed a 32-byte seed for the private key. If not provided, a random seed will be generated. | ||
* @returns Ed25519KeyIdentity | ||
*/ | ||
static generate(seed) { | ||
if (seed && seed.length !== 32) { | ||
@@ -115,2 +124,6 @@ throw new Error('Ed25519 Seed needs to be 32 bytes long.'); | ||
seed = ed25519.utils.randomPrivateKey(); | ||
// Check if the seed is all zeros | ||
if (bufEquals(seed, new Uint8Array(new Array(32).fill(0)))) { | ||
console.warn('Seed is all zeros. This is not a secure seed. Please provide a seed with sufficient entropy if this is a production environment.'); | ||
} | ||
const sk = new Uint8Array(32); | ||
@@ -117,0 +130,0 @@ for (let i = 0; i < 32; i++) |
{ | ||
"name": "@dfinity/identity", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"author": "DFINITY Stiftung <sdk@dfinity.org>", | ||
@@ -48,4 +48,4 @@ "license": "Apache-2.0", | ||
"peerDependencies": { | ||
"@dfinity/agent": "^1.0.0", | ||
"@dfinity/principal": "^1.0.0", | ||
"@dfinity/agent": "^1.0.1", | ||
"@dfinity/principal": "^1.0.1", | ||
"@peculiar/webcrypto": "^1.4.0" | ||
@@ -52,0 +52,0 @@ }, |
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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
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
329260
2602
0