Skip to content

Documentation / @cashconnect-js/core / primitives/private-key

PrivateKeyFromArgs

ts
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

ts
// 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()

ts
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
ParameterTypeDescription
privateKeystring | Uint8Array<ArrayBufferLike>The private key as either Bytes, Hex or WIF.
Returns

PrivateKey

The created Private Key Primitive.

Throws

If the private key cannot be decoded.

fromBytes()

ts
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
ParameterTypeDescription
privateKeyBytesUint8Array{string} Raw Private Key bytes.
Returns

PrivateKey

The created Private Key Entity.

Throws

If Private Key could not be created.

fromHex()

ts
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
ParameterTypeDescription
privateKeyHexstring{string} Raw Private Key bytes encoded as hexadecimal string.
Returns

PrivateKey

The created Private Key Entity.

Throws

If Private Key could not be created.

fromWIF()

ts
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
ParameterType
wifstring
removePrefixesstring[]
Returns

PrivateKey

The Private Key

Throws

If Private Key cannot be created.

fromRaw()

ts
static fromRaw(bytes): PrivateKey;

Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:160

Creates an instance from serialized data.

Parameters
ParameterTypeDescription
bytesUint8ArraySerialized Primitive Data
Returns

PrivateKey

A new instance of this primitive.

Remarks

This method is UNSAFE and MUST only be used where input is already verified or trusted.

generateRandom()

ts
static generateRandom(): PrivateKey;

Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:169

Generate a random Private Key.

Returns

PrivateKey

The randomly generated Private Key

isWIF()

ts
static isWIF(wif, removePrefixes): boolean;

Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:175

Parameters
ParameterType
wifstring
removePrefixesstring[]
Returns

boolean

toHex()

ts
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()

ts
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

Bytes

The raw Private Key as a Uint8Array.

toWif()

ts
toWif(type): string;

Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:211

Convert this Private Key to a WIF format.

Parameters
ParameterTypeDefault valueDescription
typeWalletImportFormatType'mainnet'{WalletImportFormatType} The type of WIF to create.
Returns

string

The Private Key in WIF format.

toString()

ts
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()

ts
toRaw(): Bytes;

Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:231

Converts this primitive to a serializable raw object representation.

Returns

Bytes

Serialzied primitive data.

derivePublicKey()

ts
derivePublicKey(): PublicKey;

Defined in: cashconnect-js/packages/core/src/primitives/private-key.ts:242

Derive the public key from this private key.

Returns

PublicKey

The derived Public Key.

Throws

If Public Key cannot be derived.

signMessageRecoverableCompact()

ts
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
ParameterTypeDescription
messagestring | 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()

ts
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
ParameterTypeDescription
messagestring | 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()

ts
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
ParameterTypeDescription
messagestring | 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()

ts
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
ParameterTypeDescription
messagestring | 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()

ts
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
ParameterTypeDescription
messagestringThe message.
Returns

Uint8Array

The generated signature.

Throws

If message hash cannot be signed.

signMessageHashRecoverableCompact()

ts
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
ParameterTypeDescription
messageHashstring | Uint8Array<ArrayBufferLike>{Uint8Array} The hash of the message.
Returns

RecoverableSignature

The generated signature.

Throws

If message hash cannot be signed.

signMessageHashCompact()

ts
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
ParameterTypeDescription
messageHashstring | Uint8Array<ArrayBufferLike>{Uint8Array} The hash of the message.
Returns

Uint8Array

The generated signature.

Throws

If message hash cannot be signed.

signMessageHashSchnorr()

ts
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
ParameterTypeDescription
messageHashstring | Uint8Array<ArrayBufferLike>{Uint8Array} The hash of the message.
Returns

Uint8Array

The generated signature.

Throws

If message hash cannot be signed.

signMessageHashDER()

ts
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
ParameterTypeDescription
messageHashstring | Uint8Array<ArrayBufferLike>{Uint8Array} The hash of the message.
Returns

Uint8Array

The generated signature.

Throws

If message hash cannot be signed.