Skip to main content
Zondax Github LinkZondax Github Link
Theme SwitchTheme Switch

Wasm API

Wasm api for filecoin signer service.

generateMnemonic

Generate a 24 english words mnemonic.

const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";

const mnemonic = signer_wasm.generateMnemonic();

//
console.log(mnemonic);

keyDerive

Derive a child key from a mnemonic following a BIP44 path.

Arguments :

  • mnemonic: a string containing the words;
  • path: a BIP44 path;
  • password: for encrypted seed if none use an empty string (e.g "")
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";

const mnemonic = "equip will roof matter pink blind book anxiety banner elbow sun young";

const path = "m/44'/461'/0'/0/1";

const keypair = signer_wasm.keyDerive(mnemonic, path, "");

console.log(keypair);

keyDeriveFromSeed

Derive a child key from a seed following a BIP44 path.

Arguments :

  • seed: a seed (hexstring or Buffer);
  • path: a BIP44 path;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";
const bip39 = require('bip39');

const mnemonic = "equip will roof matter pink blind book anxiety banner elbow sun young";

const seed = bip39.mnemonicToSeedSync(mnemonic).toString('hex');

const path = "m/44'/461'/0'/0/1";

const keypair = signer_wasm.keyDeriveFromSeed(seed, "m/44'/461'/0'/0/1");

console.log(keypair);

keyRecover

Recover a extended key from a private key.

Arguments :

  • privateKey: a private key (base64 string or Buffer);
  • testnet: a boolean value. Indicate if you want testnet or mainnet address;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools;

let privateKey = "8VcW07ADswS4BV2cxi5rnIadVsyTDDhY1NfDH19T8Uo=";

const testnet = true;

const keypair = signer_wasm.keyRecover(privateKey, testnet);

console.log(keypair);

transactionSerialize

Serialize a transaction and return a CBOR hexstring.

Arguments :

  • transaction: a filecoin transaction;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";

const transaction = {
"to": "t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
"from": "t1b4zd6ryj5dsnwda5jtjxj6ptkia5e35s52ox7ka",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": 25000,
"method": 0,
"params": ""
};

const cbor_transaction = signer_wasm.transactionSerialize(transaction);

//
console.log(cbor_transaction);

transactionSerializeRaw

Serialize a transaction and return the CBOR equivalent as a Uint8Array.

Arguments :

  • transaction: a filecoin transaction;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";

const transaction = {
"to": "t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
"from": "t1b4zd6ryj5dsnwda5jtjxj6ptkia5e35s52ox7ka",
"nonce": 1,
"value": "100000",
"gasprice": "2500",
"gaslimit": 25000,
"method": 0,
"params": ""
};

const cbor_uint8_array = signer_wasm.transactionSerializeRaw(transaction);

//
console.log(cbor_uint8_array);

transactionParse

Parse a CBOR hextring into a filecoin transaction.

Arguments:

  • cbor_transaction: the cbor (hexstring or Buffer);
  • testnet: boolean value true if testnet or false for mainnet;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";

const cbor_transaction = "885501fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62855010f323f4709e8e4db0c1d4cd374f9f35201d26fb20144000186a0430009c41961a80040";

const testnet = true;

const transaction = signer_wasm.transactionParse(cbor_transaction, testnet);

//
console.log(transaction);

transactionSign

Sign a transaction and return the signature (RSV format).

Arguments:

  • transaction: a filecoin transaction;
  • privatekey: a private key (base64 string or buffer);
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";
const bip32 = require('bip32');

// Use your private key
const MASTER_KEY = "xprv424242424242424242";

let MASTER_NODE = bip32.fromBase58(MASTER_KEY);
const example_key = MASTER_NODE.derivePath("m/44'/461'/0'/0/0");

const signed_tx = signer_wasm.transactionSign(EXAMPLE_TRANSACTION, example_key.privateKey.toString("base64"));

console.log(signed_tx);

transactionSignLotus (support Lotus schema)

Sign a transaction and return a JSON string of the signed transaction which can then be sent to a lotus node.

Arguments:

  • transaction: a filecoin transaction;
  • privatekey: a private key (hexstring or buffer);
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";
const bip32 = require('bip32');

// Use your private key
const MASTER_KEY = "xprv424242424242424242";

let MASTER_NODE = bip32.fromBase58(MASTER_KEY);
const example_key = MASTER_NODE.derivePath("m/44'/461'/0'/0/0");

const signed_tx_json = signer_wasm.transactionSignLotus(EXAMPLE_TRANSACTION, example_key.privateKey.toString("base64"));

console.log(signed_tx_json);

transactionSignRaw

Sign a transaction and return a buffer signature.

