Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions shared/stellar-utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ export function formatAmount(amount: string, decimals: number = 7): string {
}
}

/**
* Validate a Stellar transaction hash (64-character lowercase hex string)
*/
export function validateTransactionHash(hash: string): boolean {
return /^[0-9a-f]{64}$/.test(hash);
}

export function buildPaymentOperation(params: { source?: string; destination: string; asset: string; amount: string }){
// Placeholder: return normalized operation object
return {
Expand Down
3 changes: 2 additions & 1 deletion shared/stellar-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"scripts": {
"build": "tsc",
"type-check": "tsc --noEmit"
"type-check": "tsc --noEmit",
"test": "cross-env TS_NODE_TRANSPILE_ONLY=true node --loader ts-node/esm transaction-hash.test.ts"
}
}
32 changes: 32 additions & 0 deletions shared/stellar-utils/transaction-hash.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import test from 'node:test';
import assert from 'node:assert';
import { validateTransactionHash } from './index.js';

test('validateTransactionHash returns true for a valid 64-char lowercase hex', () => {
const hash = 'abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789';
assert.strictEqual(hash.length, 64);
assert.strictEqual(validateTransactionHash(hash), true);
});

test('validateTransactionHash returns false for hash with invalid length', () => {
assert.strictEqual(validateTransactionHash('a1b2c3d4'), false);
assert.strictEqual(validateTransactionHash(''), false);
assert.strictEqual(validateTransactionHash('a'.repeat(63)), false);
assert.strictEqual(validateTransactionHash('a'.repeat(65)), false);
});

test('validateTransactionHash returns false for uppercase hex', () => {
const hash = 'A1B2C3D4E5F6A7B8C9D0E1F2A3B4C5D6E7F8A9B0C1D2E3F4A5B6C7D8E9F0A1B';
assert.strictEqual(validateTransactionHash(hash), false);
});

test('validateTransactionHash returns false for non-hex characters', () => {
assert.strictEqual(validateTransactionHash('x'.repeat(64)), false);
assert.strictEqual(validateTransactionHash('g'.repeat(64)), false);
assert.strictEqual(validateTransactionHash('z1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6e7f8a9b0c1d2e3f4a5b6c7d8e9f0a1b'), false);
});

test('validateTransactionHash returns false for non-string input', () => {
assert.strictEqual(validateTransactionHash(undefined as any), false);
assert.strictEqual(validateTransactionHash(null as any), false);
});