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
38 changes: 38 additions & 0 deletions test/chains/stellar/vectors/encoding.vector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { readFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, test, expect } from 'vitest';
import { encodeStealthMetaAddress, decodeStealthMetaAddress } from '../../../../src/chains/stellar/meta-address';
import { bytesToHex, hexToBytes } from '../../../../src/chains/stellar/utils';

const __dirname = dirname(fileURLToPath(import.meta.url));
const VECTORS_DIR = resolve(__dirname, '../../../../../packages/test-vectors/dist/stellar');

const file = JSON.parse(
readFileSync(resolve(VECTORS_DIR, 'encoding.json'), 'utf8'),
) as {
vectors: Array<{
id: string;
spendingPubKey: string;
viewingPubKey: string;
expected: { metaAddress: string };
}>;
};

describe('stellar encoding vectors', () => {
for (const v of file.vectors) {
test(`${v.id}: encode matches expected`, () => {
const spendingPubKey = hexToBytes(v.spendingPubKey);
const viewingPubKey = hexToBytes(v.viewingPubKey);

const metaAddress = encodeStealthMetaAddress(spendingPubKey, viewingPubKey);
expect(metaAddress).toBe(v.expected.metaAddress);
});

test(`${v.id}: decode round-trip`, () => {
const decoded = decodeStealthMetaAddress(v.expected.metaAddress);
expect(bytesToHex(decoded.spendingPubKey)).toBe(v.spendingPubKey);
expect(bytesToHex(decoded.viewingPubKey)).toBe(v.viewingPubKey);
});
}
});
43 changes: 43 additions & 0 deletions test/chains/stellar/vectors/key-derivation.vector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { readFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, test, expect } from 'vitest';
import { deriveStealthKeys } from '../../../../src/chains/stellar/keys';
import { bytesToHex, hexToBytes } from '../../../../src/chains/stellar/utils';
import { scalarToBytes } from '../../../../src/chains/stellar/scalar';

const __dirname = dirname(fileURLToPath(import.meta.url));
const VECTORS_DIR = resolve(__dirname, '../../../../../packages/test-vectors/dist/stellar');

const file = JSON.parse(
readFileSync(resolve(VECTORS_DIR, 'key-derivation.json'), 'utf8'),
) as {
vectors: Array<{
id: string;
signature: string;
expected: {
spendingKey: string;
viewingKey: string;
spendingScalar: string;
viewingScalar: string;
spendingPubKey: string;
viewingPubKey: string;
};
tags: string[];
}>;
};

describe('stellar key-derivation vectors', () => {
for (const v of file.vectors) {
test(`${v.id}${v.tags.length ? ` [${v.tags.join(', ')}]` : ''}`, () => {
const keys = deriveStealthKeys(hexToBytes(v.signature));

expect(bytesToHex(keys.spendingKey)).toBe(v.expected.spendingKey);
expect(bytesToHex(keys.viewingKey)).toBe(v.expected.viewingKey);
expect(bytesToHex(scalarToBytes(keys.spendingScalar))).toBe(v.expected.spendingScalar);
expect(bytesToHex(scalarToBytes(keys.viewingScalar))).toBe(v.expected.viewingScalar);
expect(bytesToHex(keys.spendingPubKey)).toBe(v.expected.spendingPubKey);
expect(bytesToHex(keys.viewingPubKey)).toBe(v.expected.viewingPubKey);
});
}
});
79 changes: 79 additions & 0 deletions test/chains/stellar/vectors/scan-match.vector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { readFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, test, expect } from 'vitest';
import { checkStealthAddress } from '../../../../src/chains/stellar/scan';
import { deriveStealthPrivateScalar } from '../../../../src/chains/stellar/spend';
import { scalarToBytes } from '../../../../src/chains/stellar/scalar';
import { bytesToHex, hexToBytes } from '../../../../src/chains/stellar/utils';
import { SCHEME_ID } from '../../../../src/chains/stellar/constants';

const __dirname = dirname(fileURLToPath(import.meta.url));
const VECTORS_DIR = resolve(__dirname, '../../../../../packages/test-vectors/dist/stellar');

interface ScanVector {
id: string;
description: string;
announcement: {
schemeId: number;
stealthAddress: string;
caller: string;
ephemeralPubKey: string;
metadata: string;
};
viewingKey: string;
spendingPubKey: string;
spendingScalar: string;
expected: {
isMatch: boolean;
stealthAddress: string | null;
stealthPrivateScalar: string | null;
};
tags: string[];
}

