From 0f10ad29e36543829c91e6e9fd1e9b7fc1b10dd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yago=20P=C3=A9rez=20V=C3=A1zquez?= Date: Mon, 2 Oct 2023 12:07:20 +0200 Subject: [PATCH] Add playground --- playground/config/run.ts | 3 +- playground/protocol-kit/eip1271.ts | 76 ++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 playground/protocol-kit/eip1271.ts diff --git a/playground/config/run.ts b/playground/config/run.ts index 7b546f34a..ad9ceaa0b 100644 --- a/playground/config/run.ts +++ b/playground/config/run.ts @@ -4,7 +4,8 @@ const playInput = process.argv[2] const playgroundProtocolKitPaths = { 'deploy-safe': 'protocol-kit/deploy-safe', - 'generate-safe-address': 'protocol-kit/generate-safe-address' + 'generate-safe-address': 'protocol-kit/generate-safe-address', + eip1271: 'protocol-kit/eip1271' } const playgroundApiKitPaths = { 'propose-transaction': 'api-kit/propose-transaction', diff --git a/playground/protocol-kit/eip1271.ts b/playground/protocol-kit/eip1271.ts new file mode 100644 index 000000000..5210bf796 --- /dev/null +++ b/playground/protocol-kit/eip1271.ts @@ -0,0 +1,76 @@ +import Safe from '@safe-global/protocol-kit' +import { EthersAdapter } from '@safe-global/protocol-kit' +import { ethers } from 'ethers' + +// This file can be used to play around with the Safe Core SDK + +interface Config { + RPC_URL: string + OWNER1_PRIVATE_KEY: string + OWNER2_PRIVATE_KEY: string + OWNER3_PRIVATE_KEY: string + SAFE_2_3_ADDRESS: string +} + +const config: Config = { + RPC_URL: '', + // Create a Safe 2/3 with 3 owners and fill this info + OWNER1_PRIVATE_KEY: '', + OWNER2_PRIVATE_KEY: '', + OWNER3_PRIVATE_KEY: '', + SAFE_2_3_ADDRESS: '' +} + +async function main() { + const provider = new ethers.providers.JsonRpcProvider(config.RPC_URL) + const signer1 = new ethers.Wallet(config.OWNER1_PRIVATE_KEY, provider) + const signer2 = new ethers.Wallet(config.OWNER2_PRIVATE_KEY, provider) + + // Create safeSdk instances + const safeSdk1 = await Safe.create({ + ethAdapter: new EthersAdapter({ + ethers, + signerOrProvider: signer1 + }), + safeAddress: config.SAFE_2_3_ADDRESS + }) + + const safeSdk2 = await Safe.create({ + ethAdapter: new EthersAdapter({ + ethers, + signerOrProvider: signer2 + }), + safeAddress: config.SAFE_2_3_ADDRESS + }) + + const MESSAGE_TO_SIGN = 'I am the owner of this Safe account' + + const chainId: number = await safeSdk1.getChainId() + const safeVersion = await safeSdk1.getContractVersion() + + const messageHash = ethers.utils.hashMessage(MESSAGE_TO_SIGN) + const safeMessageHash = await safeSdk1.signatures.getMessageHash(messageHash) + + const ethSignSig = await safeSdk1.signatures.signEIP191Message(safeMessageHash) + const typedDataSig = await safeSdk2.signatures.signEIP712Message({ + safeAddress: config.SAFE_2_3_ADDRESS, + safeVersion, + chainId, + data: messageHash + }) + + // Validate the signature sending the Safe message hash and the concatenated signatures + const isValid = await safeSdk1.signatures.isValidSignature(messageHash, [ + typedDataSig, + ethSignSig + ]) + + console.log('Message: ', MESSAGE_TO_SIGN) + console.log('Message Hash: ', messageHash) + console.log('Safe Message Hash: ', safeMessageHash) + console.log('Signatures: ', ethSignSig, typedDataSig) + + console.log(`The signature is ${isValid ? 'valid' : 'invalid'}`) +} + +main()