Helper library to sign any EIP191 data.
For more information see Documentation.
The @lukso/eip191-signer.js
package is used to sign messages according to the EIP191 standard.
If you want to sign with the version 0x45, then use the function signEthereumSignedMessage.
If you want to sign with the version 0x00, then use the function signDataWithIntendedValidator.
npm install @lukso/eip191-signer.js
import { EIP191Signer } from '@lukso/eip191-signer.js';
const eip191Signer = new EIP191Signer();
The LSP6-KeyManager standard uses version 0 for signed messages. Therefore, it must use signDataWithIntendedValidator
.
// Web3.js Example
const chainId = await web3.eth.getChainId();
// Indicate that the signature is always valid
const validityTimestamp = 0;
let encodedMessage = web3.utils.encodePacked(
{ value: LSP25_VERSION, type: 'uint256' }, // //LSP25 Version = 25;
{ value: chainId, type: 'uint256' },
{ value: nonce, type: 'uint256' },
{ value: validityTimestamp, type: 'uint256' },
{ value: msgValue, type: 'uint256' },
{ value: abiPayload, type: 'bytes' },
);
let eip191Signer = new EIP191Signer();
let { signature } = await eip191Signer.signDataWithIntendedValidator(
keyManagerAddress, // intended validator is the address of the Key Manager
encodedMessage, //
controllerPrivateKey,
);
// ethers.js Example
const network = await provider.getNetwork();
const chainId = network.chainId;
// The block.timestamp(s) which the signature is valid in between
const startingTimestamp = ....;
const endingTimestamp = ....;
const validityTimestamp = ethers.utils.hexConcat([
ethers.utils.zeroPad(ethers.utils.hexlify(startingTimestamp), 16),
ethers.utils.zeroPad(ethers.utils.hexlify(endingTimestamp), 16),
]);
let encodedMessage = ethers.utils.solidityPack(
["uint256", "uint256", "uint256", "uint256", "uint256", "bytes"],
[
LSP25_VERSION, // LSP25_VERSION = 25;
chainId,
nonce,
validityTimestamp
msgValue,
abiPayload
]
);
let eip191Signer = new EIP191Signer();
let { signature } = await eip191Signer.signDataWithIntendedValidator(
keyManagerAddress, // intended validator is the address of the Key Manager
encodedMessage, //
controllerPrivateKey,
);
eip191Signer.hashEthereumSignedMessage(message);
Hashes the given message with the version 0x45.
The message will be enveloped as follows: '\x19' + '\x45' + 'thereum Signed Message:\n' + messageBytes.length + message
and hashed using keccak256.
eip191Signer.hashDataWithIntendedValidator(validatorAddress, message);
Hashes the given message with the version 0x00.
The message will be enveloped as follows: '\x19' + '\x00' + validatorAddress + message
and hashed using keccak256.
eip191Signer.signEthereumSignedMessage(message, signingKey);
This method is for signing a message with the version 0x45.
The message passed as parameter will be wrapped as follows: '\x19' + '\x45' + 'thereum Signed Message:\n' + messageBytes.length + message
.
eip191Signer.signDataWithIntendedValidator(
validatorAddress,
message,
signingKey,
);
This method is for signing a message with the version 0x00.
The message passed as parameter will be wrapped as follows: '\x19' + '\x00' + validatorAddress + message
.
eip191Signer.recover(messageHash, signature);
Recovers the address which was used to sign the given message.
Please check CONTRIBUTING.md
.
eip191-signer.js is Apache 2.0 licensed.
Magali Morin 💻 |
Fabian Vogelsteller 🤔 |
Callum Grindle 👀 🧑🏫 |
Hugo Masclet 👀 🧑🏫 |