diff --git a/docs/sdk/authentication/auth-sig.md b/docs/sdk/authentication/auth-sig.md index feec4c24..ac105937 100644 --- a/docs/sdk/authentication/auth-sig.md +++ b/docs/sdk/authentication/auth-sig.md @@ -26,7 +26,7 @@ In the `AuthSig` data structure: - `signedMessage` is the original message that was signed - `address` is the public key address that was used to create the signature -You can refer to the `AuthSig` type definition in the [Lit JS SDK Latest Version](https://js-sdk.litprotocol.com/interfaces/types_src.AuthSig.html). +You can refer to the `AuthSig` type definition in the [Lit JS SDK V2](https://js-sdk.litprotocol.com/interfaces/types_src.AuthSig.html). ## Obtaining an `AuthSig` in the browser @@ -75,16 +75,56 @@ Following the same data structure as above, you can format your smart contract ` - `sig` is the actual hex-encoded signature - `derivedVia` must be "EIP1271" to inform the nodes that this `AuthSig` is for smart contracts -- `signedMessage` is any string that you want to pass to the `isValidSignature(bytes32 _hash, bytes memory _signature)` as the `_hash` argument +- `signedMessage` is any string that you want to pass to the `isValidSignature(bytes32 _hash, bytes memory _signature)` as the `_hash` argument. - `address` is the address of the smart contract +You can present the smart contract `AuthSig` object to the Lit Nodes just like any other `AuthSig`. +Check out this [**React** project](https://replit.com/@lit/Smart-Contract-Authsig-EIP1271#smart-contract-authsig/src/App.js) for an example of how to generate and use a smart contract `AuthSig`. + :::note -The smart contract must implement the `isValidSignature(bytes32 _hash, bytes memory _signature)` function since the Lit Nodes will call this function to validate the `AuthSig`. Refer to the [EIP-1271](https://eips.ethereum.org/EIPS/eip-1271) docs to understand the `isValidSignature` function. +The smart contract must implement the `isValidSignature(bytes32 _hash, bytes memory _signature)` function since the Lit Nodes will call this function to validate the `AuthSig`. Refer to the [EIP-1271](https://eips.ethereum.org/EIPS/eip-1271) docs to understand the `isValidSignature` function. The current behavior is having an issue as of 16/11/23. This is because of a bug detailed below in the consideration. ::: -You can present the smart contract `AuthSig` object to the Lit Nodes just like any other `AuthSig`. +#### Current Behavior with `signedMessage` +The current implementation involves a specific handling of the `signedMessage` parameter that may not be immediately apparent. This has led to some confusion and difficulty in implementation. The key points are: + +Encoding of `signedMessage`: The `signedMessage` should be a `string` without modifications. It's not meant to be in _hexadecimal_ format or any other encoding. + +**Backend Processing:** + +- The backend processes the `signedMessage` by first converting it to `bytes`, then encoding it in hex without the `0x` prefix. +- This `hex` string is then passed to the `keccak256` hash function. + +The issue arises because `keccak256` interprets this as a `string`, not as _hexadecimal_ bytes. + + +**Solution to signedMessage Issue** +To correctly process the `signedMessage`, follow these steps: + +_Correct Encoding:_ + +Convert the `signedMessage` to `bytes`. +Then convert these `bytes` to a `hex` string, then remove `0x` prefix and convert back to `bytes`. +Finally, apply `keccak256` to these bytes. + +_Example Implementation:_ +```js +const message = "example message"; +const hexMessage = toBytes(toHex(toBytes(message)).slice(2).toLowerCase()); +const hashBytes = keccak256(hexMessage); + +// ERC-1271 Signing Logic +const signature = .... + +authSig = { + sig: signature, // 0x00 + derivedVia: "EIP1271", + signedMessage: "test message", + address: "0x..." // abstracted wallet address +}; +``` + -Check out this [**React** project](https://replit.com/@lit/Smart-Contract-Authsig-EIP1271#smart-contract-authsig/src/App.js) for an example of how to generate and use a smart contract `AuthSig`. ### Clearing Local Storage @@ -101,15 +141,12 @@ const siwe = require('siwe'); async function main() { // Initialize LitNodeClient - const litNodeClient = new LitJsSdk.LitNodeClientNodeJs({ - alertWhenUnauthorized: false, - litNetwork: 'cayenne', - }); + const litNodeClient = new LitJsSdk.LitNodeClientNodeJs(); await litNodeClient.connect(); // Initialize the signer const wallet = new ethers.Wallet(''); - const address = ethers.getAddress(await wallet.getAddress()); + const address = ethers.utils.getAddress(await wallet.getAddress()); // Craft the SIWE message const domain = 'localhost'; @@ -122,7 +159,7 @@ async function main() { statement, uri: origin, version: '1', - chainId: 1, + chainId: '1', }); const messageToSign = siweMessage.prepareMessage(); diff --git a/versioned_docs/version-2.0/sdk/explanation/authentication/authSig.md b/versioned_docs/version-2.0/sdk/explanation/authentication/authSig.md index 646ef2c5..ac105937 100644 --- a/versioned_docs/version-2.0/sdk/explanation/authentication/authSig.md +++ b/versioned_docs/version-2.0/sdk/explanation/authentication/authSig.md @@ -75,16 +75,56 @@ Following the same data structure as above, you can format your smart contract ` - `sig` is the actual hex-encoded signature - `derivedVia` must be "EIP1271" to inform the nodes that this `AuthSig` is for smart contracts -- `signedMessage` is any string that you want to pass to the `isValidSignature(bytes32 _hash, bytes memory _signature)` as the `_hash` argument +- `signedMessage` is any string that you want to pass to the `isValidSignature(bytes32 _hash, bytes memory _signature)` as the `_hash` argument. - `address` is the address of the smart contract +You can present the smart contract `AuthSig` object to the Lit Nodes just like any other `AuthSig`. +Check out this [**React** project](https://replit.com/@lit/Smart-Contract-Authsig-EIP1271#smart-contract-authsig/src/App.js) for an example of how to generate and use a smart contract `AuthSig`. + :::note -The smart contract must implement the `isValidSignature(bytes32 _hash, bytes memory _signature)` function since the Lit Nodes will call this function to validate the `AuthSig`. Refer to the [EIP-1271](https://eips.ethereum.org/EIPS/eip-1271) docs to understand the `isValidSignature` function. +The smart contract must implement the `isValidSignature(bytes32 _hash, bytes memory _signature)` function since the Lit Nodes will call this function to validate the `AuthSig`. Refer to the [EIP-1271](https://eips.ethereum.org/EIPS/eip-1271) docs to understand the `isValidSignature` function. The current behavior is having an issue as of 16/11/23. This is because of a bug detailed below in the consideration. ::: -You can present the smart contract `AuthSig` object to the Lit Nodes just like any other `AuthSig`. +#### Current Behavior with `signedMessage` +The current implementation involves a specific handling of the `signedMessage` parameter that may not be immediately apparent. This has led to some confusion and difficulty in implementation. The key points are: + +Encoding of `signedMessage`: The `signedMessage` should be a `string` without modifications. It's not meant to be in _hexadecimal_ format or any other encoding. + +**Backend Processing:** + +- The backend processes the `signedMessage` by first converting it to `bytes`, then encoding it in hex without the `0x` prefix. +- This `hex` string is then passed to the `keccak256` hash function. + +The issue arises because `keccak256` interprets this as a `string`, not as _hexadecimal_ bytes. + + +**Solution to signedMessage Issue** +To correctly process the `signedMessage`, follow these steps: + +_Correct Encoding:_ + +Convert the `signedMessage` to `bytes`. +Then convert these `bytes` to a `hex` string, then remove `0x` prefix and convert back to `bytes`. +Finally, apply `keccak256` to these bytes. + +_Example Implementation:_ +```js +const message = "example message"; +const hexMessage = toBytes(toHex(toBytes(message)).slice(2).toLowerCase()); +const hashBytes = keccak256(hexMessage); + +// ERC-1271 Signing Logic +const signature = .... + +authSig = { + sig: signature, // 0x00 + derivedVia: "EIP1271", + signedMessage: "test message", + address: "0x..." // abstracted wallet address +}; +``` + -Check out this [**React** project](https://replit.com/@lit/Smart-Contract-Authsig-EIP1271#smart-contract-authsig/src/App.js) for an example of how to generate and use a smart contract `AuthSig`. ### Clearing Local Storage