@ledgerhq/coin-tezos
Version:
88 lines • 3.54 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSignOperation = void 0;
exports.getOperationContents = getOperationContents;
const rxjs_1 = require("rxjs");
const errors_1 = require("@ledgerhq/errors");
const buildOptimisticOperation_1 = require("./buildOptimisticOperation");
const logic_1 = require("../logic");
const tezosToolkit_1 = require("../logic/tezosToolkit");
// Exported for test purpose only
async function getOperationContents({ account, transaction, counter, public_key, public_key_hash, }) {
let publicKey = undefined;
if (!account.tezosResources.revealed) {
publicKey = {
publicKey: public_key,
publicKeyHash: public_key_hash,
};
}
return (0, logic_1.craftTransaction)({
address: account.freshAddress,
counter,
}, {
type: transaction.mode,
recipient: transaction.recipient,
amount: BigInt(transaction.amount.toString()),
fee: {
fees: (transaction.fees || 0).toString(),
gasLimit: (transaction.gasLimit || 0).toString(),
storageLimit: (transaction.storageLimit || 0).toString(),
},
}, publicKey);
}
const buildSignOperation = (signerContext) => ({ account, deviceId, transaction }) => new rxjs_1.Observable(o => {
let cancelled = false;
async function main() {
const { fees } = transaction;
if (!fees)
throw new errors_1.FeeNotLoaded();
const { freshAddressPath, freshAddress } = account;
const signedInfo = await signerContext(deviceId, async (signer) => {
const ledgerSigner = signer.createLedgerSigner(freshAddressPath, false, 0);
const tezosToolkit = (0, tezosToolkit_1.getTezosToolkit)();
tezosToolkit.setProvider({ signer: ledgerSigner });
const publicKey = await ledgerSigner.publicKey();
const publicKeyHash = await ledgerSigner.publicKeyHash();
const sourceData = await tezosToolkit.rpc.getContract(freshAddress);
o.next({ type: "device-signature-requested" });
if (cancelled) {
return;
}
const { type, contents } = await getOperationContents({
account,
transaction,
counter: Number(sourceData.counter),
public_key: publicKey,
public_key_hash: publicKeyHash,
});
const forgedBytes = await (0, logic_1.rawEncode)(contents);
const signature = await ledgerSigner.sign(forgedBytes);
return {
type,
signature,
};
});
if (!signedInfo) {
return;
}
o.next({ type: "device-signature-granted" });
const { type, signature } = signedInfo;
const operation = (0, buildOptimisticOperation_1.buildOptimisticOperation)(account, transaction, type);
o.next({
type: "signed",
signedOperation: {
operation,
// we slice the signature to remove the `03` prefix
// which souldn't be included in the signature
signature: signature.sbytes.slice(2),
},
});
}
main().then(() => o.complete(), e => o.error(e));
return () => {
cancelled = true;
};
});
exports.buildSignOperation = buildSignOperation;
exports.default = exports.buildSignOperation;
//# sourceMappingURL=signOperation.js.map