@kadena/kadena-cli
Version:
Kadena CLI tool to interact with the Kadena blockchain (manage keys, transactions, etc.)
62 lines • 2.31 kB
JavaScript
import { kadenaEncrypt, kadenaKeyPairsFromRandom } from '@kadena/hd-wallet';
import { kadenaGenKeypair } from '@kadena/hd-wallet/chainweaver';
import { randomBytes } from 'node:crypto';
import { notEmpty } from '../../utils/globalHelpers.js';
export class PlainKeyService {
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/parameter-properties
constructor(services) {
this.services = services;
}
async storeKeyPairs(keyPairs, alias) {
const warnings = [];
const results = await Promise.all(keyPairs.map(async (key, index) => {
var _a;
const _alias = keyPairs.length > 1 ? `${alias}-${index}` : alias;
const store = {
alias: _alias,
legacy: (_a = key.legacy) !== null && _a !== void 0 ? _a : false,
publicKey: key.publicKey,
secretKey: key.secretKey,
};
try {
const filepath = await this.services.config.setPlainKey(store);
return { ...store, filepath };
}
catch (error) {
if (error instanceof Error) {
warnings.push(error.message);
return null;
}
throw error;
}
}));
return { keys: results.filter(notEmpty), warnings };
}
async _generateLegacyKeys(amount) {
const keyPairs = [];
const password = '';
const rootKey = await kadenaEncrypt(password, randomBytes(128));
for (let i = 0; i < amount; i++) {
const { publicKey, secretKey } = await kadenaGenKeypair(password, rootKey, i);
keyPairs.push({
publicKey: publicKey,
secretKey: secretKey,
legacy: true,
});
}
return keyPairs;
}
async generateKeyPairs(amount, legacy) {
if (legacy === true) {
return this._generateLegacyKeys(amount);
}
return kadenaKeyPairsFromRandom(amount).map((keyPair) => ({
...keyPair,
legacy: false,
}));
}
list(directory) {
return this.services.config.getPlainKeys(directory);
}
}
//# sourceMappingURL=plainkey.service.js.map