@ledgerhq/coin-casper
Version:
Ledger Casper integration
395 lines • 15.1 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createMockAccountShapeData = exports.createMockSignedOperation = exports.createMockOperationSet = exports.createMockTransactionSet = exports.createMockAccountSet = exports.createMockOperation = exports.createMockTransaction = exports.createMockAccount = exports.TEST_TRANSFER_IDS = exports.TEST_TRANSACTION_HASHES = exports.TEST_ADDRESSES = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const fee_1 = require("../bridge/bridgeHelpers/fee");
const operation_1 = require("@ledgerhq/coin-framework/operation");
const cryptoassets_1 = require("@ledgerhq/cryptoassets");
const consts_1 = require("../consts");
const index_1 = require("@ledgerhq/coin-framework/account/index");
/**
* Sample Casper addresses for testing
*/
exports.TEST_ADDRESSES = {
// Sample public key for SECP256K1 derivation
SECP256K1: "0202ba6dc98cbe677711a45bf028a03646f9e588996eb223fad2485e8bc391b01581",
// Sample recipient address for SECP256K1 format
RECIPIENT_SECP256K1: "0203A17118eC0e64c4e4FdbDbEe0eA14D118C9aAf08C6c81bbB776Cae607cEB84EcB",
// Sample recipient address for ED25519 format
RECIPIENT_ED25519: "01e28f293af356e7a15068e535c248ec07c887b2ab7a5d9557037a0e998e5d97bf",
// Invalid address for negative tests
INVALID: "notavalidaddress",
};
/**
* Sample transaction hashes for testing
*/
exports.TEST_TRANSACTION_HASHES = {
VALID: "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef",
PENDING: "pending0123456789abcdef0123456789abcdef0123456789abcdef01234567",
FAILED: "failed00123456789abcdef0123456789abcdef0123456789abcdef01234567",
};
/**
* Sample transfer IDs for testing
*/
exports.TEST_TRANSFER_IDS = {
VALID: "123456",
NUMERIC: "654321",
INVALID: "not-a-valid-id",
};
/**
* Create a mock Casper account for testing
*
* @param options Optional parameters to override default values
* @returns A mock Casper account
*/
const createMockAccount = (options) => {
const seedIdentifier = options?.seedIdentifier || exports.TEST_ADDRESSES.SECP256K1;
const currency = (0, cryptoassets_1.getCryptoCurrencyById)("casper");
const account = {
id: `js:2:casper:${seedIdentifier}:casper_wallet`,
seedIdentifier,
derivationMode: "casper_wallet",
index: options?.index || 0,
freshAddress: options?.freshAddress || seedIdentifier,
freshAddressPath: options?.freshAddressPath || "44'/506'/0'/0/1",
blockHeight: options?.blockHeight || 0,
balance: options?.balance instanceof bignumber_js_1.default
? options.balance
: new bignumber_js_1.default(options?.balance || "10000000000"), // 10 CSPR
spendableBalance: options?.spendableBalance instanceof bignumber_js_1.default
? options.spendableBalance
: new bignumber_js_1.default(options?.spendableBalance || "10000000000"), // 10 CSPR
operations: options?.operations || [],
pendingOperations: options?.pendingOperations || [],
type: "Account",
swapHistory: [],
syncHash: undefined,
nfts: [],
used: true,
currency,
operationsCount: 0,
subAccounts: [],
creationDate: new Date(),
lastSyncDate: new Date(),
balanceHistoryCache: {
HOUR: { latestDate: null, balances: [] },
DAY: { latestDate: null, balances: [] },
WEEK: { latestDate: null, balances: [] },
},
};
return { ...account, ...options };
};
exports.createMockAccount = createMockAccount;
/**
* Create a mock Casper transaction for testing
*
* @param options Optional parameters to override default values
* @returns A mock Casper transaction
*/
const createMockTransaction = (options) => {
const defaultFees = (0, fee_1.getEstimatedFees)();
const transaction = {
family: "casper",
amount: options?.amount instanceof bignumber_js_1.default
? options.amount
: new bignumber_js_1.default(options?.amount || consts_1.CASPER_MINIMUM_VALID_AMOUNT_MOTES),
recipient: options?.recipient || exports.TEST_ADDRESSES.RECIPIENT_SECP256K1,
fees: options?.fees instanceof bignumber_js_1.default ? options.fees : defaultFees,
transferId: options?.transferId,
useAllAmount: options?.useAllAmount || false,
};
return { ...transaction, ...options };
};
exports.createMockTransaction = createMockTransaction;
/**
* Create a mock Casper operation for testing
*
* @param account The account associated with the operation
* @param transaction The transaction details
* @param options Optional parameters to override default values
* @returns A mock Casper operation
*/
const createMockOperation = (account, transaction, options) => {
const hash = options?.hash || "0x" + Math.random().toString(16).substring(2, 10);
const type = options?.type || "OUT";
const operation = {
id: options?.id || (0, operation_1.encodeOperationId)(account.id, hash, type),
hash,
type,
senders: [account.freshAddress],
recipients: [transaction.recipient],
accountId: account.id,
blockHash: options?.blockHash || null,
blockHeight: options?.blockHeight || null,
value: transaction.amount.plus(transaction.fees),
fee: transaction.fees,
date: options?.date || new Date(),
extra: {
transferId: transaction.transferId,
},
nftOperations: [],
subOperations: [],
};
return { ...operation, ...options };
};
exports.createMockOperation = createMockOperation;
/**
* Creates a set of accounts with different balances and statuses for comprehensive testing
*
* @returns An array of mock Casper accounts
*/
const createMockAccountSet = () => {
return [
// Standard account with balance
(0, exports.createMockAccount)(),
// Empty account
(0, exports.createMockAccount)({
balance: new bignumber_js_1.default("0"),
spendableBalance: new bignumber_js_1.default("0"),
}),
// Account with pending operations
(0, exports.createMockAccount)({
pendingOperations: [
(0, exports.createMockOperation)((0, exports.createMockAccount)(), (0, exports.createMockTransaction)({ amount: new bignumber_js_1.default("500000000") }), { type: "OUT", id: "pending-op-1" }),
],
}),
// Account with high balance
(0, exports.createMockAccount)({
balance: new bignumber_js_1.default("100000000000000"), // 100,000 CSPR
spendableBalance: new bignumber_js_1.default("100000000000000"),
}),
];
};
exports.createMockAccountSet = createMockAccountSet;
/**
* Creates a set of transactions with different parameters for validation testing
*
* @returns An array of mock Casper transactions
*/
const createMockTransactionSet = () => {
return [
// Standard valid transaction
(0, exports.createMockTransaction)({
amount: new bignumber_js_1.default("1000000000"), // 1 CSPR
}),
// Transaction with invalid recipient
(0, exports.createMockTransaction)({
recipient: exports.TEST_ADDRESSES.INVALID,
}),
// Transaction with zero amount (should fail validation)
(0, exports.createMockTransaction)({
amount: new bignumber_js_1.default("0"),
}),
// Transaction with very high amount (should trigger warnings)
(0, exports.createMockTransaction)({
amount: new bignumber_js_1.default("999000000000"), // 999 CSPR
}),
// Transaction with transfer ID
(0, exports.createMockTransaction)({
transferId: exports.TEST_TRANSFER_IDS.VALID,
}),
// Transaction with invalid transfer ID
(0, exports.createMockTransaction)({
transferId: exports.TEST_TRANSFER_IDS.INVALID,
}),
// Transaction with useAllAmount flag
(0, exports.createMockTransaction)({
useAllAmount: true,
}),
];
};
exports.createMockTransactionSet = createMockTransactionSet;
/**
* Creates a set of operations with different types and statuses for testing
*
* @param account The account to associate with the operations
* @returns An array of mock Casper operations
*/
const createMockOperationSet = (account) => {
const standardTx = (0, exports.createMockTransaction)({
amount: new bignumber_js_1.default("1000000000"),
});
const largeTx = (0, exports.createMockTransaction)({
amount: new bignumber_js_1.default("5000000000"),
});
const smallTx = (0, exports.createMockTransaction)({
amount: new bignumber_js_1.default("100000000"),
});
const transferIdTx = (0, exports.createMockTransaction)({
transferId: exports.TEST_TRANSFER_IDS.VALID,
});
// Create operations with different dates for sorting tests
const now = new Date();
const yesterday = new Date(now);
yesterday.setDate(yesterday.getDate() - 1);
const lastWeek = new Date(now);
lastWeek.setDate(lastWeek.getDate() - 7);
const lastMonth = new Date(now);
lastMonth.setMonth(lastMonth.getMonth() - 1);
return [
// Standard OUT operation
(0, exports.createMockOperation)(account, standardTx, {
type: "OUT",
hash: "0xoutoperation1",
date: now,
}),
// IN operation
(0, exports.createMockOperation)(account, standardTx, {
type: "IN",
hash: "0xinoperation1",
date: yesterday,
senders: [exports.TEST_ADDRESSES.RECIPIENT_SECP256K1], // Override sender
}),
// Large amount operation
(0, exports.createMockOperation)(account, largeTx, {
type: "OUT",
hash: "0xoutlarge1",
date: lastWeek,
}),
// Small amount operation
(0, exports.createMockOperation)(account, smallTx, {
type: "OUT",
hash: "0xoutsmall1",
date: lastMonth,
}),
// Operation with transfer ID
(0, exports.createMockOperation)(account, transferIdTx, {
type: "OUT",
hash: "0xtransferid1",
date: lastMonth,
}),
// Operation with block height
(0, exports.createMockOperation)(account, standardTx, {
type: "OUT",
hash: "0xwithblock1",
blockHeight: 12345,
blockHash: "0xblockhash123",
date: lastMonth,
}),
];
};
exports.createMockOperationSet = createMockOperationSet;
/**
* Creates a mock signed operation for testing broadcast functionality
*
* @param account The account associated with the operation
* @param transaction The transaction to create the signed operation from
* @param options Optional parameters to override default values
* @returns A mock signed operation
*/
const createMockSignedOperation = (account, transaction, options) => {
const operation = (0, exports.createMockOperation)(account, transaction, options?.operationOptions);
return {
signature: options?.signature || "deadbeef1234567890abcdef",
operation,
rawData: {
tx: JSON.stringify(options?.rawTxJson || {
hash: exports.TEST_TRANSACTION_HASHES.VALID,
header: {
account: account.freshAddress,
timestamp: new Date().getTime(),
},
payment: {
target: transaction.recipient,
amount: transaction.amount.toString(),
},
fee: transaction.fees.toString(),
}),
},
};
};
exports.createMockSignedOperation = createMockSignedOperation;
/**
* Creates mock data for testing account shape functionality
*
* @returns Object containing mock data for account shape tests
*/
const createMockAccountShapeData = () => {
const mockAddress = exports.TEST_ADDRESSES.SECP256K1;
const mockCurrency = (0, cryptoassets_1.getCryptoCurrencyById)("casper");
const mockDerivationMode = "casper_wallet";
const mockAccountInfo = {
address: mockAddress,
currency: mockCurrency,
derivationMode: mockDerivationMode,
index: 0,
derivationPath: "44'/506'/0'/0/0",
};
const mockAccountId = (0, index_1.encodeAccountId)({
type: "js",
version: "2",
currencyId: mockCurrency.id,
xpubOrAddress: mockAddress,
derivationMode: mockDerivationMode,
});
const mockBlockHeight = 12345;
const mockPurseUref = "uref-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef-007";
const mockAccountHash = "account-hash-1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef";
const mockBalance = new bignumber_js_1.default("1000000000");
const mockTxs = [
{
deploy_hash: "deploy-hash-1",
block_hash: "block-hash-1",
caller_public_key: mockAddress,
execution_type_id: 1,
cost: "10000",
payment_amount: "100000000",
timestamp: "2023-01-01T12:00:00Z",
status: "success",
args: {
id: {
parsed: 12345,
cl_type: {
Option: "U64",
},
},
amount: {
parsed: "500000000",
cl_type: "U512",
},
target: {
parsed: exports.TEST_ADDRESSES.RECIPIENT_SECP256K1,
cl_type: "PublicKey",
},
},
amount: "500000000",
},
];
const mockOperations = [
{
id: "mock-operation-id",
hash: "deploy-hash-1",
type: "OUT",
value: new bignumber_js_1.default("550000000"),
fee: new bignumber_js_1.default("50000000"),
blockHeight: 12345,
blockHash: null,
hasFailed: false,
accountId: mockAccountId,
senders: [mockAccountHash],
recipients: ["recipient-account-hash"],
date: new Date("2023-01-01T12:00:00Z"),
extra: {
transferId: "12345",
},
},
];
return {
mockAddress,
mockCurrency,
mockDerivationMode,
mockAccountInfo,
mockAccountId,
mockBlockHeight,
mockPurseUref,
mockAccountHash,
mockBalance,
mockTxs,
mockOperations,
};
};
exports.createMockAccountShapeData = createMockAccountShapeData;
//# sourceMappingURL=fixtures.js.map