Arguments:

  • transaction: a filecoin transaction;
  • privatekey: a private key (base64 string or buffer);
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";
const bip32 = require('bip32');

// Use your private key
const MASTER_KEY = "xprv424242424242424242";

let MASTER_NODE = bip32.fromBase58(MASTER_KEY);
const example_key = MASTER_NODE.derivePath("m/44'/461'/0'/0/0");

const buffer_signature = signer_wasm.transactionSignRaw(EXAMPLE_TRANSACTION, example_key.privateKey.toString("base64"));

console.log(buffer_signature);

verifySignature

Verify a signature.

Arguments :

  • signature: RSV format signature;
  • CBOR transaction: the CBOR transaction;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";


// RSV format signature
// r value [32 bits]
// s value [32 bits]
// v recovering id [1 bit]
const signatureRSV = "541025ca93d7d15508854520549f6a3c1582fbde1a511f21b12dcb3e49e8bdff3eb824cd8236c66b120b45941fd07252908131ffb1dffa003813b9f2bdd0c2f601";

const cbor_transaction = "885501fd1d0f4dfcd7e99afcb99a8326b7dc459d32c62855010f323f4709e8e4db0c1d4cd374f9f35201d26fb20144000186a0430009c41961a80040";


const result = signer_wasm.verifySignature(signatureRSV, cbor_transaction);

// true
console.log(result);

serializeParams

Serialize parameters into cbor data.

Arguments :

  • params: params object;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";

let addresses = ["t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy","t1d2xrzcslx7xlbbylc5c3d5lvandqw4iwl6epxba"];

let constructor_params = { signers: addresses, num_approvals_threshold: 1, unlock_duration: 0 }

let params = {
code_cid: 'fil/1/multisig',
constructor_params: Buffer.from(filecoin_signer.serializeParams(constructor_params)).toString('base64')
}

let serialized_params = filecoin_signer.serializeParams(params);

console.log(serialized_params);

deserializeParams

Deserialize parameters into javascript object given and actor type and a method nuber associated with the parameter.

Arguments :

  • params: base64 cbor encoded parameters;
  • actoType: a string giving the actor type (e.g "multisig");
  • method: method associated with encoded parameters (e.g 7 -> SwapSigners);
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";

let cbor_base64 = "glUB/R0PTfzX6Zr8uZqDJrfcRZ0yxihVAR6vHIpLv+6whwsXRbH1dQNHC3EW"

let params = filecoin_signer.deserializeParams(cbor_base64, "multisig", 7)

console.log(params);

deserializeConstructorParams

Deserialize specificaly constructor parameters into javascript object given the code CID associated with the parameter.

Arguments :

  • params: base64 cbor encoded constructor parameters;
  • codeCID: a string giving the actor type (e.g "paymentchannel");
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";

let cbor_base64 = "glUB/R0PTfzX6Zr8uZqDJrfcRZ0yxihVAR6vHIpLv+6whwsXRbH1dQNHC3EW"

let params = filecoin_signer.deserializeConstructorParams(cbor_base64, "paymentchannel")

console.log(params);

verifyVoucherSignature

Verify a voucher signature.

Arguments :

  • signed voucher: the base64 string representing the signed voucher;
  • signer address: the signer address;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";


const signedVoucher = "i0MA8gcAAED2AAFEAAGGoACAWEIBayRmYQQCatrELBc2rwfu0jJk0EmVr+eVccDsThtM1ZVzkrC53a6qVgrgFkB8OHoiZSlNmW/nmCU7G2POhEeo2gE=";
const signerAddress = "t1d2xrzcslx7xlbbylc5c3d5lvandqw4iwl6epxba";

const result = filecoin_signer.verifyVoucherSignature(signedVoucher, signerAddress);

// true
console.log(result);

getCid

Get the cid hash of a signed message.

Arguments :

  • signed message: json object of the sign message;
const signer_wasm = require('@zondax/filecoin-signing-tools');
// or for browser
// import * as signer_wasm from "@zondax/filecoin-signing-tools";


const signedMessage = {
message: {
to: "t17uoq6tp427uzv7fztkbsnn64iwotfrristwpryy",
from: "t1d2xrzcslx7xlbbylc5c3d5lvandqw4iwl6epxba",
nonce: 1,
value: "100000",
gas_limit: 2500000,
gas_fee_cap: "1",
gas_premium: "1",
method: 0,
params: "",
},
signature: {
type: 1,
data: "0wRrFJZFIVh8m0JD+f5C55YrxD6YAWtCXWYihrPTKdMfgMhYAy86MVhs43hSLXnV+47UReRIe8qFdHRJqFlreAE=",
}
}

const cid = filecoin_signer.getCid(signedMessage)

console.log(cid);