PHP WebShell
Текущая директория: /usr/lib/node_modules/bitgo/node_modules/@near-js/crypto/lib/esm
Просмотр файла: key_pair_ed25519.js
import { baseEncode, baseDecode } from "@near-js/utils";
import { ed25519 } from "@noble/curves/ed25519";
import randombytes from "randombytes";
import { KeySize, KeyType } from "./constants.js";
import { KeyPairBase } from "./key_pair_base.js";
import { PublicKey } from "./public_key.js";
class KeyPairEd25519 extends KeyPairBase {
publicKey;
secretKey;
extendedSecretKey;
/**
* Construct an instance of key pair given a secret key.
* It's generally assumed that these are encoded in base58.
* @param extendedSecretKey
*/
constructor(extendedSecretKey) {
super();
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;
}
/**
* Generate a new random keypair.
* @example
* const keyRandom = KeyPair.fromRandom();
* keyRandom.publicKey
* // returns [PUBLIC_KEY]
*
* keyRandom.secretKey
* // returns [SECRET_KEY]
*/
static fromRandom() {
const secretKey = randombytes(KeySize.SECRET_KEY);
const publicKey = ed25519.getPublicKey(new Uint8Array(secretKey));
const extendedSecretKey = new Uint8Array([...secretKey, ...publicKey]);
return new KeyPairEd25519(baseEncode(extendedSecretKey));
}
/**
* Signs a message using the key pair's secret key.
* @param message The message to be signed.
* @returns {Signature} The signature object containing the signature and the public key.
*/
sign(message) {
const signature = ed25519.sign(message, baseDecode(this.secretKey));
return { signature, publicKey: this.publicKey };
}
/**
* Verifies the signature of a message using the key pair's public key.
* @param message The message to be verified.
* @param signature The signature to be verified.
* @returns {boolean} `true` if the signature is valid, otherwise `false`.
*/
verify(message, signature) {
return this.publicKey.verify(message, signature);
}
/**
* Returns a string representation of the key pair in the format 'ed25519:[extendedSecretKey]'.
* @returns {string} The string representation of the key pair.
*/
toString() {
return `ed25519:${this.extendedSecretKey}`;
}
/**
* Retrieves the public key associated with the key pair.
* @returns {PublicKey} The public key.
*/
getPublicKey() {
return this.publicKey;
}
}
export {
KeyPairEd25519
};
Выполнить команду
Для локальной разработки. Не используйте в интернете!