@mysten/sui
Version:
Sui TypeScript API(Work in Progress)
145 lines (144 loc) • 5.43 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var keypair_exports = {};
__export(keypair_exports, {
DEFAULT_SECP256R1_DERIVATION_PATH: () => DEFAULT_SECP256R1_DERIVATION_PATH,
Secp256r1Keypair: () => Secp256r1Keypair
});
module.exports = __toCommonJS(keypair_exports);
var import_p256 = require("@noble/curves/p256");
var import_blake2b = require("@noble/hashes/blake2b");
var import_sha256 = require("@noble/hashes/sha256");
var import_utils = require("@noble/hashes/utils");
var import_bip32 = require("@scure/bip32");
var import_keypair = require("../../cryptography/keypair.js");
var import_mnemonics = require("../../cryptography/mnemonics.js");
var import_publickey = require("./publickey.js");
const DEFAULT_SECP256R1_DERIVATION_PATH = "m/74'/784'/0'/0/0";
class Secp256r1Keypair extends import_keypair.Keypair {
/**
* Create a new keypair instance.
* Generate random keypair if no {@link Secp256r1Keypair} is provided.
*
* @param keypair Secp256r1 keypair
*/
constructor(keypair) {
super();
if (keypair) {
this.keypair = keypair;
} else {
const secretKey = import_p256.secp256r1.utils.randomPrivateKey();
const publicKey = import_p256.secp256r1.getPublicKey(secretKey, true);
this.keypair = { publicKey, secretKey };
}
}
/**
* Get the key scheme of the keypair Secp256r1
*/
getKeyScheme() {
return "Secp256r1";
}
/**
* Generate a new random keypair
*/
static generate() {
return new Secp256r1Keypair();
}
/**
* Create a keypair from a raw secret key byte array.
*
* This method should only be used to recreate a keypair from a previously
* generated secret key. Generating keypairs from a random seed should be done
* with the {@link Keypair.fromSeed} method.
*
* @throws error if the provided secret key is invalid and validation is not skipped.
*
* @param secretKey secret key byte array or Bech32 secret key string
* @param options: skip secret key validation
*/
static fromSecretKey(secretKey, options) {
if (typeof secretKey === "string") {
const decoded = (0, import_keypair.decodeSuiPrivateKey)(secretKey);
if (decoded.schema !== "Secp256r1") {
throw new Error(`Expected a Secp256r1 keypair, got ${decoded.schema}`);
}
return this.fromSecretKey(decoded.secretKey, options);
}
const publicKey = import_p256.secp256r1.getPublicKey(secretKey, true);
if (!options || !options.skipValidation) {
const encoder = new TextEncoder();
const signData = encoder.encode("sui validation");
const msgHash = (0, import_utils.bytesToHex)((0, import_blake2b.blake2b)(signData, { dkLen: 32 }));
const signature = import_p256.secp256r1.sign(msgHash, secretKey, { lowS: true });
if (!import_p256.secp256r1.verify(signature, msgHash, publicKey, { lowS: true })) {
throw new Error("Provided secretKey is invalid");
}
}
return new Secp256r1Keypair({ publicKey, secretKey });
}
/**
* Generate a keypair from a 32 byte seed.
*
* @param seed seed byte array
*/
static fromSeed(seed) {
const publicKey = import_p256.secp256r1.getPublicKey(seed, true);
return new Secp256r1Keypair({ publicKey, secretKey: seed });
}
/**
* The public key for this keypair
*/
getPublicKey() {
return new import_publickey.Secp256r1PublicKey(this.keypair.publicKey);
}
/**
* The Bech32 secret key string for this Secp256r1 keypair
*/
getSecretKey() {
return (0, import_keypair.encodeSuiPrivateKey)(this.keypair.secretKey, this.getKeyScheme());
}
/**
* Return the signature for the provided data.
*/
async sign(data) {
const msgHash = (0, import_sha256.sha256)(data);
const sig = import_p256.secp256r1.sign(msgHash, this.keypair.secretKey, {
lowS: true
});
return sig.toCompactRawBytes();
}
/**
* Derive Secp256r1 keypair from mnemonics and path. The mnemonics must be normalized
* and validated against the english wordlist.
*
* If path is none, it will default to m/74'/784'/0'/0/0, otherwise the path must
* be compliant to BIP-32 in form m/74'/784'/{account_index}'/{change_index}/{address_index}.
*/
static deriveKeypair(mnemonics, path) {
if (path == null) {
path = DEFAULT_SECP256R1_DERIVATION_PATH;
}
if (!(0, import_mnemonics.isValidBIP32Path)(path)) {
throw new Error("Invalid derivation path");
}
const privateKey = import_bip32.HDKey.fromMasterSeed((0, import_mnemonics.mnemonicToSeed)(mnemonics)).derive(path).privateKey;
return Secp256r1Keypair.fromSecretKey(privateKey);
}
}
//# sourceMappingURL=keypair.js.map
;