-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
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
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,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() |