Skip to content

feat: port high-level DApp crypto utilities from wallet plugin#37

Merged
gmesika-coti merged 11 commits into
coti-io:mainfrom
plucena-coti:feat/dapp-crypto-utils-fork-base
Jun 14, 2026
Merged

feat: port high-level DApp crypto utilities from wallet plugin#37
gmesika-coti merged 11 commits into
coti-io:mainfrom
plucena-coti:feat/dapp-crypto-utils-fork-base

Conversation

@plucena-coti

@plucena-coti plucena-coti commented May 26, 2026

Copy link
Copy Markdown
Contributor

This PR merges the robust logic originally wrapped in the wallet-plugin down into crypto_utils.ts, bringing strict AES key validation normalizeAesKey, balance threshold sanity checks in decryptCtUint64, signature V-byte mapping in normalizeSignature and buildItSignature to the TypeScript SDK

@plucena-coti plucena-coti force-pushed the feat/dapp-crypto-utils-fork-base branch from 94a976f to 15f0465 Compare May 26, 2026 19:07
@plucena-coti plucena-coti force-pushed the feat/dapp-crypto-utils-fork-base branch from 15f0465 to 1d84b64 Compare May 27, 2026 22:20
@plucena-coti

Copy link
Copy Markdown
Contributor Author

@gmesika-coti please approve

gh pr edit 37 --repo coti-io/coti-sdk-typescript --add-reviewer gmesika-coti
GraphQL: plucena-coti does not have the correct permissions to execute RequestReviews (requ
estReviews)

…ing sign() in buildItSignature

- Removed signDigest() and normalizeSignature() which duplicated logic already
  available via the existing sign() utility.
- Simplified buildItSignature() to call sign() directly and wrap with hexlify().
- Removed unused 'concat' import from ethers.
@plucena-coti

Copy link
Copy Markdown
Contributor Author

Updated: Removed redundant signDigest() and normalizeSignature() functions — their logic was already covered by the existing sign() utility. buildItSignature() now calls sign() directly wrapped with hexlify(), keeping the API surface minimal and avoiding duplication.

plucena and others added 5 commits June 10, 2026 10:11
- Introduced buildItMessageHash() as a shared helper that computes the
  solidityPackedKeccak256 digest used by both signInputText and buildItSignature.
- Eliminates duplicated hash construction across the two signing paths.
- normalizeAesKey: prefix stripping, lowercasing, length and hex validation
- validateAesKey: null/empty guards and normalization propagation
- isInsaneDecryptedValue: threshold logic, custom base, decimal clamping
- decryptCtUint64: zero passthrough, round-trip, invalid key handling, sanity check
- buildItSignature: 65-byte output, signer recovery, parity with signInputText
… throw on invalid keys

- normalizeAesKey now only accepts 32-char hex strings (128-bit) since COTI
  exclusively uses AES-128; 256-bit keys are no longer silently accepted.
- decryptCtUint64 removes the try/catch wrapper — invalid keys now throw
  directly instead of being swallowed and returning null.
- Updated tests to reflect the stricter validation behavior.
- normalizeDecimals now throws on invalid input (negative, >36, non-integer,
  non-finite) instead of silently clamping.
- isZeroValue simplified to only handle bigint (matching ctUint type).
- buildItSignature validates that signerAddress matches the privateKey-derived
  address before signing.
- Improved JSDoc across all wallet plugin additions.
- Consistent code style (4-space indent, no semicolons).
- Tests updated: added cases for decimal validation throws and signer mismatch.
…idation

- Replaced direct address derivation with a Wallet instance for improved clarity.
- Simplified the signature generation process by utilizing signInputText.
- Maintained existing validation to ensure signerAddress matches the derived address from the privateKey.
@gmesika-coti gmesika-coti requested a review from a team June 10, 2026 20:23
plucena added 4 commits June 10, 2026 21:24
…CtUint64 and validateAesKey

- Merged validateAesKey logic into normalizeAesKey (now accepts null/undefined).
- Pushed zero-ciphertext short-circuit and key normalization into decryptUint
  directly, eliminating the need for the decryptCtUint64 wrapper.
- Removed decryptCtUint64, validateAesKey, isZeroValue, and DecryptionOptions
  as they are no longer needed.
- Fixed stringCrypt test to convert generateRandomAesKeySizeNumber() output
  from binary to hex (matching the SDK's documented 32-char format).
- Updated error.handling tests to assert decryptUint now throws on invalid keys
  instead of silently producing incorrect results.
- All 366 tests passing.
…ment

- Moved normalizeAesKey call into encodeKey so every encrypt/decrypt path
  validates the key consistently at the lowest level.
- Removed the duplicate normalizeAesKey call from decryptUint (now handled
  by encodeKey).
- Updated error.handling and integration/validation tests: encodeKey, prepareIT,
  and decryptUint256 now throw on malformed keys instead of silently producing
  incorrect results.
- All 358 tests passing.
@sonarqubecloud

Copy link
Copy Markdown

@gmesika-coti gmesika-coti merged commit 974beea into coti-io:main Jun 14, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants