stellar-plus
Version:
beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain
174 lines (173 loc) • 12 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const sign_transaction_1 = require("../../../../stellar-plus/core/pipelines/sign-transaction");
const network_1 = require("../../../../stellar-plus/network");
const transaction_mock_1 = require("../../../../stellar-plus/test/mocks/transaction-mock");
const MOCKED_KEYPAIRS = [
stellar_sdk_1.Keypair.fromSecret('SAO45YQLDI4LIEPP2HXYVX72XBKEN4OBWYKR3P6AOS7EMOLJCJX5IF5A'),
stellar_sdk_1.Keypair.fromSecret('SA2WW3DO6AVJQO5V4MU64DSDL34FRXVIQXIUMKS7JMAENCCI3ORMQVLA'),
stellar_sdk_1.Keypair.fromSecret('SCHH7OAC6MC4NF3TG2JML56WJT5U7ZE355USOKGXZCQ2FCJZEX62OEKR'),
];
const TESTNET_PASSPHRASE = (0, network_1.TestNet)().networkPassphrase;
const MOCKED_UNSGINED_TRANSACTION_XDR = 'AAAAAgAAAAC8CrO4sEcs28O8U8KWvl4CpiGpCgRlbEwf2fp21SRe0gAAAGQADg/kAAAAAQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA';
const MOCKED_SIGNED_TRANSACTION_XDR = 'AAAAAgAAAAC8CrO4sEcs28O8U8KWvl4CpiGpCgRlbEwf2fp21SRe0gAAAGQADg/kAAAAAQAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAEAAAAAAAAABQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAB1SRe0gAAAEBH30tw2MS4pbpLZ8RbEBTLxF2xalFGJRLfDhgcbildNTgujl6hbNxmw2qltop/SZfe34R4q9v+KVhp5pQ4DmkL';
const MOCKED_UNSIGNED_TRANSACTION = stellar_sdk_1.TransactionBuilder.fromXDR(MOCKED_UNSGINED_TRANSACTION_XDR, TESTNET_PASSPHRASE);
const MOCKED_SIGNED_TRANSACTION = stellar_sdk_1.TransactionBuilder.fromXDR(MOCKED_SIGNED_TRANSACTION_XDR, TESTNET_PASSPHRASE);
const MOCKED_SIGNATURE_REQUIREMENTS = [
{
publicKey: MOCKED_KEYPAIRS[0].publicKey(),
thresholdLevel: 1,
},
];
const MOCKED_SIGNER = (0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[0].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
});
const MOCKED_SIGNERS = [MOCKED_SIGNER];
const MOCKED_ST_INPUT = {
transaction: MOCKED_UNSIGNED_TRANSACTION,
signatureRequirements: MOCKED_SIGNATURE_REQUIREMENTS,
signers: MOCKED_SIGNERS,
};
const MOCKED_ST_OUTPUT = MOCKED_SIGNED_TRANSACTION;
describe('SignTransactionPipeline', () => {
let signTransactionPipeline;
beforeEach(() => {
jest.clearAllMocks();
});
describe('Sign Transaction', () => {
beforeEach(() => {
signTransactionPipeline = new sign_transaction_1.SignTransactionPipeline();
jest.clearAllMocks();
});
it('should sign transaction successfully', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
jest.spyOn(MOCKED_SIGNER, 'sign').mockResolvedValueOnce(MOCKED_SIGNED_TRANSACTION_XDR);
yield expect(signTransactionPipeline.execute(MOCKED_ST_INPUT)).resolves.toEqual(MOCKED_ST_OUTPUT);
expect(MOCKED_SIGNER.sign).toHaveBeenCalledWith(MOCKED_UNSIGNED_TRANSACTION);
expect(MOCKED_SIGNER.getPublicKey).toHaveBeenCalledTimes(1);
}));
it('should sign transaction successfully with multiple signers', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const mockedSigners = MOCKED_SIGNERS.concat((0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[1].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}), (0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[2].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}));
const mockedInput = Object.assign(Object.assign({}, MOCKED_ST_INPUT), { signers: mockedSigners });
jest.spyOn(MOCKED_SIGNER, 'sign').mockResolvedValueOnce(MOCKED_SIGNED_TRANSACTION_XDR);
yield expect(signTransactionPipeline.execute(mockedInput)).resolves.toEqual(MOCKED_ST_OUTPUT);
expect(MOCKED_SIGNER.sign).toHaveBeenCalledWith(MOCKED_UNSIGNED_TRANSACTION);
expect(MOCKED_SIGNER.getPublicKey).toHaveBeenCalledTimes(1);
expect(mockedSigners[1].sign).not.toHaveBeenCalledOnce();
expect(mockedSigners[2].sign).not.toHaveBeenCalledOnce();
}));
it('should sign transaction successfully with multiple signers and multiple requirements', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const mockedSigners = MOCKED_SIGNERS.concat((0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[1].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}), (0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[2].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}));
const mockedSignatureRequirements = MOCKED_SIGNATURE_REQUIREMENTS.concat({
publicKey: mockedSigners[1].getPublicKey(),
thresholdLevel: 1,
}, {
publicKey: mockedSigners[2].getPublicKey(),
thresholdLevel: 1,
});
const mockedInput = Object.assign(Object.assign({}, MOCKED_ST_INPUT), { signatureRequirements: mockedSignatureRequirements, signers: mockedSigners });
jest.spyOn(MOCKED_SIGNER, 'sign').mockResolvedValueOnce(MOCKED_SIGNED_TRANSACTION_XDR);
yield expect(signTransactionPipeline.execute(mockedInput)).resolves.toEqual(MOCKED_ST_OUTPUT);
expect(mockedSigners[0].sign).toHaveBeenCalledWith(MOCKED_UNSIGNED_TRANSACTION);
expect(mockedSigners[1].sign).toHaveBeenCalledWith(MOCKED_SIGNED_TRANSACTION);
expect(mockedSigners[2].sign).toHaveBeenCalledWith(MOCKED_SIGNED_TRANSACTION);
}));
it('should sign transaction successfully with same signer multiple times', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const mockedSigners = MOCKED_SIGNERS.concat((0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[0].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}), (0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[0].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}), (0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[0].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}));
const mockedInput = Object.assign(Object.assign({}, MOCKED_ST_INPUT), { signers: mockedSigners });
jest.spyOn(MOCKED_SIGNER, 'sign').mockResolvedValueOnce(MOCKED_SIGNED_TRANSACTION_XDR);
yield expect(signTransactionPipeline.execute(mockedInput)).resolves.toEqual(MOCKED_ST_OUTPUT);
expect(mockedSigners[0].sign).toHaveBeenCalledWith(MOCKED_UNSIGNED_TRANSACTION);
expect(mockedSigners[1].sign).not.toHaveBeenCalled();
expect(mockedSigners[2].sign).not.toHaveBeenCalled();
}));
it('should throw error if try to sign with same signer and requirements multiple times', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const mockedSigners = MOCKED_SIGNERS.concat((0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[0].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}), (0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[0].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}), (0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[0].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
}));
const mockedSignatureRequirements = MOCKED_SIGNATURE_REQUIREMENTS.concat({
publicKey: mockedSigners[0].getPublicKey(),
thresholdLevel: 1,
}, {
publicKey: mockedSigners[0].getPublicKey(),
thresholdLevel: 1,
}, {
publicKey: mockedSigners[0].getPublicKey(),
thresholdLevel: 1,
});
const mockedInput = Object.assign(Object.assign({}, MOCKED_ST_INPUT), { signers: mockedSigners, signatureRequirements: mockedSignatureRequirements });
jest.spyOn(MOCKED_SIGNER, 'sign').mockRejectedValueOnce('Error signing transaction');
yield expect(signTransactionPipeline.execute(mockedInput)).rejects.toThrow('Failed to sign transaction!');
expect(mockedSigners[0].sign).toHaveBeenCalledWith(MOCKED_UNSIGNED_TRANSACTION);
expect(mockedSigners[1].sign).not.toHaveBeenCalled();
expect(mockedSigners[2].sign).not.toHaveBeenCalled();
}));
it('should throw error if no signature requirements received', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const mockedInput = Object.assign(Object.assign({}, MOCKED_ST_INPUT), { signatureRequirements: [] });
yield expect(signTransactionPipeline.execute(mockedInput)).rejects.toThrow('No signature requirements provided!');
}));
it('should throw error if no signers received', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const mockedInput = Object.assign(Object.assign({}, MOCKED_ST_INPUT), { signers: [] });
yield expect(signTransactionPipeline.execute(mockedInput)).rejects.toThrow('No signers provided!');
}));
it('should throw error if signature requirements does not contain signer public key', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const mockedInput = Object.assign(Object.assign({}, MOCKED_ST_INPUT), { signers: [(0, transaction_mock_1.mockAccountHandler)({ accountKey: 'anotherPublicKey' })] });
yield expect(signTransactionPipeline.execute(mockedInput)).rejects.toThrow('The signer was not found!');
}));
it('should throw error if XDR convertion fails', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
jest.spyOn(MOCKED_SIGNER, 'sign').mockResolvedValueOnce('invalidXDR');
yield expect(signTransactionPipeline.execute(MOCKED_ST_INPUT)).rejects.toThrow('Failed to sign transaction!');
expect(MOCKED_SIGNER.sign).toHaveBeenCalledTimes(1);
expect(MOCKED_SIGNER.getPublicKey).toHaveBeenCalledTimes(2);
}));
it('should throw error if transaction sign fails', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
;
MOCKED_SIGNER.sign.mockImplementationOnce(() => {
throw new Error('Error signing transaction');
});
yield expect(signTransactionPipeline.execute(MOCKED_ST_INPUT)).rejects.toThrow('Failed to sign transaction!');
expect(MOCKED_SIGNER.sign).toHaveBeenCalledTimes(1);
expect(MOCKED_SIGNER.getPublicKey).toHaveBeenCalledTimes(2);
}));
it('should throw error if signer contains signatureSchema', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const signatureSchema = (0, transaction_mock_1.mockSignatureSchema)();
const mockedSigner = (0, transaction_mock_1.mockAccountHandler)({
accountKey: MOCKED_KEYPAIRS[0].publicKey(),
outputSignedTransaction: MOCKED_SIGNED_TRANSACTION_XDR,
signatureSchema: signatureSchema,
});
const mockedInput = Object.assign(Object.assign({}, MOCKED_ST_INPUT), { signers: [mockedSigner] });
yield expect(signTransactionPipeline.execute(mockedInput)).rejects.toThrow('Multisignature support not implemented yet');
}));
});
});