-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: refactor and update AA reference (#236)
- closes #190 - reorders the pages in the AA developer reference to improve information flow - adds new details about using P256Verify & using smart accounts
- Loading branch information
1 parent
89b8073
commit 2a6fe0d
Showing
5 changed files
with
184 additions
and
93 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
....build/65.developer-reference/40.account-abstraction/50.signature-validation.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
--- | ||
title: Signature Validation | ||
description: Recommended approaches to signature validation. | ||
--- | ||
|
||
One of the most notable differences between various types of accounts to be built | ||
is different signature schemes. We expect accounts to support the [EIP-1271](https://eips.ethereum.org/EIPS/eip-1271) standard. | ||
|
||
## OpenZeppelin Libraries | ||
|
||
The | ||
[`@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol`](https://github.com/OpenZeppelin/openzeppelin-contracts/blob/5ed5a86d1d22f387ce69ab4e0ace405de8bc888d/contracts/utils/cryptography/SignatureChecker.sol#L22) | ||
library provides a way to verify signatures for different | ||
account implementations. We strongly encourage you to use this library whenever you need to check that a signature of an account is correct. | ||
|
||
### Adding the library to your project | ||
|
||
::code-group | ||
|
||
```bash [npm] | ||
npm add @openzeppelin/contracts | ||
``` | ||
|
||
```bash [yarn] | ||
yarn add @openzeppelin/contracts | ||
``` | ||
|
||
```bash [pnpm] | ||
pnpm add @openzeppelin/contracts | ||
``` | ||
|
||
```bash [bun] | ||
bun add @openzeppelin/contracts | ||
``` | ||
|
||
:: | ||
|
||
### Example of using the library | ||
|
||
```solidity | ||
pragma solidity ^0.8.0; | ||
import { SignatureChecker } from "@openzeppelin/contracts/utils/cryptography/SignatureChecker.sol"; | ||
contract TestSignatureChecker { | ||
using SignatureChecker for address; | ||
function isValidSignature( | ||
address _address, | ||
bytes32 _hash, | ||
bytes memory _signature | ||
) public pure returns (bool) { | ||
return _address.isValidSignatureNow(_hash, _signature); | ||
} | ||
} | ||
``` | ||
|
||
## Verifying AA signatures | ||
|
||
Our SDK provides two methods within `utils` to verify the signature of an account: | ||
[`isMessageSignatureCorrect`](https://sdk.zksync.io/js/ethers/api/v6/utilities#ismessagesignaturecorrect) and [`isTypedDataSignatureCorrect`](https://sdk.zksync.io/js/ethers/api/v6/utilities#istypeddatasignaturecorrect). | ||
|
||
```ts | ||
import { utils, EIP712Signer } from "zksync-ethers"; | ||
|
||
const isValidMessageSignature = await utils.isMessageSignatureCorrect(provider, ADDRESS, message, messageSignature); | ||
|
||
const isValidTypesSignature = await utils.isTypedDataSignatureCorrect(provider, ADDRESS, await eip712Signer.getDomain(), utils.EIP712_TYPES, EIP712Signer.getSignInput(tx), typedSignature); | ||
``` | ||
|
||
Currently these methods only support verifying ECDSA signatures, but very soon they will support EIP1271 signature verification as well. | ||
|
||
Both of these methods return `true` or `false` depending on whether the message signature is correct. | ||
|
||
It is **not recommended** to use the `ethers.js` library to verify user signatures. | ||
|
||
## Validating Secp256r1 Signatures | ||
|
||
The [`P256Verify` precompile](https://docs.zksync.io/build/developer-reference/era-contracts/elliptic-curve-precompiles#p256) | ||
is available to validate secp256r1 signatures. | ||
|
||
```solidity | ||
address constant P256 = 0x0000000000000000000000000000000000000100; | ||
/** | ||
* input[ 0: 32] = signed data hash | ||
* input[ 32: 64] = signature r | ||
* input[ 64: 96] = signature s | ||
* input[ 96:128] = public key x | ||
* input[128:160] = public key y | ||
*/ | ||
bytes memory input = abi.encodePacked( | ||
hash, | ||
rs[0], | ||
rs[1], | ||
pubKey[0], | ||
pubKey[1] | ||
); | ||
(bool __, bytes memory output) = P256.staticcall(input); | ||
// if signature is valid: | ||
// output = 0x0000000000000000000000000000000000000000000000000000000000000001 | ||
// if signature is NOT valid: | ||
// output.length == 0 | ||
``` | ||
|
||
You can find a more in-depth example showing how | ||
it can be used in the ["Signing Transactions with WebAuthn"](https://code.zksync.io/tutorials/signing-transactions-with-webauthn) tutorial. |
File renamed without changes.
81 changes: 0 additions & 81 deletions
81
....build/65.developer-reference/40.account-abstraction/60.signature-validation.md
This file was deleted.
Oops, something went wrong.