Skip to content

Commit

Permalink
Add playground
Browse files Browse the repository at this point in the history
  • Loading branch information
yagopv committed Oct 2, 2023
1 parent bbfe4c9 commit 0f10ad2
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 1 deletion.
3 changes: 2 additions & 1 deletion playground/config/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
76 changes: 76 additions & 0 deletions playground/protocol-kit/eip1271.ts
Original file line number Diff line number Diff line change
@@ -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: '<RPC_URL>',
// Create a Safe 2/3 with 3 owners and fill this info
OWNER1_PRIVATE_KEY: '<OWNER1_PRIVATE_KEY>',
OWNER2_PRIVATE_KEY: '<OWNER2_PRIVATE_KEY>',
OWNER3_PRIVATE_KEY: '<OWNER3_PRIVATE_KEY>',
SAFE_2_3_ADDRESS: '<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()

0 comments on commit 0f10ad2

Please sign in to comment.