@roochnetwork/rooch-sdk
Version:
65 lines (64 loc) • 1.88 kB
JavaScript
import nacl from "tweetnacl";
import { ROOCH_ADDRESS_LENGTH, RoochAddress } from "../../address/index.js";
import { PublicKey, SIGNATURE_SCHEME_TO_FLAG } from "../../crypto/index.js";
import { blake2b, fromB64 } from "../../utils/index.js";
const PUBLIC_KEY_SIZE = 32;
class Ed25519PublicKey extends PublicKey {
/**
* Create a new Ed25519PublicKey object
* @param value ed25519 public key as buffer or base-64 encoded string
*/
constructor(value) {
super();
if (typeof value === "string") {
this.data = fromB64(value);
} else if (value instanceof Uint8Array) {
this.data = value;
} else {
this.data = Uint8Array.from(value);
}
if (this.data.length !== PUBLIC_KEY_SIZE) {
throw new Error(
`Invalid public key input. Expected ${PUBLIC_KEY_SIZE} bytes, got ${this.data.length}`
);
}
}
/**
* Checks if two Ed25519 public keys are equal
*/
equals(publicKey) {
return super.equals(publicKey);
}
/**
* Return the byte array representation of the Ed25519 public key
*/
toBytes() {
return this.data;
}
/**
* Return the Rooch address associated with this Ed25519 public key
*/
flag() {
return SIGNATURE_SCHEME_TO_FLAG.ED25519;
}
/**
* Verifies that the signature is valid for the provided message
*/
async verify(message, signature) {
return nacl.sign.detached.verify(message, signature, this.toBytes());
}
/**
* Return the Rooch address associated with this Ed25519 public key
*/
toAddress() {
const tmp = new Uint8Array(PUBLIC_KEY_SIZE + 1);
tmp.set([SIGNATURE_SCHEME_TO_FLAG.ED25519]);
tmp.set(this.toBytes(), 1);
return new RoochAddress(blake2b(tmp, { dkLen: 32 }).slice(0, ROOCH_ADDRESS_LENGTH * 2));
}
}
Ed25519PublicKey.SIZE = PUBLIC_KEY_SIZE;
export {
Ed25519PublicKey
};
//# sourceMappingURL=publickey.js.map