@mysten/sui
Version:
Sui TypeScript API(Work in Progress)
95 lines (94 loc) • 3.3 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 publickey_exports = {};
__export(publickey_exports, {
Secp256r1PublicKey: () => Secp256r1PublicKey
});
module.exports = __toCommonJS(publickey_exports);
var import_bcs = require("@mysten/bcs");
var import_p256 = require("@noble/curves/p256");
var import_sha256 = require("@noble/hashes/sha256");
var import_publickey = require("../../cryptography/publickey.js");
var import_signature_scheme = require("../../cryptography/signature-scheme.js");
var import_signature = require("../../cryptography/signature.js");
const SECP256R1_PUBLIC_KEY_SIZE = 33;
class Secp256r1PublicKey extends import_publickey.PublicKey {
/**
* Create a new Secp256r1PublicKey object
* @param value secp256r1 public key as buffer or base-64 encoded string
*/
constructor(value) {
super();
if (typeof value === "string") {
this.data = (0, import_bcs.fromBase64)(value);
} else if (value instanceof Uint8Array) {
this.data = value;
} else {
this.data = Uint8Array.from(value);
}
if (this.data.length !== SECP256R1_PUBLIC_KEY_SIZE) {
throw new Error(
`Invalid public key input. Expected ${SECP256R1_PUBLIC_KEY_SIZE} bytes, got ${this.data.length}`
);
}
}
/**
* Checks if two Secp256r1 public keys are equal
*/
equals(publicKey) {
return super.equals(publicKey);
}
/**
* Return the byte array representation of the Secp256r1 public key
*/
toRawBytes() {
return this.data;
}
/**
* Return the Sui address associated with this Secp256r1 public key
*/
flag() {
return import_signature_scheme.SIGNATURE_SCHEME_TO_FLAG["Secp256r1"];
}
/**
* Verifies that the signature is valid for for the provided message
*/
async verify(message, signature) {
let bytes;
if (typeof signature === "string") {
const parsed = (0, import_signature.parseSerializedSignature)(signature);
if (parsed.signatureScheme !== "Secp256r1") {
throw new Error("Invalid signature scheme");
}
if (!(0, import_publickey.bytesEqual)(this.toRawBytes(), parsed.publicKey)) {
throw new Error("Signature does not match public key");
}
bytes = parsed.signature;
} else {
bytes = signature;
}
return import_p256.secp256r1.verify(
import_p256.secp256r1.Signature.fromCompact(bytes),
(0, import_sha256.sha256)(message),
this.toRawBytes()
);
}
}
Secp256r1PublicKey.SIZE = SECP256R1_PUBLIC_KEY_SIZE;
//# sourceMappingURL=publickey.js.map
;