stellar-plus
Version:
beta version of stellar-plus, an all-in-one sdk for the Stellar blockchain
147 lines (146 loc) • 8.76 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
/* eslint-disable @typescript-eslint/no-explicit-any */
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
const stellar_sdk_1 = require("@stellar/stellar-sdk");
const default_1 = require("../../../../stellar-plus/account/account-handler/default");
const errors_1 = require("../../../../stellar-plus/account/account-handler/default/errors");
const network_1 = require("../../../../stellar-plus/network");
jest.mock('@stellar/stellar-sdk', () => {
// The mock doesnt spread the whole originalModule because some internal exported objects cause failures
// so we just unmock the necessary items.
// uncomment and use the following line if you need to check the contents of the module:
// const originalModule: typeof import('@stellar/stellar-sdk') = jest.requireActual('@stellar/stellar-sdk')
const originalModule = jest.requireActual('@stellar/stellar-sdk');
return {
Horizon: originalModule.Horizon,
Keypair: originalModule.Keypair,
Transaction: originalModule.Transaction,
FeeBumpTransaction: originalModule.FeeBumpTransaction,
xdr: originalModule.xdr,
authorizeEntry: jest.fn(),
};
});
const MOCKED_AUTHORIZE_ENTRY = stellar_sdk_1.authorizeEntry;
const MOCKED_SOROBAN_AUTH_ENTRY = {
credentials: jest.fn(),
rootInvocation: jest.fn(),
toXDR: jest.fn(),
};
const TESTNET_CONFIG = (0, network_1.TestNet)();
describe('DefaultAccountHandler', () => {
describe('Initialization', () => {
it('should initialize with just the networkConfig and generate a keypair', () => {
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG });
const spySecretKey = jest.mocked(dah.secretKey);
expect(spySecretKey).toBeDefined();
expect(dah.getPublicKey()).toBe(stellar_sdk_1.Keypair.fromSecret(spySecretKey).publicKey());
});
it('should initialize with a secret key', () => {
const secretKey = stellar_sdk_1.Keypair.random().secret();
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey });
expect(dah.getPublicKey()).toBe(stellar_sdk_1.Keypair.fromSecret(secretKey).publicKey());
});
});
describe('Core features', () => {
it('should sign a transaction with its secret key', () => {
const keypair = stellar_sdk_1.Keypair.random();
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
const mockedXdrResult = 'Mocked XDR Result';
const mockedTx = {
sign: jest.fn().mockReturnValue('Signed'),
toXDR: jest.fn().mockReturnValue(mockedXdrResult),
};
const spySign = jest.spyOn(mockedTx, 'sign');
const signedTx = dah.sign(mockedTx);
expect(signedTx).toBe(mockedXdrResult);
expect(spySign).toHaveBeenCalledExactlyOnceWith(keypair);
});
it('should sign a fee bummp transaction with its secret key', () => {
const keypair = stellar_sdk_1.Keypair.random();
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
const mockedXdrResult = 'Mocked XDR Result';
const mockedTx = {
sign: jest.fn().mockReturnValue('Signed'),
toXDR: jest.fn().mockReturnValue(mockedXdrResult),
};
const spySign = jest.spyOn(mockedTx, 'sign');
const signedTx = dah.sign(mockedTx);
expect(signedTx).toBe(mockedXdrResult);
expect(spySign).toHaveBeenCalledExactlyOnceWith(keypair);
});
it('should sign a soroban authorization entry with its secret key', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const keypair = stellar_sdk_1.Keypair.random();
MOCKED_AUTHORIZE_ENTRY.mockImplementationOnce(() => MOCKED_SOROBAN_AUTH_ENTRY);
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
const signedEntry = yield dah.signSorobanAuthEntry(MOCKED_SOROBAN_AUTH_ENTRY, 123, TESTNET_CONFIG.networkPassphrase);
expect(signedEntry).toBe(MOCKED_SOROBAN_AUTH_ENTRY);
expect(MOCKED_AUTHORIZE_ENTRY).toHaveBeenCalledExactlyOnceWith(MOCKED_SOROBAN_AUTH_ENTRY, keypair, 123, TESTNET_CONFIG.networkPassphrase);
}));
it('should sign data with its secret key', () => {
const keypair = stellar_sdk_1.Keypair.random();
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
const data = Buffer.from('Mocked Data');
const signature = dah.signData(data);
expect(dah.verifySignature(data, signature)).toBe(true);
});
});
describe('Getters', () => {
it('should return the public key of the account', () => {
const keypair = stellar_sdk_1.Keypair.random();
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
expect(dah.getPublicKey()).toBe(keypair.publicKey());
});
it('should return the secret key of the account', () => {
const keypair = stellar_sdk_1.Keypair.random();
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
expect(dah.getSecretKey()).toBe(keypair.secret());
});
});
describe('Error Handling', () => {
beforeEach(() => {
jest.clearAllMocks();
});
it('should throw an error if the secret key provided in the constructor is invalid', () => {
const invalidSecretKey = 'Mocked Secret Key';
expect(() => {
new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: invalidSecretKey });
}).toThrow(errors_1.DAHError.failedToLoadSecretKeyError(new Error('Mocked error')));
});
it('should throw an error if the public key cannot be derived from the current secret key', () => {
const invalidSecret = 'Mocked Secret Key';
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG });
jest.replaceProperty(dah, 'secretKey', invalidSecret);
expect(dah.getPublicKey).toThrow(errors_1.DAHError.failedToLoadSecretKeyError(new Error('Mocked error')));
});
it('should throw an error if the transaction cannot be signed', () => {
const keypair = stellar_sdk_1.Keypair.random();
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
const mockedTx = {
sign: jest.fn().mockImplementationOnce(() => {
throw new Error('Mocked error');
}),
toXDR: jest.fn(),
};
const spySign = jest.spyOn(mockedTx, 'sign');
expect(() => {
dah.sign(mockedTx);
}).toThrow(errors_1.DAHError.failedToSignTransactionError(new Error('Mocked error')));
expect(spySign).toHaveBeenCalledExactlyOnceWith(keypair);
});
it('should throw an error if the authorizeEntry cannot be signed', () => tslib_1.__awaiter(void 0, void 0, void 0, function* () {
const keypair = stellar_sdk_1.Keypair.random();
MOCKED_AUTHORIZE_ENTRY.mockImplementationOnce(() => {
throw new Error('Mocked error');
});
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
yield expect(dah.signSorobanAuthEntry(MOCKED_SOROBAN_AUTH_ENTRY, 123, TESTNET_CONFIG.networkPassphrase)).rejects.toThrow(errors_1.DAHError.failedToSignAuthorizationEntryError(new Error('Mocked error'), 'mocked auth entry xdr', 123, TESTNET_CONFIG.networkPassphrase));
}));
it('should throw an error if data cannot be signed', () => {
const keypair = stellar_sdk_1.Keypair.random();
const dah = new default_1.DefaultAccountHandlerClient({ networkConfig: TESTNET_CONFIG, secretKey: keypair.secret() });
expect(() => dah.signData(null)).toThrow(errors_1.DAHError.failedToSignDataError(new Error('Mocked error')));
});
});
});