crypto-ducatus-wallet-core
Version:
A multi-currency support library for address derivation, private key creation, and transaction creation
69 lines • 3.24 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var ethers_1 = require("ethers");
var utils = require('web3-utils');
var DUCXTxProvider = (function () {
function DUCXTxProvider() {
}
DUCXTxProvider.prototype.create = function (params) {
switch (params.network) {
case 'testnet':
params.chainId = 0x6772;
break;
case 'livenet':
case 'mainnet':
params.chainId = 0x6773;
break;
}
var recipients = params.recipients, nonce = params.nonce, gasPrice = params.gasPrice, data = params.data, gasLimit = params.gasLimit, _a = params.chainId, chainId = _a === void 0 ? 0x6773 : _a;
var _b = recipients[0], address = _b.address, amount = _b.amount;
var txData = {
nonce: utils.toHex(nonce),
gasLimit: utils.toHex(gasLimit),
gasPrice: utils.toHex(gasPrice),
to: address,
data: data,
value: utils.toHex(Number(amount).toLocaleString('fullwide', { useGrouping: false })),
chainId: chainId
};
console.log('create tx', txData);
return ethers_1.ethers.utils.serializeTransaction(txData);
};
DUCXTxProvider.prototype.getSignatureObject = function (params) {
var tx = params.tx, key = params.key;
var signingKey = new ethers_1.ethers.utils.SigningKey(key.privKey);
var signDigest = signingKey.signDigest.bind(signingKey);
return signDigest(ethers_1.ethers.utils.keccak256(tx));
};
DUCXTxProvider.prototype.getSignature = function (params) {
var signatureHex = ethers_1.ethers.utils.joinSignature(this.getSignatureObject(params));
return signatureHex;
};
DUCXTxProvider.prototype.getHash = function (params) {
var tx = params.tx;
return ethers_1.ethers.utils.parseTransaction(tx).hash;
};
DUCXTxProvider.prototype.applySignature = function (params) {
var tx = params.tx, signature = params.signature;
var parsedTx = ethers_1.ethers.utils.parseTransaction(tx);
var nonce = parsedTx.nonce, gasPrice = parsedTx.gasPrice, gasLimit = parsedTx.gasLimit, to = parsedTx.to, value = parsedTx.value, data = parsedTx.data, chainId = parsedTx.chainId;
var txData = { nonce: nonce, gasPrice: gasPrice, gasLimit: gasLimit, to: to, value: value, data: data, chainId: chainId };
if (typeof signature == 'string') {
signature = ethers_1.ethers.utils.splitSignature(signature);
}
var signedTx = ethers_1.ethers.utils.serializeTransaction(txData, signature);
var parsedTxSigned = ethers_1.ethers.utils.parseTransaction(signedTx);
if (!parsedTxSigned.hash) {
throw new Error('Signature invalid');
}
return signedTx;
};
DUCXTxProvider.prototype.sign = function (params) {
var tx = params.tx, key = params.key;
var signature = this.getSignatureObject({ tx: tx, key: key });
return this.applySignature({ tx: tx, signature: signature });
};
return DUCXTxProvider;
}());
exports.DUCXTxProvider = DUCXTxProvider;
//# sourceMappingURL=index.js.map