Safe 백서

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

ctionData] _ 33 }) _ 33 _ 33 // Connect OWNER_4_ADDRESS and the address of SAFE_2_3_ADDRESS _ 33 protocolKit = await protocolKit.connect({ _ 33 provider: RPC_URL, _ 33 signer: OWNER_4_ADDRESS, _ 33 safeAddress: SAFE_2_3_ADDRESS _ 33 }) _ 33 _ 33 // Sign the transactionSafe2_3 with OWNER_4_ADDRESS _ 33 // After this, the transactionSafe2_3 contains the signature from OWNER_4_ADDRESS _ 33 transactionSafe2_3 = await protocolKit.signTransaction( _ 33 transactionSafe2_3, _ 33 SigningMethod.SAFE_SIGNATURE, _ 33 SAFE_3_4_ADDRESS // Parent Safe address _ 33 ) _ 33 _ 33 // Connect OWNER_5_ADDRESS _ 33 protocolKit = await protocolKit.connect({ _ 33 provider: RPC_URL, _ 33 signer: OWNER_5_ADDRESS _ 33 }) _ 33 _ 33 // Sign the transactionSafe2_3 with OWNER_5_ADDRESS _ 33 // After this, the transactionSafe2_3 contains the signature from OWNER_5_ADDRESS _ 33 transactionSafe2_3 = await protocolKit.signTransaction( _ 33 transactionSafe2_3, _ 33 SigningMethod.SAFE_SIGNATURE, _ 33 SAFE_3_4_ADDRESS // Parent Safe address _ 33 ) At this point, the transactionSafe2_3 object should look like this: _ 15 EthSafeTransaction { _ 15 signatures: Map(2) { _ 15 '0xe11ba2b4d45eaed5996cd0823791e0c93114882d' => EthSafeSignature { _ 15 signer: '0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d', _ 15 data: '0xd3e6565e5590641db447277243cf24711dce533cfcaaf3a64415dcb9fa309fbf2de1ae4709c6450752acc0d45e01b67b55379bdf4e3dc32b2d89ad0a60c231d61f', _ 15 isContractSignature: false _ 15 }, _ 15 '0xd03ea8624c8c5987235048901fb614fdca89b117' => EthSafeSignature { _ 15 signer: '0xd03ea8624C8C5987235048901fB614fDcA89b117', _ 15 data: '0x023d1746ed548e90f387a6b8ddba26e6b80a78d5bfbc36e5bfcbfd63e136f8071db6e91c037fa36bde72159138bbb74fc359b35eb515e276a7c0547d5eaa042520', _ 15 isContractSignature: false _ 15 } _ 15 }, _ 15 data: { ... } _ 15 } We now have two signatures from the owners, OWNER_4_ADDRESS and OWNER_5_ADDRESS . Following the same process, we can create the contract signature and examine the result. The signatures.data represents a specific signature. The isContractSignature flag set to false indicates that the signature isn't a smart contract signature but an ECDSA signature instead. To generate a Safe compatible signature, we use the buildContractSignature method, which takes an array of signatures and returns another signature that can be used with Safe accounts. After that, we add the signature from safe1_1 to our initial transaction. _ 10 // Build the contract signature of SAFE_2_3_ADDRESS _ 10 const signatureSafe2_3 = await buildContractSignature( _ 10 Array.from(transactionSafe2_3.signatures.values()), _ 10 SAFE_2_3_ADDRESS _ 10 ) _ 10 _ 10 // Add the signatureSafe2_3 to safeTransaction _ 10 // After this, the safeTransaction contains the signature from OWNER_1_ADDRESS, OWNER_2_ADDRESS, SAFE_1_1_ADDRESS and SAFE_2_3_ADDRESS _ 10 safeTransaction.addSignature(signatureSafe2_3) The signatureSafe2_3 object should look like this: _ 10 0x000000000000000000000000f75D61D6C27a7CC5788E633c1FC130f0F4a62D330000000000000000000000000000000000000000000000000000000000000041000000000000000000000000000000000000000000000000000000000000000082023d1746ed548e90f387a6b8ddba26e6b80a78d5bfbc36e5bfcbfd63e136f8071db6e91c037fa36bde72159138bbb74fc359b35eb515e276a7c0547d5eaa042520d3e6565e5590641db447277243cf24711dce533cfcaaf3a64415dcb9fa309fbf2de1ae4709c6450752acc0d45e01b67b55379bdf4e3dc32b2d89ad0a60c231d61f Type Description Bytes Value Hex Hex string characters 1 0x Verifier Padded address of the contract that implements the EIP-1271 interface to verify the signature. The Safe signer address 32 000000000000000000000000f75D61D6C27a7CC5788E633c1FC130f0F4a62D33 Data position Start position of the signature data (offset relative to the beginning of the signature data). 41 hex is 65 in decimal 32 0000000000000000000000000000000000000000000000000000000000000041 Signature Type 00 for Safe accounts (opens in a new tab) 1 00 Signature Length The length of the signature. 82 hex is 130 in decimal 32 0000000000000000000000000000000000000000000000000000000000000082 Signature Signature bytes that are verified by the signature verifier (130 bytes are represented by 260 characters in an hex string) 130 023d1746ed548e90f387a6b8ddba26e6b80a78d5bfbc36e5bfcbfd63e136f8071db6e91c037fa36bde72159138bbb74fc359b35eb515e276a7c0547d5eaa042520d3e6565e5590641db447277243cf24711dce533cfcaaf3a64415dcb9fa309fbf2de1ae4709c6450752acc0d45e01b67b55379bdf4e3dc32b2d89ad0a60c231d61f The table looks very similar to the previous one, but there are two main differences: The Signature Length value has doubled because safe2_3 needs two signatures. The Signature value is a concatenation of the two regular signatures. After following all the steps above, the safeTransaction now contains all the signatures from the owners of the Safe. The safeTransaction object should look like this: _ 36 EthSafeTransaction { _ 36 signatures: Map(4) { _ 36 '0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1' => EthSafeSignature { _ 36 signer: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1', _ 36 data: '0x969308e2abeda61a0c9c41b3c615012f50dd7456ca76ea39a18e3b975abeb67f275b07810dd59fc928f3f9103e52557c1578c7c5c171ffc983afa5306466b1261f', _ 36 isContractSignature: false _ 36 }, _ 36 '0xffcf8fdee72ac11b5c542428b35eef5769c409f0' => EthSafeSignature { _ 36 signer: '0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0', _ 36 data: '0x4d63c79cf9d743782bc31ad58c1a316020b39839ab164caee7ecac9829f685cc44ec0d066a5dfe646b2ffeeb37575df131daf9c96ced41b8c7c4aea8dc5461801c', _ 36 isContractSignature: false _ 36 }, _ 36 '0x215033cde0619d60b7352348f4598316cc39bc6e' => EthSafeSignature { _ 36 signer: '0x215033cdE0619D60B7352348F4598316Cc39bC6E', _ 36 data: '0x5edb6ffe67dd935d93d07c634970944ba0b096f767b92018ad635e8b28effeea5a1e512f1ad6f886690e0e30a3fae2c8c61d3f83d24d43276acdb3254b92ea5b1f', _ 36 isContractSignature: true _ 36 }, _ 36 '0xf75d61d6c27a7cc5788e633c1fc130f0f4a62d33' => EthSafeSignature { _ 36 signer: '0xf75D61D6C27a7CC5788E633c1FC130f0F4a62D33', _ 36 data: '0x023d1746ed548e90f387a6b8ddba26e6b80a78d5bfbc36e5bfcbfd63e136f8071db6e91c037fa36bde72159138bbb74fc359b35eb515e276a7c0547d5eaa042520d3e6565e5590641db447277243cf24711dce533cfcaaf3a64415dcb9fa309fbf2de1ae4709c6450752acc0d45e01b67b55379bdf4e3dc32b2d89ad0a60c231d61f', _ 36 isContractSignature: true _ 36 } _ 36 }, _ 36 data: { _ 36 to: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1', _ 36 value: '100000000000000000', _ 36 data: '0x', _ 36 operation: 0, _ 36 baseGas: '0', _ 36 gasPrice: '0', _ 36 gasToken: '0x0000000000000000000000000000000000000000', _ 36 refundReceiver: '0x0000000000000000000000000000000000000000', _ 36 nonce: 0, _ 36 safeTxGas: '0' _ 36 } _ 36 } Propose the transaction To store the transactions and signatures off-chain, we need to call the Safe Transaction Service API - a centralized and open-source service that anyone can deploy and run. The Safe Transaction Service is used by Safe{Wallet} (opens in a new tab) to store transactions and signatures by default. To store a new transaction, we need to call the proposeTransaction from the API Kit, passing the Safe address, an object with the transaction, and a signature from one owner. _ 22 // Get the signature from OWNER_1_ADDRESS _ 22 const signatureOwner1 = safeTransaction.getSignature(OWNER_1_ADDRESS) as EthSafeSignature _ 22 _ 22 // Get the transaction hash of the safeTransaction _ 22 const safeTransactionHash = await protocolKit.getTransactionHash(safeTransaction) _ 22 _ 22 // Instantiate the API Kit _ 22 // Use the chainId where you have the Safe account deployed _ 22 // How to get an Api key => http://docs.safe.global/core-api/how-to-use-api-keys _ 22 const apiKit = new SafeApiKit({ _ 22 chainId, _ 22 apiKey: 'YOUR_API_KEY' _ 22 }) _ 22 _ 22 // Propose the transaction _ 22 await apiKit.proposeTransaction({ _ 22 safeAddress: SAFE_3_4_ADDRESS, _ 22 safeTransactionData: safeTransaction.data, _ 22 safeTxHash: safeTransactionHash, _ 22 senderAddress: signerAddress, _ 22 senderSignature: buildSignatureBytes([signatureOwner1]) _ 22 }) The transaction is now publicly available in the Safe Transaction Service with the signature of the owner who submitted it. Confirm the transaction To add the signatures from the remaining owners, we need to call the confirmTransaction , passing the safeMessageHash and a signature from the owner. Once a transaction is proposed, it becomes available on Safe{Wallet} (opens in a new tab) . However, to execute the transaction, all the confirmations from the owners are needed. _ 19 const signatureOwner2 = safeTransaction.getSignature(OWNER_2_ADDRESS) as EthSafeSignature _ 19 _ 19 // Confirm the transaction from OWNER_2_ADDRESS _ 19 await apiKit.confirmTransaction( _ 19 safeTransactionHash, _ 19 buildSignatureBytes([signatureOwner2]) _ 19 ) _ 19 _ 19 // Confirm the transaction with the owner SAFE_1_1_ADDRESS _ 19 await apiKit.confirmTransaction( _ 19 safeTransactionHash, _ 19 buildSignatureBytes([signatureSafe1_1]) _ 19 ) _ 19 _ 19 // Add signature from the owner SAFE_2_3_ADDRESS _ 19 await apiKit.confirmTransaction( _ 19 safeTransactionHash, _ 19 buildSignatureBytes([signerSafeSig2_3]) _ 19 ) At this point, the transaction stored in the Safe Transaction Service contains all the required signatures from the owners of the Safe. The getTransaction method returns the transaction with the confirmations property to check all the added signatures. _ 10 // Get the transactions _ 10 const signedTransaction = await apiKit.getTransaction(safeTransactionHash) _ 10 _ 10 // Get the confirmations _ 10 const confirmations = signedTransaction.confirmations Safe{Wallet} (opens in a new tab) exposes to its users the list of pending transactions. _ 10 https://app.safe.global/transactions/queue?safe=<NETWORK_PREFIX>:<SAFE_ADDRESS> Execute the transaction Connect the Safe and an a signer to the Protocol Kit. Ensure enough funds are available in the owner's account to execute the transaction and cover the gas costs. Once the Protocol Kit is initialized, the executeTransaction method receives and executes the transaction with the required signatures. _ 10 protocolKit = await protocolKit.connect({ _ 10 provider: RPC_URL, _ 10 signer: OWNER_1_PRIVATE_KEY, _ 10 safeAddress: SAFE_3_4_ADDRESS _ 10 }) _ 10 _ 10 // Execute the Safe transaction _ 10 const transactionResponse = await protocolKit.executeTransaction(safeTransaction) At this point, the Safe transaction should be executed on-chain and listed on Safe{Wallet} (opens in a new tab) . _ 10 https://app.safe.global/transactions/history?safe=<NETWORK_PREFIX>:<SAFE_ADDRESS> The safeTransaction.encodedSignature method returns the signatures concatenated and sorted by the address of the signers. It should look like this: _ 10 0x000000000000000000000000215033cdE0619D60B7352348F4598316Cc39bC6E000000000000000000000000000000000000000000000000000000000000010400969308e2abeda61a0c9c41b3c615012f50dd7456ca76ea39a18e3b975abeb67f275b07810dd59fc928f3f9103e52557c1578c7c5c171ffc983afa5306466b1261f000000000000000000000000f75D61D6C27a7CC5788E633c1FC130f0F4a62D330000000000000000000000000000000000000000000000000000000000000165004d63c79cf9d743782bc31ad58c1a316020b39839ab164caee7ecac9829f685cc44ec0d066a5dfe646b2ffeeb37575df131daf9c96ced41b8c7c4aea8dc5461801c00000000000000000000000000000000000000000000000000000000000000415edb6ffe67dd935d93d07c634970944ba0b096f767b92018ad635e8b28effeea5a1e512f1ad6f886690e0e30a3fae2c8c61d3f83d24d43276acdb3254b92ea5b1f0000000000000000000000000000000000000000000000000000000000000082023d1746ed548e90f387a6b8ddba26e6b80a78d5bfbc36e5bfcbfd63e136f8071db6e91c037fa36bde72159138bbb74fc359b35eb515e276a7c0547d5eaa042520d3e6565e5590641db447277243cf24711dce533cfcaaf3a64415dcb9fa309fbf2de1ae4709c6450752acc0d45e01b67b55379bdf4e3dc32b2d89ad0a60c231d61f Type Description Bytes Acc byte Value Hex Hex string characters 1 - 0x 1/1 Safe signer Safe Address 32 32 000000000000000000000000215033cdE0619D60B7352348F4598316Cc39bC6E Data position for 1/1 Safe 104 hex = Signature data for 1/1 Safe start at byte 260 32 64 0000000000000000000000000000000000000000000000000000000000000104 Signature Type Smart contract signature 1 65 00 Owner signature OWNER_1_ADDRESS signature 65 130 969308e2abeda61a0c9c41b3c615012f50dd7456ca76ea39a18e3b975abeb67f275b07810dd59fc928f3f9103e52557c1578c7c5c171ffc983afa5306466b1261f 2/3 Safe signer Safe Address 32 162 000000000000000000000000f75D61D6C27a7CC5788E633c1FC130f0F4a62D33 Data position for 2/3 Verifier 165 hex = Signature data for 2/3 Safe start at byte 357 32 194 0000000000000000000000000000000000000000000000000000000000000165 Signature Type Smart contract signature 1 195 00 Owner signature OWNER_2_ADDRESS signature 65 260 4d63c79cf9d743782bc31ad58c1a316020b39839ab164caee7ecac9829f685cc44ec0d066a5dfe646b2ffeeb37575df131daf9c96ced41b8c7c4aea8dc5461801c 1/1 Safe Signature Length Start of the 1/1 Safe Signature. 41 hex = 65 bytes 32 292 0000000000000000000000000000000000000000000000000000000000000041 Signature OWNER_3_ADDRESS signature 65 357 5edb6ffe67dd935d93d07c634970944ba0b096f767b92018ad635e8b28effeea5a1e512f1ad6f886690e0e30a3fae2c8c61d3f83d24d43276acdb3254b92ea5b1f 2/3 Safe Signature length Start of the 2/3 Safe Signature. 82 hex = 130 bytes 32 389 0000000000000000000000000000000000000000000000000000000000000082 Signature OWNER_4_ADDRESS and OWNER_5_ADDRESS concatenated signatures 130 519 023d1746ed548e90f387a6b8ddba26e6b80a78d5bfbc36e5bfcbfd63e136f8071db6e91c037fa36bde72159138bbb74fc359b35eb515e276a7c0547d5eaa042520d3e6565e5590641db447277243cf24711dce533cfcaaf3a64415dcb9fa309fbf2de1ae4709c6450752acc0d45e01b67b55379bdf4e3dc32b2d89ad0a60c231d61f Signatures Messages Was this page helpful? Report issue ## /sdk/protocol-kit/guides/signatures/messages SDK Protocol Kit Guides Signatures Messages Message signatures Using the Protocol Kit, this guide explains how to generate and sign messages from a Safe account, including plain string messages and EIP-712 JSON messages. ℹ️ Before starting, check this guide's setup . Prerequisites Node.js and npm (opens in a new tab) Steps Install dependencies _ 10 yarn install @safe-global/protocol-kit Create a message Messages can be plain strings or valid EIP-712 typed data structures. _ 10 // An example of a string message _ 10 const STRING_MESSAGE = "I'm the owner of this Safe account" _ 47 // An example of a typed data message _ 47 const TYPED_MESSAGE = { _ 47 types: { _ 47 EIP712Domain: [ _ 47 { name: 'name', type: 'string' }, _ 47 { name: 'version', type: 'string' }, _ 47 { name: 'chainId', type: 'uint256' }, _ 47 { name: 'verifyingContract', type: 'address' } _ 47 ], _ 47 Person: [ _ 47 { name: 'name', type: 'string' }, _ 47 { name: 'wallets', type: 'address[]' } _ 47 ], _ 47 Mail: [ _ 47 { name: 'from', type: 'Person' }, _ 47 { name: 'to', type: 'Person[]' }, _ 47 { name: 'contents', type: 'string' } _ 47 ] _ 47 }, _ 47 domain: { _ 47 name: 'Ether Mail', _ 47 version: '1', _ 47 chainId: Number(chainId), _ 47 verifyingContract: '0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC' _ 47 }, _ 47 primaryType: 'Mail', _ 47 message: { _ 47 from: { _ 47 name: 'Cow', _ 47 wallets: [ _ 47 '0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826', _ 47 '0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF' _ 47 ] _ 47 }, _ 47 to: [ _ 47 { _ 47 name: 'Bob', _ 47 wallets: [ _ 47 '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB', _ 47 '0xB0BdaBea57B0BDABeA57b0bdABEA57b0BDabEa57', _ 47 '0xB0B0b0b0b0b0B000000000000000000000000000' _ 47 ] _ 47 } _ 47 ], _ 47 contents: 'Hello, Bob!' _ 47 } _ 47 } The createMessage method in the Protocol Kit allows for creating new messages and returns an instance of the EthSafeMessage class. Here, we are passing TYPED_MESSAGE , but STRING_MESSAGE could also be passed. _ 10 let safeMessage = protocolKit.createMessage(TYPED_MESSAGE) The returned safeMessage object contains the message data ( safeMessage.data ) and a map of owner-signature pairs ( safeMessage.signatures ). The structure is similar to the EthSafeTransaction class but applied for messages instead of transactions. We use let to initialize the safeMessage variable because we will add the signatures later. _ 10 class EthSafeMessage implements SafeMessage { _ 10 data: EIP712TypedData | string _ 10 signatures: Map<string, SafeSignature> = new Map() _ 10 ... _ 10 // Other props and methods _ 10 } Sign the message Once the safeMessage object is created, we need to collect the signatures from the signers who will sign it. Following our setup , we will sign a message with SAFE_3_4_ADDRESS , the main Safe account in this guide. To do that, we first need to sign the same message with its owners: OWNER_1_ADDRESS , OWNER_2_ADDRESS , SAFE_1_1_ADDRESS , and SAFE_2_3_ADDRESS . ECDSA signatures This applies to OWNER_1_ADDRESS and OWNER_2_ADDRESS accounts, as both are EOAs. The signMessage method takes the safeMessage together with a SigningMethod and adds the new signature to the signMessage.signatures map. Depending on the type of message, the SigningMethod can take these values: SigningMethod.ETH_SIGN SigningMethod.ETH_SIGN_TYPED_DATA_V4 _ 25 // Connect OWNER_1_ADDRESS _ 25 protocolKit = await protocolKit.connect({ _ 25 provider: RPC_URL _ 25 signer: OWNER_1_PRIVATE_KEY _ 25 }) _ 25 _ 25 // Sign the safeMessage with OWNER_1_ADDRESS _ 25 // After this, the safeMessage contains the signature from OWNER_1_ADDRESS _ 25 safeMessage = await protocolKit.signMessage( _ 25 safeMessage, _ 25 SigningMethod.ETH_SIGN_TYPED_DATA_V4 _ 25 ) _ 25 _ 25 // Connect OWNER_2_ADDRESS _ 25 protocolKit = await protocolKit.connect({ _ 25 provider: RPC_URL _ 25 signer: OWNER_2_PRIVATE_KEY _ 25 }) _ 25 _ 25 // Sign the safeMessage with OWNER_2_ADDRESS _ 25 // After this, the safeMessage contains the signature from OWNER_1_ADDRESS and OWNER_2_ADDRESS _ 25 safeMessage = await protocolKit.signMessage( _ 25 safeMessage, _ 25 SigningMethod.ETH_SIGN_TYPED_DATA_V4 _ 25 ) Smart contract signatures When signing with a Safe account, the SigningMethod will take the value SigningMethod.SAFE_SIGNATURE . 1/1 Safe account This applies to the SAFE_1_1_ADDRESS account, another owner of SAFE_3_4_ADDRESS . We need to connect the Protocol Kit to SAFE_1_1_ADDRESS and the OWNER_3_ADDRESS account (the only owner of SAFE_1_1_ADDRESS ) and sign the message. _ 27 // Create a new message object _ 27 let messageSafe1_1 = await createMessage(TYPED_MESSAGE) _ 27 _ 27 // Connect OWNER_3_ADDRESS and SAFE_1_1_ADDRESS _ 27 protocolKit = await protocolKit.connect({ _ 27 provider: RPC_URL _ 27 signer: OWNER_3_PRIVATE_KEY, _ 27 safeAddress: SAFE_1_1_ADDRESS _ 27 }) _ 27 _ 27 // Sign the messageSafe1_1 with OWNER_3_ADDRESS _ 27 // After this, the messageSafe1_1 contains the signature from OWNER_3_ADDRESS _ 27 messageSafe1_1 = await signMessage( _ 27 messageSafe1_1, _ 27 SigningMethod.SAFE_SIGNATURE, _ 27 SAFE_3_4_ADDRESS // Parent Safe address _ 27 ) _ 27 _ 27 // Build the contract signature of SAFE_1_1_ADDRESS _ 27 const signatureSafe1_1 = await buildContractSignature( _ 27 Array.from(messageSafe1_1.signatures.values()), _ 27 SAFE_1_1_ADDRESS _ 27 ) _ 27 _ 27 // Add the signatureSafe1_1 to safeMessage _ 27 // After this, the safeMessage contains the signature from OWNER_1_ADDRESS, OWNER_2_ADDRESS and SAFE_1_1_ADDRESS _ 27 safeMessage.addSignature(signatureSafe1_1) When signing with a child Safe account, we need to specify the parent Safe address to generate the signature based on the version of the contract. 2/3 Safe account This applies to the SAFE_2_3_ADDRESS account, another owner of SAFE_3_4_ADDRESS . We need to connect the Protocol Kit to SAFE_2_3_ADDRESS and the OWNER_4_ADDRESS and OWNER_5_ADDRESS accounts (owners of SAFE_2_3_ADDRESS ) and sign the message. _ 41 // Create a new message object _ 41 let messageSafe2_3 = await createMes
프로젝트로 돌아가기