Skip to content
Merged
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
2 changes: 1 addition & 1 deletion src.ts/_tests/golden-capture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ async function transactions(): Promise<Bucket> {
});
await cap(o, "signed", async () => {
const w = new Wallet({ key: PRIV_A, prefix: PREFIX });
const signed = await w.signTransaction(base);
const signed = await w.signTransaction({ ...base, to: w.address });
const parsed = Transaction.from(signed);
return {
serialized: signed,
Expand Down
6 changes: 3 additions & 3 deletions src.ts/_tests/test-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { networkIdToPrefix } from "../address/index.js";

const TIMEOUT_PERIOD = 120000;
const tesSignedTransaction =
"0xf8ce800a830f423f0496ce276773ac97d16855a3c8faa45399136b56d419486081c880b8ab54bc60412c90c104a89865a43c33f335836bcb473f0cd477ecb2c82e6310676c87b708e3971d009b4a04e518f12454c3ee1aebc19a6b7df00076b834b692b7432611f3dafff0f422dfbae722ba4b989cfc82fee2e539fbbf21d652ea587df59e8421a3a8bc8ca2ba35033598954c54e10c00315484db568379ce94f9c894e3e6e4c7ee216676b713ca892d9b26746ae902a772e217a6a8bb493ce2bb313cf0cb66e76765d4c45ec6b68600";
"0xf8ce800a830f423f0396ab03a5fd22b9bee8b8ab877c86e0a2c21765e1d5bfc581c880b8abcee66d9ba5aed1a813379ac23b7a326830bcbca9501b9c0d1e1214d71814ec96f41330d1abdd7a56babda62a3eaa1c376699a64e152b6c928064fe78973123e0d8a3a6effbe6f809384fd1af97e7094fd839d6f92c379707f177d4bddf13cac611c10802c02a0dd7f2f4bc1a248a7df02600315484db568379ce94f9c894e3e6e4c7ee216676b713ca892d9b26746ae902a772e217a6a8bb493ce2bb313cf0cb66e76765d4c45ec6b68600";
const testWalletPhrase =
"better artwork flavor fish solve deer orient spread adapt doll attack hour sort copper super income bacon engine skate ill similar wink crack club";
const testSeed =
Expand Down Expand Up @@ -55,12 +55,12 @@ const baseWallet = new BaseWallet({
});
const randomWallet = Wallet.createRandom(networkIdToPrefix(3), provider);
const transaction = Transaction.from({
to: "0xce276773ac97d16855a3c8faa45399136b56d4194860",
to: baseWalletAddress,
value: 200n,
nonce: 0,
energyLimit: 999999n,
energyPrice: 10n,
networkId: 4,
networkId: 3,
});

// The provider is shared by every suite in this file, so it has to outlive
Expand Down
53 changes: 30 additions & 23 deletions src.ts/_tests/test-core-units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ describe("Core denomination ladder", () => {

it("treats ore as the atomic unit with no fractional part", () => {
assert.equal(parseUnits("1", "ore"), BigInt(1));
// Sub-ore precision is truncated rather than rounded or rejected.
assert.equal(parseUnits("1.9", "ore"), BigInt(1));
// A fractional ore cannot be represented.
assert.throws(() => parseUnits("1.9", "ore"), /too many decimals/);
assert.equal(formatUnits(BigInt(1), "ore"), "1");
});

Expand Down Expand Up @@ -116,7 +116,7 @@ describe("Core denominations reject Ethereum names", () => {
describe("unit conversion by explicit decimals", () => {
it("accepts a decimal count in place of a unit name", () => {
assert.equal(parseUnits("1.5", 6), BigInt(1500000));
assert.equal(parseUnits("1.5", 0), BigInt(1));
assert.throws(() => parseUnits("1.5", 0), /too many decimals/);
assert.equal(formatUnits(BigInt(1500000), 6), "1.5");
});

Expand All @@ -127,7 +127,8 @@ describe("unit conversion by explicit decimals", () => {

it("agrees with the named units", () => {
for (const [unit, decimals] of LADDER) {
assert.equal(parseUnits("2.5", unit), parseUnits("2.5", decimals), unit);
const exact = decimals === 0 ? "2" : "2.5";
assert.equal(parseUnits(exact, unit), parseUnits(exact, decimals), unit);
const value = BigInt(10) ** BigInt(18);
assert.equal(
formatUnits(value, unit),
Expand Down Expand Up @@ -176,14 +177,31 @@ describe("unit conversion input handling", () => {
assert.equal(parseUnits("1e-18", "core"), BigInt(1));
});

it("truncates precision beyond the unit's decimals", () => {
// 19 decimal places against an 18 decimal unit: the extra digit is
// dropped rather than rounding or throwing.
assert.equal(
parseUnits("1." + "1".repeat(19), "core"),
BigInt("1111111111111111111"),
);
assert.equal(parseUnits("1.9999", 2), BigInt(199));
it("rejects unrepresentable amounts and accepts extra trailing zeros", () => {
for (const value of [
"1." + "1".repeat(19),
"0.0000000000000000001",
"1e-19",
"-1e-19",
]) {
assert.throws(() => parseXCB(value), /too many decimals/);
assert.throws(() => parseUnits(value), /too many decimals/);
}
assert.throws(() => parseUnits("1.9999", 2), /too many decimals/);
assert.equal(parseUnits("1.2300", 2), 123n);
assert.equal(parseUnits("1.000", "ore"), 1n);
});

it("keeps scientific amounts exact and bounds exponent expansion", () => {
assert.equal(parseXCB("1.000000000000000001e0"), 1000000000000000001n);
assert.equal(parseUnits("9007199254740993e0", "ore"), 9007199254740993n);
assert.equal(parseUnits("-12345E-2", 2), -12345n);
assert.equal(parseUnits(".0012e+4", 0), 12n);
assert.equal(parseUnits("+1.234e+3", 0), 1234n);
assert.equal(parseXCB("10000000000000000000e-37"), 1n);
assert.throws(() => parseXCB("1e1000000"), /overflow/);
assert.throws(() => parseXCB("1e-1000000"), /too many decimals/);
assert.equal(parseXCB("0e1000000"), 0n);
});

it("requires the parse input to be a string", () => {
Expand Down Expand Up @@ -228,17 +246,6 @@ describe("unit conversion input handling", () => {
}
});

it("still truncates legitimate excess precision", () => {
// Rejecting malformed input must not break the truncation of a single
// over-long fraction.
assert.equal(
parseUnits("1." + "1".repeat(19), "core"),
BigInt("1111111111111111111"),
);
assert.equal(parseUnits("1.9", "ore"), BigInt(1));
assert.equal(parseUnits("1.9999", 2), BigInt(199));
});

it("accepts the full range of BigNumberish for formatting", () => {
assert.equal(formatUnits("1000000000000000000", "core"), "1.0");
assert.equal(formatUnits(BigInt(10) ** BigInt(18), "core"), "1.0");
Expand Down
116 changes: 116 additions & 0 deletions src.ts/_tests/test-default-provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import assert from "node:assert/strict";
import {
getDefaultProvider,
JsonRpcProvider,
Network,
WebSocketProvider,
} from "../index.js";

import type { Networkish } from "../providers/index.js";

describe("Core default RPC profiles", () => {
const profiles = [
{
name: "mainnet",
id: 1,
url: "https://xcbapi.coreblockchain.net/",
inputs: [undefined, "mainnet", "cb", 1, 1n],
},
{
name: "devin",
id: 3,
url: "https://xcbapi.corecoin.cc/",
inputs: ["devin", "testnet", "ab", 3, 3n],
},
];

for (const { name, id, url, inputs } of profiles) {
it(`routes each form of ${name} directly to its Core endpoint`, () => {
const networks: Array<Networkish | undefined> = [
...inputs,
new Network(name, id),
{ name, networkId: id },
];
for (const network of networks) {
const provider = getDefaultProvider(network);
try {
assert.ok(provider instanceof JsonRpcProvider);
assert.equal(provider._getConnection().url, url);
} finally {
provider.destroy();
}
}
});

it(`verifies the RPC network ID for ${name}`, async () => {
const provider = getDefaultProvider(name);
assert.ok(provider instanceof JsonRpcProvider);
let reportedId = id;
provider._send = async (payload) => {
assert.ok(!Array.isArray(payload));
assert.equal(payload.method, "xcb_networkId");
return [{ id: payload.id, result: "0x" + reportedId.toString(16) }];
};
try {
assert.equal((await provider.getNetwork()).networkId, BigInt(id));
reportedId = id === 1 ? 3 : 1;
await assert.rejects(provider.getNetwork(), { code: "NETWORK_ERROR" });
} finally {
provider.destroy();
}
});
}

it("requires an explicit endpoint outside the two public networks", () => {
for (const network of [
"homestead",
"ropsten",
"sepolia",
"matic",
"private",
"ce",
1337,
new Network("ropsten", 3),
new Network("devin", 1),
]) {
assert.throws(
() => getDefaultProvider(network),
/unknown network|no public Core RPC/,
);
}
});

it("rejects legacy backend selection for named profiles", () => {
assert.throws(
() => getDefaultProvider("mainnet", { exclusive: "infura" }),
/backend options/,
);
});

it("connects directly to an explicitly supplied HTTP URL or WebSocket", async () => {
const url = "http://localhost:8545/";
const http = getDefaultProvider(url);
try {
assert.ok(http instanceof JsonRpcProvider);
assert.equal(http._getConnection().url, url);
} finally {
http.destroy();
}

const socket = {
onopen: null,
onmessage: null,
onerror: null,
readyState: 0,
send() {},
close() {},
};
const ws = getDefaultProvider(socket);
try {
assert.ok(ws instanceof WebSocketProvider);
assert.equal(ws.websocket, socket);
} finally {
await ws.destroy();
}
});
});
16 changes: 16 additions & 0 deletions src.ts/_tests/test-devin-fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,22 @@ describe("Devin transaction responses", () => {
);
assert.equal(tx.energyPrice, 10_000_000_000n);
assert.equal(tx.networkId, 3n);
assert.equal(tx.index, 0);
});

it("reads nonzero RPC transaction indexes and preserves a normalized index", () => {
const raw = { ...swapResponse, transactionIndex: "0x10" };
assert.equal(formatTransactionResponse(raw).index, 16);
assert.equal(formatTransactionResponse({ ...raw, index: 0 }).index, 0);
});

it("leaves the index undefined when a pending transaction has no position", () => {
const { transactionIndex, ...fields } = swapResponse;
const pending = { ...fields, blockHash: null, blockNumber: null };
for (const raw of [pending, { ...pending, transactionIndex: null }]) {
const tx = formatTransactionResponse(raw);
assert.equal(tx.index, undefined);
}
});

it("re-serializes to the hash the network assigned", () => {
Expand Down
2 changes: 2 additions & 0 deletions src.ts/_tests/test-live-devin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,14 @@ const wxcbAbi = [
assert.ok(tx);
assert.equal(tx.from, SWAP_SENDER);
assert.equal(tx.networkId, 3n);
assert.equal(tx.index, 0);
assert.ok(tx.energyPrice > 0n);
assert.equal(Transaction.from(tx).hash, SWAP_HASH);

const block = await provider.getBlock(SWAP_BLOCK, true);
assert.equal(block?.prefetchedTransactions[0].hash, SWAP_HASH);
assert.equal(block?.prefetchedTransactions[0].networkId, 3n);
assert.equal(block?.prefetchedTransactions[0].index, tx.index);

const receipt = await provider.getTransactionReceipt(SWAP_HASH);
assert.equal(receipt?.status, 1);
Expand Down
44 changes: 44 additions & 0 deletions src.ts/_tests/test-mnemonic-hd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,27 @@ describe("Mnemonic: seed derivation and password", () => {
assert.equal(mnemonic.computeSeed(), seed, "deterministic");
});

it("matches BIP-39 with a Unicode passphrase on every mnemonic entry point", () => {
const phrase = "abandon ".repeat(11) + "about";
// Independent PBKDF2-HMAC-SHA512 vector over NFKD UTF-8.
const expected =
"0d02d87e72ac4f3c2f2b782309d759672b1bfdc60263be08c188007731870434ef952d7061cf5bcef1301d75d45ae91d2986ff844829bd15607a26851f8f4ca6";
for (const password of ["Café 🔑", "Cafe\u0301 🔑"]) {
assert.equal(
Mnemonic.fromPhrase({ phrase, password }).computeSeed(),
expected,
);
assert.equal(
HDNodeWallet.mnemonicToSeed(phrase.split(" ").join("\u3000"), password),
expected,
);
assert.equal(
Wallet.fromPhrase({ phrase, password, prefix: PREFIX }).address,
Wallet.fromSeed({ seed: expected, prefix: PREFIX }).address,
);
}
});

it("gives every strength its own seed", () => {
const seeds = STRENGTHS.map(({ bytes }) =>
Mnemonic.fromEntropy("0x" + "0f".repeat(bytes)).computeSeed(),
Expand Down Expand Up @@ -1251,6 +1272,29 @@ describe("HDNodeWallet: network prefixes", () => {
});

describe("HDNodeWallet: seed input handling", () => {
it("redacts invalid seeds from error details on both public entry points", () => {
const marker = "abcdef012345";
for (const seed of [
marker,
marker.repeat(12),
marker + "z",
new Uint8Array([0xab]),
{ secret: marker },
]) {
for (const create of [Wallet.fromSeed, HDNodeWallet.fromSeed]) {
assert.throws(
() => create({ seed: seed as any, prefix: PREFIX }),
(err: any) =>
err.code === "INVALID_ARGUMENT" &&
err.argument === "seed" &&
err.value === "[ REDACTED ]" &&
!err.message.includes(marker) &&
!JSON.stringify(err).includes(marker),
);
}
}
});

it("derives the same wallet from a seed given as hex text or bytes", () => {
const expected = hd();
assert.equal(
Expand Down
Loading