You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

signer.js 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.multisign = exports.verifySignature = exports.authorizeChannel = void 0;
  4. const bignumber_js_1 = require("bignumber.js");
  5. const lodash_1 = require("lodash");
  6. const ripple_address_codec_1 = require("ripple-address-codec");
  7. const ripple_binary_codec_1 = require("ripple-binary-codec");
  8. const ripple_keypairs_1 = require("ripple-keypairs");
  9. const errors_1 = require("../errors");
  10. const transactions_1 = require("../models/transactions");
  11. function multisign(transactions) {
  12. if (transactions.length === 0) {
  13. throw new errors_1.ValidationError('There were 0 transactions to multisign');
  14. }
  15. transactions.forEach((txOrBlob) => {
  16. const tx = getDecodedTransaction(txOrBlob);
  17. (0, transactions_1.validate)(tx);
  18. if (tx.Signers == null || tx.Signers.length === 0) {
  19. throw new errors_1.ValidationError("For multisigning all transactions must include a Signers field containing an array of signatures. You may have forgotten to pass the 'forMultisign' parameter when signing.");
  20. }
  21. if (tx.SigningPubKey !== '') {
  22. throw new errors_1.ValidationError('SigningPubKey must be an empty string for all transactions when multisigning.');
  23. }
  24. });
  25. const decodedTransactions = transactions.map((txOrBlob) => {
  26. return getDecodedTransaction(txOrBlob);
  27. });
  28. validateTransactionEquivalence(decodedTransactions);
  29. return (0, ripple_binary_codec_1.encode)(getTransactionWithAllSigners(decodedTransactions));
  30. }
  31. exports.multisign = multisign;
  32. function authorizeChannel(wallet, channelId, amount) {
  33. const signingData = (0, ripple_binary_codec_1.encodeForSigningClaim)({
  34. channel: channelId,
  35. amount,
  36. });
  37. return (0, ripple_keypairs_1.sign)(signingData, wallet.privateKey);
  38. }
  39. exports.authorizeChannel = authorizeChannel;
  40. function verifySignature(tx) {
  41. const decodedTx = getDecodedTransaction(tx);
  42. return (0, ripple_keypairs_1.verify)((0, ripple_binary_codec_1.encodeForSigning)(decodedTx), decodedTx.TxnSignature, decodedTx.SigningPubKey);
  43. }
  44. exports.verifySignature = verifySignature;
  45. function validateTransactionEquivalence(transactions) {
  46. const exampleTransaction = JSON.stringify(Object.assign(Object.assign({}, transactions[0]), { Signers: null }));
  47. if (transactions
  48. .slice(1)
  49. .some((tx) => JSON.stringify(Object.assign(Object.assign({}, tx), { Signers: null })) !== exampleTransaction)) {
  50. throw new errors_1.ValidationError('txJSON is not the same for all signedTransactions');
  51. }
  52. }
  53. function getTransactionWithAllSigners(transactions) {
  54. const sortedSigners = (0, lodash_1.flatMap)(transactions, (tx) => { var _a; return (_a = tx.Signers) !== null && _a !== void 0 ? _a : []; }).sort(compareSigners);
  55. return Object.assign(Object.assign({}, transactions[0]), { Signers: sortedSigners });
  56. }
  57. function compareSigners(left, right) {
  58. return addressToBigNumber(left.Signer.Account).comparedTo(addressToBigNumber(right.Signer.Account));
  59. }
  60. function addressToBigNumber(address) {
  61. const hex = Buffer.from((0, ripple_address_codec_1.decodeAccountID)(address)).toString('hex');
  62. const numberOfBitsInHex = 16;
  63. return new bignumber_js_1.BigNumber(hex, numberOfBitsInHex);
  64. }
  65. function getDecodedTransaction(txOrBlob) {
  66. if (typeof txOrBlob === 'object') {
  67. return (0, ripple_binary_codec_1.decode)((0, ripple_binary_codec_1.encode)(txOrBlob));
  68. }
  69. return (0, ripple_binary_codec_1.decode)(txOrBlob);
  70. }
  71. //# sourceMappingURL=signer.js.map