function hexToScalar(hex: string): bigint {
const bytes = hexToBytes(hex);
let scalar = 0n;
for (let i = bytes.length - 1; i >= 0; i--) {
scalar = (scalar << 8n) | BigInt(bytes[i]);
}
return scalar;
}

const file = JSON.parse(
readFileSync(resolve(VECTORS_DIR, 'scan-match.json'), 'utf8'),
) as { vectors: ScanVector[] };

describe('stellar scan-match vectors', () => {
for (const v of file.vectors) {
test(`${v.id}: ${v.description}`, () => {
if (v.announcement.schemeId !== SCHEME_ID) {
expect(v.expected.isMatch).toBe(false);
return;
}

const ephemeralPubKey = hexToBytes(v.announcement.ephemeralPubKey);
const viewingKey = hexToBytes(v.viewingKey);
const spendingPubKey = hexToBytes(v.spendingPubKey);
const viewTag = hexToBytes(v.announcement.metadata)[0];

const result = checkStealthAddress(ephemeralPubKey, viewingKey, spendingPubKey, viewTag);

if (!v.expected.isMatch) {
expect(result.isMatch).toBe(false);
return;
}

expect(result.isMatch).toBe(true);
expect(result.stealthAddress).toBe(v.expected.stealthAddress);

const stealthPrivScalar = deriveStealthPrivateScalar(
hexToScalar(v.spendingScalar),
viewingKey,
ephemeralPubKey,
);
expect(bytesToHex(scalarToBytes(stealthPrivScalar))).toBe(v.expected.stealthPrivateScalar);
});
}
});
44 changes: 44 additions & 0 deletions test/chains/stellar/vectors/signing.vector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { readFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, test, expect } from 'vitest';
import { signWithScalar } from '../../../../src/chains/stellar/scalar';
import { bytesToHex, hexToBytes } from '../../../../src/chains/stellar/utils';

const __dirname = dirname(fileURLToPath(import.meta.url));
const VECTORS_DIR = resolve(__dirname, '../../../../../packages/test-vectors/dist/stellar');

function hexToScalar(hex: string): bigint {
const bytes = hexToBytes(hex);
let scalar = 0n;
for (let i = bytes.length - 1; i >= 0; i--) {
scalar = (scalar << 8n) | BigInt(bytes[i]);
}
return scalar;
}

const file = JSON.parse(
readFileSync(resolve(VECTORS_DIR, 'signing.json'), 'utf8'),
) as {
vectors: Array<{
id: string;
scalar: string;
publicKey: string;
message: string;
expected: { signature: string };
tags: string[];
}>;
};

describe('stellar signing vectors', () => {
for (const v of file.vectors) {
test(`${v.id} [${v.tags.join(', ')}]`, () => {
const signature = signWithScalar(
hexToBytes(v.message),
hexToScalar(v.scalar),
hexToBytes(v.publicKey),
);
expect(bytesToHex(signature)).toBe(v.expected.signature);
});
}
});
37 changes: 37 additions & 0 deletions test/chains/stellar/vectors/stealth-address.vector.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { readFileSync } from 'node:fs';
import { resolve, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { describe, test, expect } from 'vitest';
import { generateStealthAddress } from '../../../../src/chains/stellar/stealth';
import { bytesToHex, hexToBytes } from '../../../../src/chains/stellar/utils';

const __dirname = dirname(fileURLToPath(import.meta.url));
const VECTORS_DIR = resolve(__dirname, '../../../../../packages/test-vectors/dist/stellar');

const file = JSON.parse(
readFileSync(resolve(VECTORS_DIR, 'stealth-address.json'), 'utf8'),
) as {
vectors: Array<{
id: string;
spendingPubKey: string;
viewingPubKey: string;
ephemeralPrivateKey: string;
expected: { stealthAddress: string; ephemeralPubKey: string; viewTag: number };
}>;
};

describe('stellar stealth-address vectors', () => {
for (const v of file.vectors) {
test(v.id, () => {
const result = generateStealthAddress(
hexToBytes(v.spendingPubKey),
hexToBytes(v.viewingPubKey),
hexToBytes(v.ephemeralPrivateKey),
);

expect(result.stealthAddress).toBe(v.expected.stealthAddress);
expect(bytesToHex(result.ephemeralPubKey)).toBe(v.expected.ephemeralPubKey);
expect(result.viewTag).toBe(v.expected.viewTag);
});
}
});