gill
Version:
a modern javascript/typescript client library for interacting with the Solana blockchain
113 lines (109 loc) • 4.86 kB
JavaScript
;
var fs = require('fs');
var os = require('os');
var path = require('path');
var kit = require('@solana/kit');
var assertions = require('@solana/assertions');
// src/node/const.ts
var DEFAULT_CLI_KEYPAIR_PATH = "~/.config/solana/id.json";
async function loadKeypairFromFile(filePath = DEFAULT_CLI_KEYPAIR_PATH) {
const resolvedPath = path.resolve(filePath.startsWith("~") ? filePath.replace("~", os.homedir()) : filePath);
return kit.createKeyPairFromBytes(Uint8Array.from(JSON.parse(fs.readFileSync(resolvedPath, "utf8"))));
}
async function loadKeypairSignerFromFile(filePath = DEFAULT_CLI_KEYPAIR_PATH) {
return kit.createSignerFromKeyPair(await loadKeypairFromFile(filePath));
}
async function loadKeypairFromEnvironment(variableName) {
if (!process.env[variableName]) {
throw new Error(`Environment variable '${variableName}' not set`);
}
return kit.createKeyPairFromBytes(Uint8Array.from(JSON.parse(process.env[variableName])));
}
async function loadKeypairSignerFromEnvironment(variableName) {
return kit.createSignerFromKeyPair(await loadKeypairFromEnvironment(variableName));
}
function assertKeyPairIsExtractable(keyPair) {
assertions.assertKeyExporterIsAvailable();
if (!keyPair.privateKey) {
throw new Error("Keypair is missing private key");
}
if (!keyPair.publicKey) {
throw new Error("Keypair is missing public key");
}
if (!keyPair.privateKey.extractable) {
throw new Error("Private key is not extractable");
}
}
async function extractBytesFromKeyPair(keypair) {
assertKeyPairIsExtractable(keypair);
const [publicKeyBytes, privateKeyJwk] = await Promise.all([
crypto.subtle.exportKey("raw", keypair.publicKey),
crypto.subtle.exportKey("jwk", keypair.privateKey)
]);
if (!privateKeyJwk.d) throw new Error("Failed to get private key bytes");
return new Uint8Array([...Buffer.from(privateKeyJwk.d, "base64"), ...new Uint8Array(publicKeyBytes)]);
}
async function createKeypairFromBase58(punitiveSecretKey) {
return kit.createKeyPairFromBytes(kit.getBase58Encoder().encode(punitiveSecretKey));
}
// src/node/load-keypair-base58.ts
async function loadKeypairFromEnvironmentBase58(variableName) {
if (!process.env[variableName]) {
throw new Error(`Environment variable '${variableName}' not set`);
}
return createKeypairFromBase58(process.env[variableName]);
}
async function loadKeypairSignerFromEnvironmentBase58(variableName) {
return kit.createSignerFromKeyPair(await loadKeypairFromEnvironmentBase58(variableName));
}
async function saveKeypairToFile(keypair, filePath) {
if (!filePath.endsWith(".json")) {
throw new Error("Must provide a json file path to save keypair to");
}
const resolvedPath = path.resolve(filePath.startsWith("~") ? filePath.replace("~", os.homedir()) : filePath);
let bytes;
bytes = await extractBytesFromKeyPair(keypair);
fs.writeFileSync(resolvedPath, "[" + Array.from(bytes).toString() + "]", "utf8");
const [input, output] = await Promise.all([
kit.createKeyPairSignerFromBytes(bytes),
loadKeypairSignerFromFile(resolvedPath)
]);
bytes = null;
return input.address === output.address;
}
async function saveKeypairSignerToFile(keypairSigner, filePath) {
return saveKeypairToFile(keypairSigner.keyPair, filePath);
}
async function saveKeypairToEnvFile(keypair, variableName, envFilePath = ".env") {
if (process.env[variableName]) {
throw new Error(`Environment variable '${variableName}' already exist.`);
}
const resolvedPath = path.resolve(envFilePath.startsWith("~") ? envFilePath.replace("~", os.homedir()) : envFilePath);
let bytes;
bytes = await extractBytesFromKeyPair(keypair);
const signer = await kit.createKeyPairSignerFromBytes(bytes);
fs.appendFileSync(
resolvedPath,
`
# Solana Address: ${signer.address}
${variableName}=[${Array.from(bytes).toString()}]`,
"utf8"
);
bytes = null;
}
async function saveKeypairSignerToEnvFile(keypairSigner, variableName, envFilePath = ".env") {
return saveKeypairToEnvFile(keypairSigner.keyPair, variableName, envFilePath);
}
exports.DEFAULT_CLI_KEYPAIR_PATH = DEFAULT_CLI_KEYPAIR_PATH;
exports.loadKeypairFromEnvironment = loadKeypairFromEnvironment;
exports.loadKeypairFromEnvironmentBase58 = loadKeypairFromEnvironmentBase58;
exports.loadKeypairFromFile = loadKeypairFromFile;
exports.loadKeypairSignerFromEnvironment = loadKeypairSignerFromEnvironment;
exports.loadKeypairSignerFromEnvironmentBase58 = loadKeypairSignerFromEnvironmentBase58;
exports.loadKeypairSignerFromFile = loadKeypairSignerFromFile;
exports.saveKeypairSignerToEnvFile = saveKeypairSignerToEnvFile;
exports.saveKeypairSignerToFile = saveKeypairSignerToFile;
exports.saveKeypairToEnvFile = saveKeypairToEnvFile;
exports.saveKeypairToFile = saveKeypairToFile;
//# sourceMappingURL=index.node.cjs.map
//# sourceMappingURL=index.node.cjs.map