Class Signer

The Signer Class contains the private key needed to sign transactions. It can be created using the seed, wif, or private key

Example

using private key as hex string

var privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
var signer = new Signer({ privateKey });

using private key as Uint8Array

var buffer = new Uint8Array([
236, 134, 1, 162, 79, 129, 222, 205,
87, 244, 182, 17, 181, 172, 110, 184,
1, 203, 55, 128, 187, 2, 192, 249,
205, 254, 157, 9, 218, 173, 223, 156
]);
var signer = new Signer({ privateKey: buffer });

using private key as bigint

var privateKey = 106982601049961974618234078204952280507266494766432547312316920283818886029212n;
var signer = new Signer({ privateKey });

using the seed

var signer = Signer.fromSeed("my seed");

using private key in WIF format

var signer = Signer.fromWif("L59UtJcTdNBnrH2QSBA5beSUhRufRu3g6tScDTite6Msuj7U93tM");

defining a provider

var provider = new Provider(["https://example.com/jsonrpc"]);
var privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
var signer = new Signer({ privateKey, provider });

Hierarchy

  • Signer

Implements

Constructors

  • The constructor receives de private key as hexstring, bigint or Uint8Array. See also the functions Signer.fromWif and Signer.fromSeed to create the signer from the WIF or Seed respectively.

    Example

    const privateKey = "ec8601a24f81decd57f4b611b5ac6eb801cb3780bb02c0f9cdfe9d09daaddf9c";
    cons signer = new Signer({ privateKey });
    console.log(signer.getAddress());
    // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b

    Parameters

    Returns Signer

Properties

address: string

Account address

compressed: boolean

Boolean determining if the public/private key is using the compressed format

privateKey: string | number | bigint | Uint8Array

Provider to connect with the blockchain

publicKey: string | Uint8Array

Options to apply when sending a transaction. By default broadcast is true and the other fields are undefined

Methods

  • Returns

    Signer address

    Parameters

    • compressed: boolean = true

      determines if the address should be derived from the compressed public key (default) or the public key

    Returns string

  • Function to get the private key in hex format or wif format

    Example

    const signer = Signer.fromSeed("one two three four five six");
    console.log(signer.getPrivateKey());
    // bab7fd6e5bd624f4ea0c33f7e7219262a6fa93a945a8964d9f110148286b7b37

    console.log(signer.getPrivateKey("wif"));
    // L3UfgFJWmbVziGB1uZBjkG1UjKkF7hhpXWY7mbTUdmycmvXCVtiL

    console.log(signer.getPrivateKey("wif", false));
    // 5KEX4TMHG66fT7cM9HMZLmdp4hVq4LC4X2Fkg6zeypM5UteWmtd

    Parameters

    • format: "hex" | "wif" = "hex"

      The format must be "hex" (default) or "wif"

    • compressed: boolean = true

      Optional arg when using WIF format. By default it uses the compressed value defined in the signer

    Returns string

  • Function to sign a block for federated consensus. That is, just the ecdsa signature. For other algorithms, like PoW, you have to sign the block and then process the signature to add the extra data (nonce in the case of PoW).

    Parameters

    Returns Promise<BlockJson>

  • Function to sign a hash value. It returns the bytes signature. The signature is in compact format with the recovery byte

    Parameters

    • hash: Uint8Array

      Hash value. Also known as digest

    Returns Promise<Uint8Array>

  • Function to sign a message, which could be a string or a Uint8Array

    Parameters

    • message: string | Uint8Array

    Returns Promise<Uint8Array>

  • Function to import a private key from the seed

    Example

    const signer = Signer.fromSeed("my seed");
    console.log(signer.getAddress());
    // 1BqtgWBcqm9cSZ97avLGZGJdgso7wx6pCA

    Returns

    Signer object

    Parameters

    • seed: string

      Seed words

    • compressed: boolean = true

    Returns Signer

  • Function to import a private key from the WIF

    Example

    const signer = Signer.fromWif("L59UtJcTdNBnrH2QSBA5beSUhRufRu3g6tScDTite6Msuj7U93tM")
    console.log(signer.getAddress());
    // 1MbL6mG8ASAvSYdoMnGUfG3ZXkmQ2dpL5b

    Returns

    Signer object

    Parameters

    • wif: string

      Private key in WIF format

    • compressed: boolean = true

    Returns Signer

  • Parameters

    • hash: Uint8Array
    • signature: Uint8Array
    • compressed: boolean = true

    Returns string

  • Function to recover the signer addresses from a signed transaction or block. The output format can be compressed (default) or uncompressed.

    Example

    const addresses = await signer.recoverAddress(tx);
    

    If the signature data contains more data, like in the blocks for PoW consensus, use the "transformSignature" function to extract the signature.

    Example

     const powDescriptorJson = {
    nested: {
    mypackage: {
    nested: {
    pow_signature_data: {
    fields: {
    nonce: {
    type: "bytes",
    id: 1,
    },
    recoverable_signature: {
    type: "bytes",
    id: 2,
    },
    },
    },
    },
    },
    },
    };

    const serializer = new Serializer(powDescriptorJson, {
    defaultTypeName: "pow_signature_data",
    });

    const addresses = await signer.recoverAddress(block, {
    transformSignature: async (signatureData) => {
    const powSignatureData = await serializer.deserialize(signatureData);
    return powSignatureData.recoverable_signature;
    },
    });

    Parameters

    Returns Promise<string[]>

  • Function to recover the public key from hash and signature

    Parameters

    • hash: Uint8Array

      hash sha256

    • signature: Uint8Array

      compact signature

    • compressed: boolean = true

      default true

    Returns string

  • Function to recover the publics keys from a signed transaction or block. The output format can be compressed (default) or uncompressed.

    Example

    const publicKeys = await Signer.recoverPublicKeys(tx);
    

    If the signature data contains more data, like in the blocks for PoW consensus, use the "transformSignature" function to extract the signature.

    Example

     const powDescriptorJson = {
    nested: {
    mypackage: {
    nested: {
    pow_signature_data: {
    fields: {
    nonce: {
    type: "bytes",
    id: 1,
    },
    recoverable_signature: {
    type: "bytes",
    id: 2,
    },
    },
    },
    },
    },
    },
    };

    const serializer = new Serializer(powDescriptorJson, {
    defaultTypeName: "pow_signature_data",
    });

    const publicKeys = await signer.recoverPublicKeys(block, {
    transformSignature: async (signatureData) => {
    const powSignatureData = await serializer.deserialize(signatureData);
    return powSignatureData.recoverable_signature;
    },
    });

    Parameters

    Returns Promise<string[]>

Generated using TypeDoc