Safe 백서

bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 12쪽 중 7쪽

sage(TYPED_MESSAGE) _ 41 _ 41 // Connect OWNER_4_ADDRESS and SAFE_2_3_ADDRESS _ 41 protocolKit = await protocolKit.connect({ _ 41 provider: RPC_URL, _ 41 signer: OWNER_4_PRIVATE_KEY, _ 41 safeAddress: SAFE_2_3_ADDRESS _ 41 }) _ 41 _ 41 // Sign the messageSafe2_3 with OWNER_4_ADDRESS _ 41 // After this, the messageSafe2_3 contains the signature from OWNER_4_ADDRESS _ 41 messageSafe2_3 = await protocolKit.signMessage( _ 41 messageSafe2_3, _ 41 SigningMethod.SAFE_SIGNATURE, _ 41 SAFE_3_4_ADDRESS // Parent Safe address _ 41 ) _ 41 _ 41 // Connect OWNER_5_ADDRESS _ 41 protocolKit = await protocolKit.connect({ _ 41 provider: RPC_URL, _ 41 signer: OWNER_5_PRIVATE_KEY _ 41 }) _ 41 _ 41 // Sign the messageSafe2_3 with OWNER_5_ADDRESS _ 41 // After this, the messageSafe2_3 contains the signature from OWNER_5_ADDRESS _ 41 messageSafe2_3 = await protocolKit.signMessage( _ 41 messageSafe2_3, _ 41 SigningMethod.SAFE_SIGNATURE, _ 41 SAFE_3_4_ADDRESS // Parent Safe address _ 41 ) _ 41 _ 41 // Build the contract signature of SAFE_2_3_ADDRESS _ 41 const signatureSafe2_3 = await buildContractSignature( _ 41 Array.from(messageSafe2_3.signatures.values()), _ 41 SAFE_2_3_ADDRESS _ 41 ) _ 41 _ 41 // Add the signatureSafe2_3 to safeMessage _ 41 // After this, the safeMessage contains the signature from OWNER_1_ADDRESS, OWNER_2_ADDRESS, SAFE_1_1_ADDRESS and SAFE_2_3_ADDRESS _ 41 safeMessage.addSignature(signatureSafe2_3) After following all the steps above, the safeMessage now contains all the signatures from the owners of the Safe. Publish the signed message As messages aren't stored in the blockchain, we must make them public and available to others by storing them elsewhere. Safe messages can be stored on-chain and off-chain: Off-chain : Messages are stored in the Safe Transaction Service. This is the default option and doesn't require any on-chain interaction. On-chain : Messages are stored (opens in a new tab) in the Safe contract. Safe supports signing EIP-191 (opens in a new tab) messages and EIP-712 (opens in a new tab) typed data messages all together with off-chain EIP-1271 (opens in a new tab) validation for signatures. Off-chain messages To use off-chain messages, we need to use the functionality from this guide and call the Safe Transaction Service API to store the messages and signatures. We mentioned the utility of storing messages in the contract. Off-chain messages have the same purpose, but they're stored in the Safe Transaction Service. It stores the messages and signatures in a database. It's a centralized service, but it's open-source and can be deployed by anyone. The Safe Transaction Service is used by Safe{Wallet} to store messages and signatures by default. Propose the message To store a new message, we need to call the addMessage from the API Kit, passing the Safe address, an object with the message, and a signature from one owner. _ 16 // Get the signature from OWNER_1_ADDRESS _ 16 const signatureOwner1 = safeMessage.getSignature(OWNER_1_PRIVATE_KEY) as EthSafeSignature _ 16 _ 16 // Instantiate the API Kit _ 16 // Use the chainId where you have the Safe account deployed _ 16 // How to get an Api key => http://docs.safe.global/core-api/how-to-use-api-keys _ 16 const apiKit = new SafeApiKit({ _ 16 chainId, _ 16 apiKey: 'YOUR_API_KEY' _ 16 }) _ 16 _ 16 // Propose the message _ 16 apiKit.addMessage(SAFE_3_4_ADDRESS, { _ 16 message: TYPED_MESSAGE, // or STRING_MESSAGE _ 16 signature: buildSignatureBytes([signatureOwner1]) _ 16 }) The message is now publicly available in the Safe Transaction Service with the signature of the owner who submitted it. Confirm the message To add the signatures from the remaining owners, we need to call the addMessageSignature , passing the safeMessageHash and a signature from the owner. _ 25 // Get the safeMessageHash _ 25 const safeMessageHash = await protocolKit.getSafeMessageHash( _ 25 hashSafeMessage(TYPED_MESSAGE) // or STRING_MESSAGE _ 25 ) _ 25 _ 25 // Get the signature from OWNER_2_ADDRESS _ 25 const signatureOwner2 = safeMessage.getSignature(OWNER_2_ADDRESS) as EthSafeSignature _ 25 _ 25 // Add signature from OWNER_2_ADDRESS _ 25 await apiKit.addMessageSignature( _ 25 safeMessageHash, _ 25 buildSignatureBytes([signatureOwner2]) _ 25 ) _ 25 _ 25 // Add signature from the owner SAFE_1_1_ADDRESS _ 25 await apiKit.addMessageSignature( _ 25 safeMessageHash, _ 25 buildSignatureBytes([signatureSafe1_1]) _ 25 ) _ 25 _ 25 // Add signature from the owner SAFE_2_3_ADDRESS _ 25 await apiKit.addMessageSignature( _ 25 safeMessageHash, _ 25 buildSignatureBytes([signatureSafe2_3]) _ 25 ) At this point, the message stored in the Safe Transaction Service contains all the required signatures from the owners of the Safe. The getMessage method returns the status of a message. _ 10 const confirmedMessage = await apiKit.getMessage(safeMessageHash) Safe{Wallet} (opens in a new tab) exposes to its users the list of off-chain messages signed by a Safe account. _ 10 https://app.safe.global/transactions/messages?safe=<NETWORK_PREFIX>:<SAFE_ADDRESS> On-chain messages Storing messages on-chain is less efficient than doing it off-chain because it requires executing a transaction to store the message hash in the contract, resulting in additional gas costs. To do this on-chain, we use the SignMessageLib contract. _ 10 // Get the contract with the correct version _ 10 const signMessageLibContract = await getSignMessageLibContract({ _ 10 safeVersion: '1.4.1' _ 10 }) We need to calculate the messageHash , encode the call to the signMessage function in the SignMessageLib contract and create the transaction that will store the message hash in that contract. _ 13 const messageHash = hashSafeMessage(MESSAGE) _ 13 const txData = signMessageLibContract.encode('signMessage', [messageHash]) _ 13 _ 13 const safeTransactionData: SafeTransactionDataPartial = { _ 13 to: signMessageLibContract.address, _ 13 value: '0', _ 13 data: txData, _ 13 operation: OperationType.DelegateCall _ 13 } _ 13 _ 13 const signMessageTx = await protocolKit.createTransaction({ _ 13 transactions: [safeTransactionData] _ 13 }) Once the transaction object is instantiated, the owners must sign and execute it. _ 10 // Collect the signatures using the signTransaction method _ 10 _ 10 // Execute the transaction to store the messageHash _ 10 await protocolKit.executeTransaction(signMessageTx) Once the transaction is executed, the message hash will be stored in the contract. Validate the signature On-chain When a message is stored on-chain, the isValidSignature method in the Protocol Kit needs to be called with the parameters messageHash and 0x . The method will check the stored hashes in the Safe contract to validate the signature. _ 10 import { hashSafeMessage } from '@safe-global/protocol-kit' _ 10 _ 10 const messageHash = hashSafeMessage(MESSAGE) _ 10 _ 10 const isValid = await protocolKit.isValidSignature(messageHash, '0x') Off-chain When a message is stored off-chain, the isValidSignature method in the Protocol Kit must be called with the messageHash and the encodedSignatures parameters. The method will check the isValidSignature function defined in the CompatibilityFallbackHandler contract (opens in a new tab) to validate the signature. _ 10 const encodedSignatures = safeMessage.encodedSignatures() _ 10 _ 10 const isValid = await protocolKit.isValidSignature( _ 10 messageHash, _ 10 encodedSignatures _ 10 ) Transactions Migrate to v1 Was this page helpful? Report issue ## /sdk/protocol-kit/guides/migrate-to-v1 SDK Protocol Kit Guides Migrate to v1 Migrate to v1 This guide references the major changes between safe-core-sdk and protocol-kit v1 to help those migrating an existing application. Note: Follow this guide before migrating to protocol-kit v2. You can remove @safe-global/safe-core-sdk from your package.json after completing this guide. Adding the new dependency To add the Protocol Kit to your project, run the following: _ 10 yarn add @safe-global/protocol-kit@1.3.0 If you use the types library, you will need to update to v2.3.0: _ 10 yarn add @safe-global/safe-core-sdk-types@2.3.0 EthAdapter EthersAdapter (safe-ethers-lib) EthersAdapter isn't in a separate package anymore. Now, it's provided inside the protocol-kit package. protocol-kit v1 only supports ethers v5 _ 10 // old _ 10 import EthersAdapter from '@safe-global/safe-ethers-lib' _ 10 _ 10 // new _ 10 import { EthersAdapter } from '@safe-global/protocol-kit' After this change, you can remove @safe-global/safe-ethers-lib from your package.json . Web3Adapter (safe-web3-lib) Web3Adapter isn't in a separate package anymore. Now, it's part of the protocol-kit package. Note: protocol-kit v1 only supports Web3.js v1. _ 10 // old _ 10 import Web3Adapter from '@safe-global/safe-web3-lib' _ 10 _ 10 // new _ 10 import { Web3Adapter } from '@safe-global/protocol-kit' After this change, you can remove @safe-global/safe-web3-lib from your package.json . Type changes Type changes are affecting the web3 and ethers adapter libraries. getSafeContract , getMultisendContract , getMultisendCallOnlyContract , getCompatibilityFallbackHandlerContract , getSafeProxyFactoryContract , getSignMessageLibContract and getCreateCallContract don't need the chainId parameter anymore, they will use the chain set on the provider. Also, they return a Promise now. estimateGas now returns a string instead of a number . safeFactory.deploySafe() SafeDeploymentConfig was simplified. If you were using a saltNonce you should set it like this: _ 16 // old _ 16 const safeAccountConfig: SafeAccountConfig = { _ 16 ... _ 16 } _ 16 const safeDeploymentConfig: SafeDeploymentConfig = { saltNonce } _ 16 _ 16 const safeSdk = await safeFactory.deploySafe({ safeAccountConfig, safeDeploymentConfig }) _ 16 _ 16 // new _ 16 const safeAccountConfig: SafeAccountConfig = { _ 16 ... _ 16 } _ 16 _ 16 const saltNonce = '<YOUR_CUSTOM_VALUE>' _ 16 _ 16 const protocolKit = await safeFactory.deploySafe({ safeAccountConfig, saltNonce }) getAddress() The getAddress() method now returns a Promise . _ 10 // old _ 10 const safeAddress = safeSdk.getAddress() _ 10 _ 10 // new _ 10 const safeAddress = await protocolKit.getAddress() General type changes If you set safeTxGas , baseGas , or gasPrice , you must use the type string instead of number . Messages Migrate to v2 Was this page helpful? Report issue ## /sdk/protocol-kit/guides/migrate-to-v2 SDK Protocol Kit Guides Migrate to v2 Migrate to v2 This guide references the major changes between v1 and v2 to help those migrating an existing app. Note: When upgrading to protocol-kit v2, it's necessary to upgrade to safe-core-sdk-types v3. Mastercopy to Singleton To avoid confusion between terms used as synonyms, we aligned all our code to use the word singleton . Rename isL1SafeMasterCopy to isL1SafeSingleton _ 10 // old: _ 10 SafeFactory.create({ ethAdapter, isL1SafeMasterCopy: true }) _ 10 _ 10 // new: _ 10 SafeFactory.create({ ethAdapter, isL1SafeSingleton: true }) Ethers v6 From protocolKit v2 , EthersAdapter will only be compatible with ethers.js v6. If you still need to use v5, we recommend you keep protocolKit v1 , but we encourage you to migrate to the latest version when you can. Protocol Kit createTransaction() accepts only transaction array In protocolKit v1 , the createTransaction() method accepted either an object or an array as a parameter. To avoid confusion, we changed it to accept only an array. Here is a migration example: _ 24 // old: _ 24 const safeTransactionData = { _ 24 to: '', _ 24 data: '', _ 24 value: '', _ 24 nonce: '', _ 24 safeTxGas: '' _ 24 } _ 24 const safeTransaction = protocolKit.createTransaction({ safeTransactionData }) _ 24 _ 24 // new: _ 24 const safeTransactionData = { _ 24 to: '', _ 24 data: '', _ 24 value: '' _ 24 } _ 24 const options = { _ 24 nonce: '', _ 24 safeTxGas: '' _ 24 } _ 24 const safeTransaction = protocolKit.createTransaction({ _ 24 transactions: [safeTransactionData], _ 24 options _ 24 }) Migrate to v1 Migrate to v3 Was this page helpful? Report issue ## /sdk/protocol-kit/guides/migrate-to-v3 SDK Protocol Kit Guides Migrate to v3 Migrate to v3 This guide references the major changes between v2 and v3 to help those migrating an existing app. Note: When upgrading to protocol-kit v3, it's necessary to upgrade to safe-core-sdk-types v4. The signTransactionHash() was renamed signHash() The signTransactionHash() method was renamed to signHash() to align with the method's purpose. The method is not strictly for transactions, as the parameter is a hash, so the new name is more accurate. _ 10 // old: _ 10 protocolKit.signTransactionHash(safeTxHash) _ 10 _ 10 // new: _ 10 protocolKit.signHash(safeTxHash) Type changes The SafeTransactionEIP712Args was renamed SafeEIP712Args as the EIP-712 is not exclusive for transactions. Migrate to v2 Migrate to v4 Was this page helpful? Report issue ## /sdk/protocol-kit/guides/migrate-to-v4 SDK Protocol Kit Guides Migrate to v4 Migrate to v4 This guide references the major changes between v3 and v4 to help those migrating an existing app. Note: When upgrading to protocol-kit v4, it's necessary to upgrade to types-kit v1. The create() method was renamed init() in the SafeFactory and Safe classes We renamed the create() method to init() to better reflect the method's purpose. The term create() was misleading, suggesting a new Safe account would be created and deployed. However, this method only initializes the Safe class, so init() is a more accurate and descriptive name. _ 10 // old _ 10 const protocolKit = await Safe.create({ ... }) _ 10 const safeFactory = await SafeFactory.create({ ... }) _ 10 _ 10 // new _ 10 const protocolKit = await Safe.init({ ... }) _ 10 const safeFactory = await SafeFactory.init({ ... }) Remove the adapters We have removed the concept of adapters from the protocol-kit to simplify the library. Instead of using specific library adapters, we use now an internal SafeProvider object to interact with the Safe. This SafeProvider will be created using: An Ethereum provider, an EIP-1193 (opens in a new tab) compatible provider, or an RPC URL. An optional address of the signer that is connected to the provider or a private key. If not provided, the first account of the provider ( eth_accounts ) will be selected as the signer. The EthAdapter interface, the EthersAdapter class, and the Web3Adapter class are no longer available. Similarly, EthersAdapterConfig and Web3AdapterConfig were removed as well. _ 24 // old _ 24 const ethAdapter = new EthersAdapter({ ethers, signerOrProvider }) _ 24 // const ethAdapter = new Web3Adapter({ web3, signerAddress }) _ 24 await Safe.create({ _ 24 ethAdapter, _ 24 safeAddress: '0xSafeAddress' _ 24 ... _ 24 }) _ 24 _ 24 // new _ 24 await Safe.init({ _ 24 provider: window.ethereum, // Or any compatible EIP-1193 provider _ 24 signer: '0xSignerAddressOrPrivateKey', // Signer address or private key _ 24 safeAddress: '0xSafeAddress' _ 24 ... _ 24 }) _ 24 _ 24 // ...or... _ 24 await Safe.init({ _ 24 provider: 'http://rpc.url', // Or websocket _ 24 signer: '0xPrivateKey' // Signer private key _ 24 safeAddress: '0xSafeAddress' _ 24 ... _ 24 }) EthersTransactionOptions and Web3TransactionOptions types are now TransactionOptions Together with the adapters, we also removed the specific transaction options objects for each library, leaving just a single TransactionOptions type. We removed the gas property from the TransactionOptions object as it was a specific property for the web3.js library. Now, you should use the gasLimit property instead. EthersTransactionResult and Web3TransactionResult types are now TransactionResult Together with the adapters, we also removed the specific transaction result objects for each library, leaving just a single TransactionResult type. Contract classes suffixed with Ethers and Web3 All the contract classes that were suffixed with Ethers or Web3 were renamed to remove the suffix. _ 10 SafeBaseContractEthers, SafeBaseContractWeb3 -> SafeBaseContract _ 10 MultiSendBaseContractEthers, MultiSendBaseContractWeb3 -> MultiSendBaseContract _ 10 MultiSendCallOnlyBaseContractEthers, MultiSendCallOnlyBaseContractWeb3 -> MultiSendCallOnlyBaseContract _ 10 SafeProxyFactoryBaseContractEthers, SafeProxyFactoryBaseContractWeb3 -> SafeProxyFactoryBaseContract _ 10 SignMessageLibBaseContractEthers, SignMessageLibBaseContractWeb3 -> SignMessageLibBaseContract _ 10 CreateCallBaseContractEthers, CreateCallBaseContractWeb3 -> CreateCallBaseContract Migrate to v3 Migrate to v5 Was this page helpful? Report issue ## /sdk/protocol-kit/guides/migrate-to-v5 SDK Protocol Kit Guides Migrate to v5 Migrate to v5 This guide references the major changes between v4 and v5 to help those migrating an existing app. Removing SafeFactory class The SafeFactory class, previously used for deploying Safes, has been removed. The functionality to deploy Safes is now directly available in the Safe class through the new createSafeDeploymentTransaction method. Old Method Using SafeFactory _ 24 // old v4 code _ 24 import { SafeFactory, SafeAccountConfig } from '@safe-global/protocol-kit' _ 24 _ 24 const safeFactory = await SafeFactory.init({ _ 24 provider, _ 24 signer, _ 24 safeVersion // Optional _ 24 }) _ 24 _ 24 const safeAccountConfig: SafeAccountConfig = { _ 24 owners: ['0x...', '0x...', '0x...'], _ 24 threshold: 2 _ 24 } _ 24 _ 24 const protocolKit = await safeFactory.deploySafe({ _ 24 safeAccountConfig, _ 24 saltNonce // Optional _ 24 }) _ 24 _ 24 // Confirm the Safe is deployed and fetch properties _ 24 console.log('Is Safe deployed:', await protocolKit.isSafeDeployed()) _ 24 console.log('Safe Address:', await protocolKit.getAddress()) _ 24 console.log('Safe Owners:', await protocolKit.getOwners()) _ 24 console.log('Safe Threshold:', await protocolKit.getThreshold()) New Method Using Safe class _ 45 // new v5 code _ 45 import Safe, { PredictedSafeProps } from '@safe-global/protocol-kit' _ 45 _ 45 const predictedSafe: PredictedSafeProps = { _ 45 safeAccountConfig: { _ 45 owners: ['0x...', '0x...', '0x...'], _ 45 threshold: 2 _ 45 }, _ 45 safeDeploymentConfig: { _ 45 saltNonce, // Optional _ 45 safeVersion // Optional _ 45 } _ 45 } _ 45 _ 45 let protocolKit = await Safe.init({ _ 45 provider, _ 45 signer, _ 45 predictedSafe _ 45 }) _ 45 _ 45 // you can predict the address of your Safe if the Safe version is `v1.3.0` or above _ 45 const safeAddress = await protocolKit.getAddress() _ 45 _ 45 const deploymentTransaction = await protocolKit.createSafeDeploymentTransaction() _ 45 _ 45 // Execute this transaction using the integrated signer or your preferred external Ethereum client _ 45 const client = await protocolKit.getSafeProvider().getExternalSigner() _ 45 _ 45 const txHash = await client.sendTransaction({ _ 45 to: deploymentTransaction.to, _ 45 value: BigInt(deploymentTransaction.value), _ 45 data: deploymentTransaction.data as `0x${string}`, _ 45 chain: sepolia _ 45 }) _ 45 _ 45 const txReceipt = await client.waitForTransactionReceipt({ hash: txHash }) _ 45 _ 45 // Reconnect to the newly deployed Safe using the protocol-kit _ 45 protocolKit = await protocolKit.connect({ safeAddress }) _ 45 _ 45 // Confirm the Safe is deployed and fetch properties _ 45 console.log('Is Safe deployed:', await protocolKit.isSafeDeployed()) _ 45 console.log('Safe Address:', await protocolKit.getAddress()) _ 45 console.log('Safe Owners:', await protocolKit.getOwners()) _ 45 console.log('Safe Threshold:', await protocolKit.getThreshold()) Predict the Safe Address You can predict the address of a Safe account before its deployment, as long as you are using Safe v1.3.0 or g
프로젝트로 돌아가기