Documentation / @cashconnect-js/core / primitives/private-key
PrivateKeyFromArgs
type PrivateKeyFromArgs = Parameters<typeof from>[0];Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:532
PrivateKey
Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:34
Private Key entity.
Example
// Import our primitives
import { PrivateKey } from '@xocash/primitives';
// Create a Private Key from a WIF.
const privateKey = PrivateKey.from('wif');
// Derive the Public Key
const publicKey = PrivateKey.derivePublicKey();
// Output the CashAddr.
console.log(publicKey.deriveAddress().toCashAddr());Methods
from()
static from(privateKey): PrivateKey;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:65
Instantiates a Private Key from either Bytes, Hex or WIF.
Parameters
| Parameter | Type | Description |
|---|---|---|
privateKey | string | Uint8Array<ArrayBufferLike> | The private key as either Bytes, Hex or WIF. |
Returns
The created Private Key Primitive.
Throws
If the private key cannot be decoded.
fromBytes()
static fromBytes(privateKeyBytes): PrivateKey;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:86
Create a Private Key Entity from the given bytes.
Parameters
| Parameter | Type | Description |
|---|---|---|
privateKeyBytes | Uint8Array | {string} Raw Private Key bytes. |
Returns
The created Private Key Entity.
Throws
If Private Key could not be created.
fromHex()
static fromHex(privateKeyHex): PrivateKey;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:99
Create a Private Key Entity from the given bytes (as hexadecimal string).
Parameters
| Parameter | Type | Description |
|---|---|---|
privateKeyHex | string | {string} Raw Private Key bytes encoded as hexadecimal string. |
Returns
The created Private Key Entity.
Throws
If Private Key could not be created.
fromWIF()
static fromWIF(wif, removePrefixes): PrivateKey;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:114
Create a Private Key from a given WIF (Note that the network information from the WIF will be lost).
Parameters
| Parameter | Type |
|---|---|
wif | string |
removePrefixes | string[] |
Returns
The Private Key
Throws
If Private Key cannot be created.
fromRaw()
static fromRaw(bytes): PrivateKey;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:160
Creates an instance from serialized data.
Parameters
| Parameter | Type | Description |
|---|---|---|
bytes | Uint8Array | Serialized Primitive Data |
Returns
A new instance of this primitive.
Remarks
This method is UNSAFE and MUST only be used where input is already verified or trusted.
generateRandom()
static generateRandom(): PrivateKey;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:169
Generate a random Private Key.
Returns
The randomly generated Private Key
isWIF()
static isWIF(wif, removePrefixes): boolean;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:175
Parameters
| Parameter | Type |
|---|---|
wif | string |
removePrefixes | string[] |
Returns
boolean
toHex()
toHex(): string;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:189
Convert the raw bytes of this Private Key to hexadecimal encoding.
Returns
string
The raw bytes of the Private Key as a hexadecimal string.
toBytes()
toBytes(): Bytes;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:199
Convert this Private Key to a raw 32 byte representation.
Returns
The raw Private Key as a Uint8Array.
toWif()
toWif(type): string;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:211
Convert this Private Key to a WIF format.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
type | WalletImportFormatType | 'mainnet' | {WalletImportFormatType} The type of WIF to create. |
Returns
string
The Private Key in WIF format.
toString()
toString(): string;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:221
Returns the Private Key as a mainnet WIF.
Returns
string
The Private Key as a mainnet WIF.
toRaw()
toRaw(): Bytes;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:231
Converts this primitive to a serializable raw object representation.
Returns
Serialzied primitive data.
derivePublicKey()
derivePublicKey(): PublicKey;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:242
Derive the public key from this private key.
Returns
The derived Public Key.
Throws
If Public Key cannot be derived.
signMessageRecoverableCompact()
signMessageRecoverableCompact(message): RecoverableSignature;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:269
Sign a message using a recoverable compact signature format.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | Uint8Array<ArrayBufferLike> | The message to sign, either as a Uint8Array or string. If a string is provided, it will be converted to UTF-8 bytes. |
Returns
RecoverableSignature
The generated recoverable compact signature.
Remarks
This method will SHA256 hash the message for you.
Throws
If message cannot be signed.
signMessageCompact()
signMessageCompact(message): Uint8Array<ArrayBufferLike>;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:293
Sign a message using a compact signature format.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | Uint8Array<ArrayBufferLike> | The message to sign, either as a Uint8Array or string. If a string is provided, it will be converted to UTF-8 bytes. |
Returns
Uint8Array<ArrayBufferLike>
The generated compact signature.
Remarks
This method will SHA256 hash the message for you.
Throws
If message cannot be signed.
signMessageSchnorr()
signMessageSchnorr(message): Uint8Array<ArrayBufferLike>;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:317
Sign a message using the Schnorr signature scheme.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | Uint8Array<ArrayBufferLike> | The message to sign, either as a Uint8Array or string. If a string is provided, it will be converted to UTF-8 bytes. |
Returns
Uint8Array<ArrayBufferLike>
The generated Schnorr signature.
Remarks
This method will SHA256 hash the message for you.
Throws
If message cannot be signed.
signMessageDER()
signMessageDER(message): Uint8Array<ArrayBufferLike>;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:341
Sign a message using DER (Distinguished Encoding Rules) format.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | Uint8Array<ArrayBufferLike> | The message to sign, either as a Uint8Array or string. If a string is provided, it will be converted to UTF-8 bytes. |
Returns
Uint8Array<ArrayBufferLike>
The generated DER signature.
Remarks
This method will SHA256 hash the message for you.
Throws
If message cannot be signed.
signMessageElectron()
signMessageElectron(message): Uint8Array;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:364
Sign a message hash with this Private Key using an Electron Cash Signature.
This applies the Bitcoin and Electron Magic Bytes.
NOTE: Electron can verify this message successfully, but generates them differently internally.
Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | The message. |
Returns
Uint8Array
The generated signature.
Throws
If message hash cannot be signed.
signMessageHashRecoverableCompact()
signMessageHashRecoverableCompact(messageHash): RecoverableSignature;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:401
Sign a message hash with this Private Key using a Recoverable Compact Signature.
Parameters
| Parameter | Type | Description |
|---|---|---|
messageHash | string | Uint8Array<ArrayBufferLike> | {Uint8Array} The hash of the message. |
Returns
RecoverableSignature
The generated signature.
Throws
If message hash cannot be signed.
signMessageHashCompact()
signMessageHashCompact(messageHash): Uint8Array;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:438
Sign a message hash with this Private Key using a Compact Signature.
Parameters
| Parameter | Type | Description |
|---|---|---|
messageHash | string | Uint8Array<ArrayBufferLike> | {Uint8Array} The hash of the message. |
Returns
Uint8Array
The generated signature.
Throws
If message hash cannot be signed.
signMessageHashSchnorr()
signMessageHashSchnorr(messageHash): Uint8Array;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:473
Sign a message hash with this Private Key using a Schnorr Signature.
Parameters
| Parameter | Type | Description |
|---|---|---|
messageHash | string | Uint8Array<ArrayBufferLike> | {Uint8Array} The hash of the message. |
Returns
Uint8Array
The generated signature.
Throws
If message hash cannot be signed.
signMessageHashDER()
signMessageHashDER(messageHash): Uint8Array;Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:508
Sign a message hash with this Private Key using a DER Signature.
Parameters
| Parameter | Type | Description |
|---|---|---|
messageHash | string | Uint8Array<ArrayBufferLike> | {Uint8Array} The hash of the message. |
Returns
Uint8Array
The generated signature.
Throws
If message hash cannot be signed.