Environment Interface
Initialize a connection instance using the Chain JS SDK to interact with a blockchain node over Transport Layer Security (TLS).
Prerequisites
Before you begin, ensure that you have:
Node.js installed in your environment
The
@alipay/mychainpackage available in your projectThe following certificate and key files in a
./certs/directory:File Description user.pemYour account key. Used to derive the public/private key pair via Chain.utils.getKeyInfo().client.crtClient certificate. Presented to the blockchain node during TLS handshake. ca.crtCertificate Authority (CA) certificate. Used to verify the blockchain node's identity. client.keyClient private key. Must be decrypted with passphraseduring initialization.
Key concepts
`Chain` — the main SDK class imported from @alipay/mychain/index.node. Call it with a configuration object to create a connection instance.
`Chain.utils.getKeyInfo(accountKey, accountPassword)` — extracts the public and private keys from your account key (PEM file). Returns a keyInfo object with .publicKey and .privateKey properties.
Connection instance — the initialized client object (chain) used to submit transactions and query the blockchain node.
Recovery key pair (userRecoverPublicKey / userRecoverPrivateKey) — a secondary key pair used to recover access to your account. If you do not have a separate recovery key, use the same key pair as userPublicKey / userPrivateKey.
Initialize the environment
Step 1: Import the JS SDK
Use the node environment entry point, which enables TLS by default:
const Chain = require('@alipay/mychain/index.node')Step 2: Create a connection instance
The following example creates a connection instance using TLS in a Node.js environment.
The example uses placeholder passwords ("123abc"). Replace these with your actual passwords before running the code. Never commit passwords or private key paths to a code repository.
const Chain = require("@alipay/mychain/index.node") // TLS protocol, node environment
const fs = require("fs")
// Load the account key from the PEM file and extract the key pair
const accountKey = fs.readFileSync("./certs/user.pem", { encoding: "utf8" })
const accountPassword = "123abc" // Replace with your user.pem password
const keyInfo = Chain.utils.getKeyInfo(accountKey, accountPassword)
// Password for the client private key file (client.key)
const passphrase = "123abc" // Replace with your client.key password
// Configuration for the connection instance
let opt = {
host: '127.0.0.1', // IP address of the target blockchain node
port: 18130, // Port number of the blockchain node
timeout: 30000, // Connection timeout in milliseconds
// TLS certificate files
cert: fs.readFileSync("./certs/client.crt", { encoding: "utf8" }), // Client certificate
ca: fs.readFileSync("./certs/ca.crt", { encoding: "utf8" }), // CA certificate
key: fs.readFileSync("./certs/client.key", { encoding: "utf8" }), // Client private key
// Key pair derived from your account key (user.pem)
userPublicKey: keyInfo.publicKey,
userPrivateKey: keyInfo.privateKey,
// Recovery key pair (use the same key pair if you do not have a separate recovery key)
userRecoverPublicKey: keyInfo.publicKey,
userRecoverPrivateKey: keyInfo.privateKey,
passphrase: passphrase // Password for client.key
}
// Initialize the connection instance
const chain = Chain(opt)Replace the following placeholders with your actual values:
| Placeholder | Description | Example |
|---|---|---|
'127.0.0.1' | IP address of your blockchain node | '192.168.1.10' |
18130 | Port number of your blockchain node | 18130 |
"123abc" (accountPassword) | Password to decrypt user.pem | Your actual password |
"123abc" (passphrase) | Password to decrypt client.key | Your actual password |
What's next
After creating the connection instance (chain), use it to submit transactions and query the blockchain network. Refer to the JS SDK developer guide for available operations.