UNPKG

@crpdo/key

Version:

Streamlines key generation, derivation, and management through its simple and intuitive API

548 lines (398 loc) 20.9 kB
## Classes <dl> <dt><a href="#BaseKey">BaseKey</a></dt> <dd><p>BaseKey Class is a wrapper around a key object that allows accessing public and private keys.</p> </dd> <dt><a href="#BoxKey">BoxKey</a><code><a href="#BaseKey">BaseKey</a></code></dt> <dd><p>Class representing a BoxKey. BoxKey is a key used for encryption and decryption.</p> </dd> <dt><a href="#HdKey">HdKey</a><code><a href="#BaseKey">BaseKey</a></code></dt> <dd><p>Class representing a Hierarchical Deterministic Key (HdKey)</p> </dd> <dt><a href="#Key">Key</a></dt> <dd><p>Key class represents a cryptographic key with additional properties and methods.</p> </dd> <dt><a href="#SignKey">SignKey</a></dt> <dd><p>This class extends BaseKey and provides methods for signing and verifying messages using the Nacl library. The messages are signed using the privateKey property of the SignKey instance, and they can be verified using a given publicKey or the publicKey property of the SignKey instance.</p> </dd> </dl> <a name="BaseKey"></a> ## BaseKey BaseKey Class is a wrapper around a key object that allows accessing public and private keys. **Kind**: global class * [BaseKey](#BaseKey) * [new BaseKey(key)](#new_BaseKey_new) * [.publicKey](#BaseKey+publicKey) ⇒ <code>string</code> * [.privateKey](#BaseKey+privateKey) ⇒ <code>string</code> <a name="new_BaseKey_new"></a> ### new BaseKey(key) Constructs a new BaseKey instance. | Param | Type | Description | | --- | --- | --- | | key | <code>Object</code> | An object representing a key with publicKey and privateKey properties. | <a name="BaseKey+publicKey"></a> ### baseKey.publicKey ⇒ <code>string</code> Accessor method for the public key. **Kind**: instance property of [<code>BaseKey</code>](#BaseKey) **Returns**: <code>string</code> - The public key from the key object. **Access**: public <a name="BaseKey+privateKey"></a> ### baseKey.privateKey ⇒ <code>string</code> Accessor method for the private key. **Kind**: instance property of [<code>BaseKey</code>](#BaseKey) **Returns**: <code>string</code> - The private key from the key object. **Access**: public <a name="BoxKey"></a> ## BoxKey ⇐ [<code>BaseKey</code>](#BaseKey) Class representing a BoxKey. BoxKey is a key used for encryption and decryption. **Kind**: global class **Extends**: [<code>BaseKey</code>](#BaseKey) * [BoxKey](#BoxKey) ⇐ [<code>BaseKey</code>](#BaseKey) * [new BoxKey(seed)](#new_BoxKey_new) * _instance_ * [.publicKey](#BaseKey+publicKey) ⇒ <code>string</code> * [.privateKey](#BaseKey+privateKey) ⇒ <code>string</code> * [.encrypt(data, publicKey, nonce, encode)](#BoxKey+encrypt) ⇒ <code>string</code> * [.decrypt(data, publicKey, nonce, encode)](#BoxKey+decrypt) ⇒ <code>string</code> * _static_ * [.fromSignKey(keyPair)](#BoxKey.fromSignKey) ⇒ [<code>BoxKey</code>](#BoxKey) <a name="new_BoxKey_new"></a> ### new BoxKey(seed) Create a BoxKey. | Param | Type | Description | | --- | --- | --- | | seed | <code>string</code> | The seed used to create the BoxKey. | <a name="BaseKey+publicKey"></a> ### boxKey.publicKey ⇒ <code>string</code> Accessor method for the public key. **Kind**: instance property of [<code>BoxKey</code>](#BoxKey) **Overrides**: [<code>publicKey</code>](#BaseKey+publicKey) **Returns**: <code>string</code> - The public key from the key object. **Access**: public <a name="BaseKey+privateKey"></a> ### boxKey.privateKey ⇒ <code>string</code> Accessor method for the private key. **Kind**: instance property of [<code>BoxKey</code>](#BoxKey) **Overrides**: [<code>privateKey</code>](#BaseKey+privateKey) **Returns**: <code>string</code> - The private key from the key object. **Access**: public <a name="BoxKey+encrypt"></a> ### boxKey.encrypt(data, publicKey, nonce, encode) ⇒ <code>string</code> Encrypts data. **Kind**: instance method of [<code>BoxKey</code>](#BoxKey) **Returns**: <code>string</code> - The encrypted data. | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> | The data to be encrypted. | | publicKey | <code>string</code> | The public key used for encryption. Defaults to the BoxKey's public key if not provided. | | nonce | <code>string</code> | The nonce used for encryption. | | encode | <code>boolean</code> | Flag to encode the result. If true, the result is base64 encoded. | <a name="BoxKey+decrypt"></a> ### boxKey.decrypt(data, publicKey, nonce, encode) ⇒ <code>string</code> Decrypts data. **Kind**: instance method of [<code>BoxKey</code>](#BoxKey) **Returns**: <code>string</code> - The decrypted data. | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> | The data to be decrypted. | | publicKey | <code>string</code> | The public key used for decryption. Defaults to the BoxKey's public key if not provided. | | nonce | <code>string</code> | The nonce used for decryption. | | encode | <code>boolean</code> | Flag to decode the result. If true, the result is base64 decoded. | <a name="BoxKey.fromSignKey"></a> ### BoxKey.fromSignKey(keyPair) ⇒ [<code>BoxKey</code>](#BoxKey) Converts a SignKey to a BoxKey. **Kind**: static method of [<code>BoxKey</code>](#BoxKey) **Returns**: [<code>BoxKey</code>](#BoxKey) - The converted BoxKey. | Param | Type | Description | | --- | --- | --- | | keyPair | <code>KeyPair</code> | The SignKey to be converted. | <a name="HdKey"></a> ## HdKey ⇐ [<code>BaseKey</code>](#BaseKey) Class representing a Hierarchical Deterministic Key (HdKey) **Kind**: global class **Extends**: [<code>BaseKey</code>](#BaseKey) * [HdKey](#HdKey) ⇐ [<code>BaseKey</code>](#BaseKey) * [new HdKey(seed)](#new_HdKey_new) * _instance_ * [.depth](#HdKey+depth) ⇒ <code>number</code> * [.publicKey](#HdKey+publicKey) ⇒ <code>string</code> * [.privateKey](#HdKey+privateKey) ⇒ <code>string</code> * [.publicExtendedKey](#HdKey+publicExtendedKey) ⇒ <code>string</code> * [.privateExtendedKey](#HdKey+privateExtendedKey) ⇒ <code>string</code> * [.derive(path)](#HdKey+derive) ⇒ <code>HDKey</code> * [.sign(data)](#HdKey+sign) ⇒ <code>string</code> * [.verify(data, signature, publicKey)](#HdKey+verify) ⇒ <code>boolean</code> * [.deriveSoftened(coinSymbol, account, change, index)](#HdKey+deriveSoftened) ⇒ <code>HDKey</code> * [.deriveSoftenedPublicKey(coinSymbol, account, change, index)](#HdKey+deriveSoftenedPublicKey) ⇒ <code>string</code> * _static_ * [.deriveNode(input, [path])](#HdKey.deriveNode) ⇒ <code>HDKey</code> * [.deriveSoftenedNode(input, ...args)](#HdKey.deriveSoftenedNode) ⇒ <code>HDKey</code> * [.deriveSoftenedPublicKey(input, ...args)](#HdKey.deriveSoftenedPublicKey) ⇒ <code>string</code> * [.neuterNode(node)](#HdKey.neuterNode) <a name="new_HdKey_new"></a> ### new HdKey(seed) Create a HdKey. | Param | Type | Description | | --- | --- | --- | | seed | <code>string</code> | The seed to initialize the HdKey. | <a name="HdKey+depth"></a> ### hdKey.depth ⇒ <code>number</code> Get the depth of the key derivation. **Kind**: instance property of [<code>HdKey</code>](#HdKey) **Returns**: <code>number</code> - The depth of the key. <a name="HdKey+publicKey"></a> ### hdKey.publicKey ⇒ <code>string</code> Get the public key. **Kind**: instance property of [<code>HdKey</code>](#HdKey) **Overrides**: [<code>publicKey</code>](#BaseKey+publicKey) **Returns**: <code>string</code> - The public key. <a name="HdKey+privateKey"></a> ### hdKey.privateKey ⇒ <code>string</code> Get the private key. **Kind**: instance property of [<code>HdKey</code>](#HdKey) **Overrides**: [<code>privateKey</code>](#BaseKey+privateKey) **Returns**: <code>string</code> - The private key. <a name="HdKey+publicExtendedKey"></a> ### hdKey.publicExtendedKey ⇒ <code>string</code> Get the public extended key. **Kind**: instance property of [<code>HdKey</code>](#HdKey) **Returns**: <code>string</code> - The public extended key. <a name="HdKey+privateExtendedKey"></a> ### hdKey.privateExtendedKey ⇒ <code>string</code> Get the private extended key. **Kind**: instance property of [<code>HdKey</code>](#HdKey) **Returns**: <code>string</code> - The private extended key. <a name="HdKey+derive"></a> ### hdKey.derive(path) ⇒ <code>HDKey</code> Derive a new key from the current key. **Kind**: instance method of [<code>HdKey</code>](#HdKey) **Returns**: <code>HDKey</code> - The derived key. | Param | Type | Description | | --- | --- | --- | | path | <code>string</code> | The derivation path. | <a name="HdKey+sign"></a> ### hdKey.sign(data) ⇒ <code>string</code> Sign a piece of data with this key. **Kind**: instance method of [<code>HdKey</code>](#HdKey) **Returns**: <code>string</code> - The signature. | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> \| <code>Buffer</code> | The data to sign. | <a name="HdKey+verify"></a> ### hdKey.verify(data, signature, publicKey) ⇒ <code>boolean</code> Verify a signature on a piece of data with this key. **Kind**: instance method of [<code>HdKey</code>](#HdKey) **Returns**: <code>boolean</code> - Whether the signature is valid. | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> \| <code>Buffer</code> | The data whose signature to verify. | | signature | <code>string</code> | The signature to verify. | | publicKey | <code>HDKey</code> | The public key to verify the signature against. | <a name="HdKey+deriveSoftened"></a> ### hdKey.deriveSoftened(coinSymbol, account, change, index) ⇒ <code>HDKey</code> Derive a softened key from the current key. **Kind**: instance method of [<code>HdKey</code>](#HdKey) **Returns**: <code>HDKey</code> - The derived softened key. | Param | Type | Default | Description | | --- | --- | --- | --- | | coinSymbol | <code>string</code> | <code>&quot;BTC&quot;</code> | The coin symbol, default is 'BTC'. | | account | <code>number</code> | <code>0</code> | The account number, default is 0. | | change | <code>number</code> | <code>0</code> | The change number, default is 0. | | index | <code>number</code> | | The index number. | <a name="HdKey+deriveSoftenedPublicKey"></a> ### hdKey.deriveSoftenedPublicKey(coinSymbol, account, change, index) ⇒ <code>string</code> Derive a softened public key from the current key. **Kind**: instance method of [<code>HdKey</code>](#HdKey) **Returns**: <code>string</code> - The derived softened public key. | Param | Type | Default | Description | | --- | --- | --- | --- | | coinSymbol | <code>string</code> | <code>&quot;BTC&quot;</code> | The coin symbol, default is 'BTC'. | | account | <code>number</code> | <code>0</code> | The account number, default is 0. | | change | <code>number</code> | <code>0</code> | The change number, default is 0. | | index | <code>number</code> | | The index number. | <a name="HdKey.deriveNode"></a> ### HdKey.deriveNode(input, [path]) ⇒ <code>HDKey</code> Derives a node from an input and path. **Kind**: static method of [<code>HdKey</code>](#HdKey) **Returns**: <code>HDKey</code> - The derived node. | Param | Type | Default | Description | | --- | --- | --- | --- | | input | <code>HDKey</code> \| <code>string</code> | | The input to derive the node from, either an instance of HDKey or a string to create an HDKey from. | | [path] | <code>string</code> | <code>&quot;&#x27;m&#x27;&quot;</code> | The path to derive the node at. | <a name="HdKey.deriveSoftenedNode"></a> ### HdKey.deriveSoftenedNode(input, ...args) ⇒ <code>HDKey</code> Derives a softened node from an input and additional arguments. **Kind**: static method of [<code>HdKey</code>](#HdKey) **Returns**: <code>HDKey</code> - The derived softened node. | Param | Type | Description | | --- | --- | --- | | input | <code>HDKey</code> \| <code>string</code> | The input to derive the softened node from, either an instance of HDKey or a string to create an HDKey from. | | ...args | <code>any</code> | The additional arguments for deriving the path. | <a name="HdKey.deriveSoftenedPublicKey"></a> ### HdKey.deriveSoftenedPublicKey(input, ...args) ⇒ <code>string</code> Derives a softened public key from an input and additional arguments. **Kind**: static method of [<code>HdKey</code>](#HdKey) **Returns**: <code>string</code> - The derived softened public key. | Param | Type | Description | | --- | --- | --- | | input | <code>HDKey</code> \| <code>string</code> | The input to derive the softened public key from, either an instance of HDKey or a string to create an HDKey from. | | ...args | <code>any</code> | The additional arguments for deriving the path. | <a name="HdKey.neuterNode"></a> ### HdKey.neuterNode(node) Removes private data from a node, rendering it a public node. **Kind**: static method of [<code>HdKey</code>](#HdKey) | Param | Type | Description | | --- | --- | --- | | node | <code>HDKey</code> | The node to neuter. | <a name="Key"></a> ## Key Key class represents a cryptographic key with additional properties and methods. **Kind**: global class **Properties** | Name | Type | Description | | --- | --- | --- | | Base | [<code>BaseKey</code>](#BaseKey) | Base key | | Sign | [<code>SignKey</code>](#SignKey) | Sign key | | Box | [<code>BoxKey</code>](#BoxKey) | Box key | | Hd | [<code>HdKey</code>](#HdKey) | Hierarchical deterministic key | * [Key](#Key) * [new Key([options])](#new_Key_new) * _instance_ * [.publicKey](#Key+publicKey) ⇒ <code>string</code> * [.privateKey](#Key+privateKey) ⇒ <code>string</code> * [.derive(slug, [opts], [isChild])](#Key+derive) ⇒ [<code>Key</code>](#Key) * [.ratchet(index, [opts])](#Key+ratchet) ⇒ [<code>Key</code>](#Key) * [.sign(data)](#Key+sign) ⇒ <code>string</code> * [.verify(data, signature, publicKey)](#Key+verify) ⇒ <code>boolean</code> * [.encrypt(data, publicKey, [nonce], [encode])](#Key+encrypt) ⇒ <code>string</code> * [.decrypt(data, publicKey, [nonce], [encode])](#Key+decrypt) ⇒ <code>string</code> * _static_ * [.generateSeed()](#Key.generateSeed) ⇒ <code>string</code> <a name="new_Key_new"></a> ### new Key([options]) Create a new instance of the Key class. | Param | Type | Default | Description | | --- | --- | --- | --- | | [options] | <code>Object</code> | <code>{}</code> | Options to create the Key instance. If options is a string, it is used as the seed. Options can include properties like 'seed', 'list', 'dpath', 'dlist', 'path', 'index'. | <a name="Key+publicKey"></a> ### key.publicKey ⇒ <code>string</code> Get the public key. **Kind**: instance property of [<code>Key</code>](#Key) **Returns**: <code>string</code> - Public key of the instance. <a name="Key+privateKey"></a> ### key.privateKey ⇒ <code>string</code> Get the private key. **Kind**: instance property of [<code>Key</code>](#Key) **Returns**: <code>string</code> - Private key of the instance. <a name="Key+derive"></a> ### key.derive(slug, [opts], [isChild]) ⇒ [<code>Key</code>](#Key) Derives a new key from the current key. **Kind**: instance method of [<code>Key</code>](#Key) **Returns**: [<code>Key</code>](#Key) - The derived key. | Param | Type | Default | Description | | --- | --- | --- | --- | | slug | <code>string</code> \| <code>number</code> \| <code>Array</code> | | The identifier of the new derived key. | | [opts] | <code>Object</code> | <code>{}</code> | Additional options for the derivation. | | [isChild] | <code>boolean</code> | <code>true</code> | Whether the derived key is a child key. | <a name="Key+ratchet"></a> ### key.ratchet(index, [opts]) ⇒ [<code>Key</code>](#Key) Ratchets the keys based on the index and additional options. **Kind**: instance method of [<code>Key</code>](#Key) **Returns**: [<code>Key</code>](#Key) - The key after ratcheting. | Param | Type | Default | Description | | --- | --- | --- | --- | | index | <code>number</code> | | The index to use for ratcheting. | | [opts] | <code>Object</code> | <code>{}</code> | Additional options for the ratcheting. | <a name="Key+sign"></a> ### key.sign(data) ⇒ <code>string</code> Sign a data with the private key. **Kind**: instance method of [<code>Key</code>](#Key) **Returns**: <code>string</code> - The signature of the data. | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> \| <code>Buffer</code> | The data to sign. | <a name="Key+verify"></a> ### key.verify(data, signature, publicKey) ⇒ <code>boolean</code> Verify a signature for a data with the given public key. **Kind**: instance method of [<code>Key</code>](#Key) **Returns**: <code>boolean</code> - True if the signature is valid, false otherwise. | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> \| <code>Buffer</code> | The data to verify. | | signature | <code>string</code> | The signature to verify. | | publicKey | <code>string</code> | The public key to use for verification. | <a name="Key+encrypt"></a> ### key.encrypt(data, publicKey, [nonce], [encode]) ⇒ <code>string</code> Encrypts a data with the given public key. **Kind**: instance method of [<code>Key</code>](#Key) **Returns**: <code>string</code> - The encrypted data. | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> \| <code>Buffer</code> | The data to encrypt. | | publicKey | <code>string</code> | The public key to use for encryption. | | [nonce] | <code>string</code> | The nonce used for encryption. | | [encode] | <code>boolean</code> | Whether to encode the encrypted data. | <a name="Key+decrypt"></a> ### key.decrypt(data, publicKey, [nonce], [encode]) ⇒ <code>string</code> Decrypts a data with the given public key. **Kind**: instance method of [<code>Key</code>](#Key) **Returns**: <code>string</code> - The decrypted data. | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> \| <code>Buffer</code> | The encrypted data to decrypt. | | publicKey | <code>string</code> | The public key to use for decryption. | | [nonce] | <code>string</code> | The nonce used for decryption. | | [encode] | <code>boolean</code> | Whether to encode the decrypted data. | <a name="Key.generateSeed"></a> ### Key.generateSeed() ⇒ <code>string</code> Generates a new seed for key creation. **Kind**: static method of [<code>Key</code>](#Key) **Returns**: <code>string</code> - The generated seed. <a name="SignKey"></a> ## SignKey This class extends BaseKey and provides methods for signing and verifying messages using the Nacl library. The messages are signed using the privateKey property of the SignKey instance, and they can be verified using a given publicKey or the publicKey property of the SignKey instance. **Kind**: global class * [SignKey](#SignKey) * [new SignKey(seed)](#new_SignKey_new) * _instance_ * [.sign(data)](#SignKey+sign) ⇒ <code>string</code> * [.verify(data, signature, [publicKey])](#SignKey+verify) ⇒ <code>boolean</code> * _static_ * [.fromHdKey(hd)](#SignKey.fromHdKey) ⇒ [<code>SignKey</code>](#SignKey) <a name="new_SignKey_new"></a> ### new SignKey(seed) | Param | Type | Description | | --- | --- | --- | | seed | <code>string</code> | A seed string used to create the SignKey | <a name="SignKey+sign"></a> ### signKey.sign(data) ⇒ <code>string</code> **Kind**: instance method of [<code>SignKey</code>](#SignKey) **Returns**: <code>string</code> - - The signed data | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> | The data to sign | <a name="SignKey+verify"></a> ### signKey.verify(data, signature, [publicKey]) ⇒ <code>boolean</code> **Kind**: instance method of [<code>SignKey</code>](#SignKey) **Returns**: <code>boolean</code> - - Returns true if the signature is valid, false otherwise | Param | Type | Description | | --- | --- | --- | | data | <code>string</code> | The data to verify | | signature | <code>string</code> | The signature to check the data against | | [publicKey] | <code>string</code> | The public key to use for verification. If not provided, the publicKey of the SignKey instance is used. | <a name="SignKey.fromHdKey"></a> ### SignKey.fromHdKey(hd) ⇒ [<code>SignKey</code>](#SignKey) Generates a SignKey from a given HDKey **Kind**: static method of [<code>SignKey</code>](#SignKey) **Returns**: [<code>SignKey</code>](#SignKey) - - A new SignKey instance | Param | Type | Description | | --- | --- | --- | | hd | <code>object</code> | An HDKey instance |