Skip to content

Commit

Permalink
fix: check nonceType in DaimoNonceMetadata.fromHex (daimo-eth#284)
Browse files Browse the repository at this point in the history
  • Loading branch information
nalinbhardwaj authored Oct 2, 2023
1 parent 09d9ab1 commit 17165d6
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apps/daimo-mobile/src/view/screen/HistoryOpScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function TransferBody({ op }: { op: TransferOpEvent }) {
assert(account != null);

const opRequestId = op.nonceMetadata
? DaimoNonceMetadata.fromHex(op.nonceMetadata).identifier.toString()
? DaimoNonceMetadata.fromHex(op.nonceMetadata)?.identifier.toString()
: undefined;
const matchingTrackedRequest = account.trackedRequests.find(
(req) =>
Expand Down
6 changes: 4 additions & 2 deletions packages/daimo-api/src/contract/opIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ export class OpIndexer {

const nonceMetadata = DaimoNonce.fromHex(
numberToHex(log.args.nonce, { size: 32 })
).metadata.toHex();
)?.metadata.toHex();
if (!nonceMetadata) continue;

const curTxes = this.nonceMetadataToTxes.get(nonceMetadata);
const newTxes = curTxes
? [...curTxes, log.transactionHash]
Expand All @@ -65,7 +67,7 @@ export class OpIndexer {
if (log.logIndex > queryLogIndex) {
return DaimoNonce.fromHex(
numberToHex(log.args.nonce, { size: 32 })
).metadata.toHex();
)?.metadata.toHex();
}
}
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/daimo-api/src/pushNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ export class PushNotifier {

const receivingRequestedMoney =
log.nonceMetadata != null &&
DaimoNonceMetadata.fromHex(log.nonceMetadata).nonceType ===
DaimoNonceMetadata.fromHex(log.nonceMetadata)?.nonceType ===
DaimoNonceType.RequestResponse;

const [a, b] = await Promise.all([
Expand Down
8 changes: 5 additions & 3 deletions packages/daimo-userop/src/nonce.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ export class DaimoNonce {
return nonce;
}

public static fromHex(nonce: Hex): DaimoNonce {
public static fromHex(nonce: Hex): DaimoNonce | undefined {
const paddedNonce = bytesToHex(hexToBytes(nonce), { size: 64 });
const hexMetadata = paddedNonce.slice(0, 2 + 16) as Hex;
const metadata = DaimoNonceMetadata.fromHex(hexMetadata);
const key = `0x${paddedNonce.slice(2 + 16, 2 + 16 + 32)}` as Hex;
return new DaimoNonce(metadata, key);
return metadata ? new DaimoNonce(metadata, key) : undefined;
}
}

Expand Down Expand Up @@ -62,9 +62,11 @@ export class DaimoNonceMetadata {
this.identifier = identifier;
}

public static fromHex(hexMetadata: Hex): DaimoNonceMetadata {
public static fromHex(hexMetadata: Hex): DaimoNonceMetadata | undefined {
assert(hexMetadata.length === 16 + 2);
const nonceType = parseInt(hexMetadata.slice(2, 4), 16);
if (!(nonceType in DaimoNonceType) || nonceType === DaimoNonceType.MAX)
return undefined;
const identifier = BigInt("0x" + hexMetadata.slice(4)) as bigint;
return new DaimoNonceMetadata(nonceType, identifier);
}
Expand Down
12 changes: 12 additions & 0 deletions packages/daimo-userop/test/nonce.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,15 @@ test("DaimoNonce serde", () => {
}
}
});

test("DaimoNonce bad Nonce type serialization", () => {
const hexNonceBadType =
"0xFE00000000000123123412341234123412341234123412340000000000000000"; // Nonce type = 254
let parsed = DaimoNonce.fromHex(hexNonceBadType);
assert.strictEqual(parsed, undefined);

const hexNonceMaxType =
"0xFF00000000000123123412341234123412341234123412340000000000000000"; // Nonce type = 255
parsed = DaimoNonce.fromHex(hexNonceMaxType);
assert.strictEqual(parsed, undefined);
});

0 comments on commit 17165d6

Please sign in to comment.