@faast/tron-payments
Version:
Library to assist in processing tron payments, such as deriving addresses and sweeping funds
71 lines • 2.67 kB
JavaScript
import { BaseTronPayments } from './BaseTronPayments';
import { isValidAddress, isValidPrivateKey, privateKeyToAddress } from './helpers';
import { omit } from 'lodash';
import { PUBLIC_CONFIG_OMIT_FIELDS } from './constants';
export class KeyPairTronPayments extends BaseTronPayments {
constructor(config) {
super(config);
this.config = config;
this.addresses = {};
this.privateKeys = {};
this.addressIndices = {};
Object.entries(config.keyPairs).forEach(([iString, addressOrKey]) => {
if (typeof addressOrKey === 'undefined' || addressOrKey === null) {
return;
}
const i = Number.parseInt(iString);
if (isValidAddress(addressOrKey)) {
this.addresses[i] = addressOrKey;
this.privateKeys[i] = null;
this.addressIndices[addressOrKey] = i;
return;
}
if (isValidPrivateKey(addressOrKey)) {
const address = privateKeyToAddress(addressOrKey);
this.addresses[i] = address;
this.privateKeys[i] = addressOrKey;
this.addressIndices[address] = i;
return;
}
throw new Error(`KeyPairTronPaymentsConfig.keyPairs[${i}] is not a valid private key or address`);
});
}
getFullConfig() {
return this.config;
}
getPublicConfig() {
return {
...omit(this.config, PUBLIC_CONFIG_OMIT_FIELDS),
keyPairs: this.addresses,
};
}
getAccountId(index) {
const accountId = this.addresses[index];
if (!accountId) {
throw new Error(`No KeyPairTronPayments account configured at index ${index}`);
}
return accountId;
}
getAccountIds() {
return Object.keys(this.addressIndices);
}
async getPayport(index) {
const address = this.addresses[index];
if (typeof address === 'undefined') {
throw new Error(`Cannot get address ${index} - keyPair[${index}] is undefined`);
}
return { address };
}
async getPrivateKey(index) {
const privateKey = this.privateKeys[index];
if (typeof privateKey === 'undefined') {
throw new Error(`Cannot get private key ${index} - keyPair[${index}] is undefined`);
}
if (privateKey === null) {
throw new Error(`Cannot get private key ${index} - keyPair[${index}] is a public address`);
}
return privateKey;
}
}
export default KeyPairTronPayments;
//# sourceMappingURL=KeyPairTronPayments.js.map