@faast/ethereum-payments
Version:
Library to assist in processing ethereum payments, such as deriving addresses and sweeping funds
75 lines • 2.69 kB
JavaScript
import { omit } from 'lodash';
import { PUBLIC_CONFIG_OMIT_FIELDS } from '../constants';
import { BaseErc20Payments } from './BaseErc20Payments';
import { deriveSignatory } from '../bip44';
import { deriveAddress } from './deriveAddress';
export class HdErc20Payments extends BaseErc20Payments {
constructor(config) {
super(config);
try {
this.xprv = '';
this.xpub = '';
if (this.isValidXpub(config.hdKey)) {
this.xpub = config.hdKey;
}
else if (this.isValidXprv(config.hdKey)) {
this.xprv = config.hdKey;
this.xpub = deriveSignatory(config.hdKey, 0).xkeys.xpub;
}
}
catch (e) {
throw new Error(`Account must be a valid xprv or xpub: ${e.message}`);
}
}
static generateNewKeys() {
return deriveSignatory();
}
getXpub() {
return this.xpub;
}
getPublicConfig() {
return {
...omit(this.getFullConfig(), PUBLIC_CONFIG_OMIT_FIELDS),
tokenAddress: this.tokenAddress.toLowerCase(),
depositKeyIndex: this.depositKeyIndex,
hdKey: this.getXpub(),
};
}
getAccountId(index) {
return this.getXpub();
}
getAccountIds() {
return [this.getXpub()];
}
getAddressSalt(index) {
const key = deriveSignatory(this.getXpub(), index).keys.pub;
const salt = this.web3.utils.sha3(`0x${key}`);
if (!salt) {
throw new Error(`Cannot get address salt for index ${index}`);
}
return salt;
}
async getPayport(index) {
const signatory = deriveSignatory(this.getXpub(), index);
if (index === 0) {
return { address: signatory.address };
}
if (!this.masterAddress) {
throw new Error(`Cannot derive payport ${index} - masterAddress is falsy`);
}
const address = deriveAddress(this.masterAddress, signatory.keys.pub);
if (!this.isValidAddress(address)) {
throw new Error(`Cannot get address ${index} - validation failed for derived address`);
}
const { address: signerAddress } = deriveSignatory(this.getXpub(), this.depositKeyIndex);
return { address, signerAddress };
}
async getPrivateKey(index) {
if (!this.xprv) {
throw new Error(`Cannot get private key ${index} - HdEthereumPayments was created with an xpub`);
}
return deriveSignatory(deriveSignatory(this.xprv, 0).xkeys.xprv, index).keys.prv;
}
}
export default HdErc20Payments;
//# sourceMappingURL=HdErc20Payments.js.map