UNPKG

@ledgerhq/coin-casper

Version:
49 lines 2.24 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildSignOperation = void 0; const rxjs_1 = require("rxjs"); const logs_1 = require("@ledgerhq/logs"); const addresses_1 = require("../bridge/bridgeHelpers/addresses"); const txn_1 = require("../bridge/bridgeHelpers/txn"); const buildOptimisticOperation_1 = require("./buildOptimisticOperation"); const casper_js_sdk_1 = require("casper-js-sdk"); const buildSignOperation = (signerContext) => ({ account, transaction, deviceId }) => new rxjs_1.Observable(o => { async function main() { // log("debug", "[signOperation] start fn"); const { recipient, amount, fees, transferId } = transaction; const { address, derivationPath } = (0, addresses_1.getAddress)(account); const casperTx = await (0, txn_1.createNewTransaction)(address, recipient, amount, fees, transferId); // Serialize tx const txBytes = casperTx.toBytes(); (0, logs_1.log)("debug", `[signOperation] serialized transaction: [${txBytes.toString()}]`); o.next({ type: "device-signature-requested", }); // Sign by device const { r } = await signerContext(deviceId, async (signer) => { const r = await signer.sign(derivationPath, Buffer.from(txBytes)); return { r }; }); o.next({ type: "device-signature-granted", }); // signature verification const txHash = casperTx.hash.getHash()?.toHex() ?? ""; const signature = Buffer.concat([Buffer.from([casper_js_sdk_1.KeyAlgorithm.SECP256K1]), r.signatureRS]); const operation = (0, buildOptimisticOperation_1.buildOptimisticOperation)(account, transaction, txHash); const txJson = casperTx.toJSON(); o.next({ type: "signed", signedOperation: { operation, signature: Buffer.from(signature).toString("hex"), rawData: { tx: JSON.stringify(txJson), }, }, }); } main().then(() => o.complete(), e => o.error(e)); }); exports.buildSignOperation = buildSignOperation; //# sourceMappingURL=signOperation.js.map