@roochnetwork/rooch-sdk
Version:
100 lines (99 loc) • 3.46 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, {
Secp256k1PublicKey: () => Secp256k1PublicKey
});
module.exports = __toCommonJS(publickey_exports);
var import_secp256k1 = require("@noble/curves/secp256k1");
var import_address = require("../../address/index.js");
var import_crypto = require("../../crypto/index.js");
var import_utils = require("../../utils/index.js");
const SCHNORR_PUBLIC_KEY_SIZE = 32;
const ECDSA_PUBLIC_KEY_SIZE = 33;
class Secp256k1PublicKey extends import_crypto.PublicKey {
/**
* Create a new Secp256k1PublicKey object
* @param value secp256k1 public key as buffer or base-64 encoded string
*/
constructor(value) {
super();
if (typeof value === "string") {
this.data = (0, import_utils.fromB64)(value);
} else if (value instanceof Uint8Array) {
this.data = value;
} else {
this.data = Uint8Array.from(value);
}
if (this.data.length !== SCHNORR_PUBLIC_KEY_SIZE && this.data.length !== ECDSA_PUBLIC_KEY_SIZE) {
throw new Error(
`Invalid public key input. Expected ${SCHNORR_PUBLIC_KEY_SIZE} or ${ECDSA_PUBLIC_KEY_SIZE} bytes, got ${this.data.length}`
);
}
}
/**
* Checks if two Secp256k1 public keys are equal
*/
equals(publicKey) {
return super.equals(publicKey);
}
/**
* Return the byte array representation of the Secp256k1 public key
*/
toBytes() {
return this.data;
}
toString() {
return (0, import_utils.toHEX)(this.data);
}
/**
* Return the Bitcoin address associated with this Secp256k1 public key
*/
toAddress() {
return new import_address.AddressView(this.data);
}
toAddressWith(network) {
return new import_address.AddressView(this.data, network);
}
/**
* Return the Rooch address associated with this Secp256k1 public key
*/
flag() {
return import_crypto.SIGNATURE_SCHEME_TO_FLAG["Secp256k1"];
}
/**
* Verifies that the ecdsa signature is valid for the provided sha256 hashed message with 33 bytes public key
*/
async verify(message, signature) {
return import_secp256k1.secp256k1.verify(
import_secp256k1.secp256k1.Signature.fromCompact(signature),
(0, import_utils.sha256)(message),
this.toBytes()
);
}
/**
* Verifies that the schnorr signature is valid for the provided message with 32 bytes public key
*/
async verify_schnorr(message, signature) {
return import_secp256k1.schnorr.verify(signature, message, this.toBytes());
}
}
Secp256k1PublicKey.SCHNORR_PUBKEY_SIZE = SCHNORR_PUBLIC_KEY_SIZE;
Secp256k1PublicKey.ECDSA_PUBKEY_SIZE = ECDSA_PUBLIC_KEY_SIZE;
//# sourceMappingURL=publickey.js.map