Getting Started with the JavaScript Client Library
The Signchain JavaScript Client Library simplifies interaction with the Signchain API for secure blockchain transactions and wallet management. This guide will walk you through the steps to set up and make your first API calls using the library in a Node.js environment.
Prerequisites
To follow along, ensure you have:
- Node.js: Ensure you have Node.js installed (version 12 or higher recommended).
- Signchain Account: You need an API Key and Vault ID from your Signchain Dashboard.
- Basic JavaScript Knowledge: Familiarity with JavaScript and asynchronous programming is helpful.
Step 1: Install the Library
First, install the @grexie/signchain
package using npm or yarn:
npm install @grexie/signchain
or
yarn add @grexie/signchain
Step 2: Initialize the Client
To interact with the Signchain API, initialize the SignchainClient
with your API Key and Vault ID. This allows your server to securely access Signchain’s services.
Example
const { SignchainClient } = require('@grexie/signchain');
const client = new SignchainClient({
apiKey: 'your-api-key',
vaultId: 'your-vault-id',
});
Replace your-api-key
and your-vault-id
with the values from your Signchain Dashboard.
Step 3: Create Your First Wallet
Now that the client is initialized, let’s create a wallet. This example demonstrates how to create a wallet within your vault, a typical first step in integrating with Signchain.
Example
async function createWallet() {
try {
const wallet = await client.createWallet({ name: 'My Wallet' });
console.log('Wallet Created:', wallet);
} catch (error) {
console.error('Error creating wallet:', error.message);
}
}
createWallet();
This code initializes a wallet named “My Wallet” in your vault. The response includes information like the wallet’s unique ID, address, and metadata.
Step 4: Sign a Transaction
Signing transactions securely is one of the core functionalities of Signchain. The sign
method allows you to sign transactions using the private keys managed within your vault, ensuring that the keys are kept secure.
Example
const contractABI = [
// Your contract ABI here
];
async function signTransaction() {
try {
const options = {
chain: 'ethereum', // or 'bsc', 'polygon', etc.
contract: '0xYourContractAddress',
sender: '0xYourSenderAddress',
abi: contractABI,
functionName: 'myFunction',
args: ['param1', 'param2'],
};
const { submissionHash, args } = await client.sign(options);
console.log('Transaction Signed, Submission Hash:', submissionHash);
return args;
} catch (error) {
console.error('Error signing transaction:', error.message);
}
}
signTransaction();
In this example:
- Replace
0xYourContractAddress
and0xYourSenderAddress
with the actual contract and sender addresses. - Provide the ABI of your contract and the function you want to call, along with its arguments in
args
.
Explanation of submissionHash
and args
submissionHash
: This is a unique identifier that correlates the signed transaction with the transaction generated and recorded in your Signchain Dashboard. It helps track and reference the transaction in the dashboard.args
: Theargs
array returned by thesign
method contains the transaction data that should be sent back to the user for final execution. This data can be used with libraries likewagmi
,viem
, orethers.js
to submit the transaction on-chain.
Step 5: Execute the Transaction
To execute the transaction on the blockchain, send the args
data to the user or backend, where you can use a library such as wagmi
or ethers.js
to submit it.
Example with Wagmi
import { useWriteContract, useAccount } from 'wagmi';
const MyComponent = (props) => {
const { writeContractAsync } = useWriteContract();
const account = useAccount();
const sendTransaction = async (args) => {
const tx = await writeContractAsync({
abi: YourContractABI,
address: `your_contract_address`,
account: account.address,
functionName: `your_contract_function`,
args: [...args],
});
console.log('Transaction Executed:', tx);
};
// ...
}
In this example:
- Use the
args
data provided by Signchain to execute the transaction with a connected wallet. - The final execution of the transaction can be handled by
wagmi
,viem
, orethers.js
, depending on your setup.
Additional Resources
- API Reference: Detailed information about each endpoint and parameter.
- Usage Examples: More advanced examples and use cases to get the most out of the Signchain JavaScript Client Library.
- Security Best Practices: Recommendations for secure usage of the Signchain library.
Summary
With these basics, you’re ready to start integrating the Signchain JavaScript Client Library into your application. From creating wallets to signing and executing transactions, this client library offers a robust and secure way to manage blockchain interactions within your JavaScript environment. Explore more advanced usage and configurations in our Usage Examples section!