import nacl from 'tweetnacl' ; import { Private , Public } from './identity' ; import { encodePrivateKey , encodePublicKey , KeyType } from './proto.keys' ; import { decrypt , encrypt , privateKeyFromString , privateKeyToString , publicKeyBytesFromString , publicKeyToString , } from './utils' ; /**
 * Default implementation of the  Public interface, with encryption
 * support.
 * In theory, RSA, ed25519, and secp256k1 key types (and more) should be
 * supported, although currently only ed25519 has full verify and encrypt
 * capabilities.
 *  privateRemarks privateRemarks example example example example export class PublicKey implements Public { constructor ( public pubKey , public type = 'ed25519' ) { if ( type !== 'ed25519' ) { throw new Error ( 'Invalid keys type' ) ; } this . type = type || 'ed25519' ; } param data param sig async verify ( data , signature ) { return nacl . sign . detached . verify ( data , signature , this . pubKey ) ; } /**
     * Return the base32-encoded multibase string of the `bytes` of this public
     * key. Useful for encoding the key for sharing etc.
     */ toString ( ) { return publicKeyToString ( this ) ; } /**
     * Create a PublicKey from the result of calling `toString()`.
     */ static fromString ( str ) { const bytes = publicKeyBytesFromString ( str ) ; return new PublicKey ( bytes , 'ed25519' ) ; } /**
     * Return the protobuf-encoded bytes of this public key.
     */ get bytes ( ) : Uint8Array { return encodePublicKey ( { Type : KeyType . Ed25519 , Data : this . pubKey , } ) ; } param data encrypt ( data ) { return encrypt ( data , this . pubKey , this . type ) ; } } /**
 * Default implementation of the  Private interface, with decryption
 * support.
 * In theory, RSA, ed25519, and secp256k1 key types (and more) should be
 * supported, although currently only ed25519 has full sign and decrypt
 * capabilities.
 *  privateRemarks privateRemarks example example example example export class PrivateKey implements Private { /**
     * The raw public key bytes.
     */ pubKey : Uint8Array ; /**
     * The raw 32-byte secret seed
     */ seed : Uint8Array ; /**
     * The raw private key bytes.
     */ privKey : Uint8Array ; param secretKey param type constructor ( secretKey , public type = 'ed25519' ) { if ( type !== 'ed25519' ) { throw new Error ( 'Invalid keys type' ) ; } this . type = type || 'ed25519' ; const secret = Uint8Array . from ( secretKey ) ; if ( secret . length !== 32 ) { throw new Error ( 'secretKey length is invalid' ) ; } const naclKeys = nacl . sign . keyPair . fromSeed ( secret ) ; this . seed = secret ; this . privKey = naclKeys . secretKey ; this . pubKey = naclKeys . publicKey ; } /**
     * Returns `true` if this key contains a secret key and can sign.
     */ canSign ( ) { return ! ! this . privKey ; } param data async sign ( data ) { if ( ! this . canSign ( ) ) { throw new Error ( 'cannot sign: no secret key available' ) ; } return nacl . sign . detached ( data , this . privKey ) ; } /**
     * Get the public key associated with this identity.
     */ get public ( ) : PublicKey { return new PublicKey ( this . pubKey ) ; } /**
     * Return the protobuf-encoded bytes of this private key.
     */ get bytes ( ) : Uint8Array { return encodePrivateKey ( { Type : KeyType . Ed25519 , Data : this . privKey , } ) ; } param rawSeed static fromRawEd25519Seed ( rawSeed ) { return new this ( rawSeed , 'ed25519' ) ; } /**
     * Create a random PrivateKey.
     */ static fromRandom ( ) { const secret = nacl . randomBytes ( 32 ) ; return this . fromRawEd25519Seed ( secret ) ; } /**
     * Return the base32-encoded multibase string of the `bytes` of this private
     * key. Useful for encoding the key for sharing etc.
     */ toString ( ) { return privateKeyToString ( this ) ; } /**
     * Create a PrivateKey from the result of calling `toString()`.
     */ static fromString ( str ) { return new PrivateKey ( privateKeyFromString ( str ) , 'ed25519' ) ; } param ciphertext decrypt ( ciphertext ) { return decrypt ( ciphertext , this . privKey , this . type ) ; } }