@libp2p/crypto
Version:
Crypto primitives for libp2p
54 lines • 1.73 kB
JavaScript
import { base58btc } from 'multiformats/bases/base58';
import { CID } from 'multiformats/cid';
import {} from 'multiformats/hashes/digest';
import { identity } from 'multiformats/hashes/identity';
import { equals as uint8ArrayEquals } from 'uint8arrays/equals';
import { publicKeyToProtobuf } from '../index.js';
import { ensureEd25519Key } from './utils.js';
import * as crypto from './index.js';
export class Ed25519PublicKey {
type = 'Ed25519';
raw;
constructor(key) {
this.raw = ensureEd25519Key(key, crypto.publicKeyLength);
}
toMultihash() {
return identity.digest(publicKeyToProtobuf(this));
}
toCID() {
return CID.createV1(114, this.toMultihash());
}
toString() {
return base58btc.encode(this.toMultihash().bytes).substring(1);
}
equals(key) {
if (key == null || !(key.raw instanceof Uint8Array)) {
return false;
}
return uint8ArrayEquals(this.raw, key.raw);
}
verify(data, sig) {
return crypto.hashAndVerify(this.raw, sig, data);
}
}
export class Ed25519PrivateKey {
type = 'Ed25519';
raw;
publicKey;
// key - 64 byte Uint8Array containing private key
// publicKey - 32 byte Uint8Array containing public key
constructor(key, publicKey) {
this.raw = ensureEd25519Key(key, crypto.privateKeyLength);
this.publicKey = new Ed25519PublicKey(publicKey);
}
equals(key) {
if (key == null || !(key.raw instanceof Uint8Array)) {
return false;
}
return uint8ArrayEquals(this.raw, key.raw);
}
sign(message) {
return crypto.hashAndSign(this.raw, message);
}
}
//# sourceMappingURL=ed25519.js.map