bitpro가 원문에서 글자만 모은 내용이에요. 표·그림·수식은 빠지거나 깨질 수 있어요. 전체 12쪽 중 5쪽
4
})
_
24
_
24
await waitForTransactionReceipt(
_
24
chiadoClient!,
_
24
{ hash: transactionHashChiado }
_
24
)
_
24
_
24
const newProtocolKitChiado = await protocolKitChiado.connect({
_
24
safeAddress: chiadoPredictedSafeAddress
_
24
})
_
24
_
24
const isChiadoSafeDeployed = await newProtocolKitChiado.isSafeDeployed() // True
_
24
const chiadoDeployedSafeAddress = await newProtocolKitChiado.getAddress()
If everything went well,
isChiadoSafeDeployed
should be
true
, and the
chiadoDeployedSafeAddress
should equal the
chiadoPredictedSafeAddress
.
In addition,
chiadoDeployedSafeAddress
and
sepoliaDeployedSafeAddress
should have the same value.
Recap and further reading
After following this guide, you are able to deploy multiple Safe accounts with the same address on different chains using the Protocol Kit.
Safe deployment
Execute transactions
Was this page helpful?
Report issue
## /sdk/protocol-kit/guides/execute-transactions
SDK
Protocol Kit
Guides
Execute transactions
Execute transactions
In this guide, you will learn how to create Safe transactions, sign them, collect the signatures from the different owners, and execute them.
See the
Protocol Kit reference
to find more details and configuration options.
Prerequisites
Node.js and npm
(opens in a new tab)
.
An existing Safe with several signers.
Install dependencies
First, you need to install some dependencies.
_
10
pnpm add @safe-global/api-kit \
_
10
@safe-global/protocol-kit \
_
10
@safe-global/types-kit
Steps
Imports
Here are all the necessary imports for this guide.
_
10
import SafeApiKit from '@safe-global/api-kit'
_
10
import Safe from '@safe-global/protocol-kit'
_
10
import {
_
10
MetaTransactionData,
_
10
OperationType
_
10
} from '@safe-global/types-kit'
Setup
You need a Safe account setup with two or more signers and threshold two, so at least multiple signatures have to be collected when executing a transaction.
This example uses private keys, but any EIP-1193 compatible signers can be used.
_
10
const SAFE_ADDRESS = // ...
_
10
_
10
const OWNER_1_ADDRESS = // ...
_
10
const OWNER_1_PRIVATE_KEY = // ...
_
10
_
10
const OWNER_2_PRIVATE_KEY = // ...
_
10
_
10
const RPC_URL = 'https://ethereum-sepolia-rpc.publicnode.com'
This guide uses Sepolia, but you can use any chain from the Safe Transaction Service
supported networks
.
Initialize the Protocol Kit
To handle transactions and signatures, you need to create an instance of the Protocol Kit with the
provider
,
signer
and
safeAddress
.
Optionally, you can
track your Safe transactions on-chain
by using the
onchainAnalytics
property.
_
10
const protocolKitOwner1 = await Safe.init({
_
10
provider: RPC_URL,
_
10
signer: OWNER_1_PRIVATE_KEY,
_
10
safeAddress: SAFE_ADDRESS,
_
10
onchainAnalytics // Optional
_
10
})
Create a transaction
Create a
safeTransactionData
object with the properties of the transaction, add it to an array of transactions you want to execute, and pass it to the
createTransaction
method.
_
10
const safeTransactionData: MetaTransactionData = {
_
10
to: '0x',
_
10
value: '1', // 1 wei
_
10
data: '0x',
_
10
operation: OperationType.Call
_
10
}
_
10
_
10
const safeTransaction = await protocolKitOwner1.createTransaction({
_
10
transactions: [safeTransactionData]
_
10
})
For more details on what to include in a transaction, see the
createTransaction
method in the reference.
Propose the transaction
Before a transaction can be executed, the signer who creates it needs to send it to the Safe Transaction Service so that it is accessible by the other owners, who can then give their approval and sign the transaction.
Firstly, you need to create an instance of the API Kit. In chains where the
Safe Transaction Service
is supported, it's enough to specify the
chainId
property.
_
10
// How to get an Api key => http://docs.safe.global/core-api/how-to-use-api-keys
_
10
const apiKit = new SafeApiKit({
_
10
chainId: 11155111n,
_
10
apiKey: 'YOUR_API_KEY'
_
10
})
You need to calculate the Safe transaction hash, sign the transaction hash, and call the
proposeTransaction
method from the API Kit instance to propose a transaction.
For a full list and description of the properties see
proposeTransaction
in the API Kit reference.
_
13
// Deterministic hash based on transaction parameters
_
13
const safeTxHash = await protocolKitOwner1.getTransactionHash(safeTransaction)
_
13
_
13
// Sign transaction to verify that the transaction is coming from owner 1
_
13
const senderSignature = await protocolKitOwner1.signHash(safeTxHash)
_
13
_
13
await apiKit.proposeTransaction({
_
13
safeAddress,
_
13
safeTransactionData: safeTransaction.data,
_
13
safeTxHash,
_
13
senderAddress: OWNER_1_ADDRESS,
_
13
senderSignature: senderSignature.data
_
13
})
Retrieve the pending transactions
The other signers need to retrieve the pending transactions from the Safe Transaction Service. Depending on the situation, different methods in the API Kit are available.
Call the
getPendingTransactions
method to retrieve all the pending transactions of a Safe account.
_
10
const pendingTransactions = (await apiKit.getPendingTransactions(safeAddress)).results
Confirm the transaction
Once a signer has the pending transaction, they need to sign it with the Protocol Kit and submit the signature to the service using the
confirmTransaction
method.
_
14
const protocolKitOwner2 = await Safe.init({
_
14
provider: RPC_URL,
_
14
signer: OWNER_2_PRIVATE_KEY,
_
14
safeAddress: SAFE_ADDRESS
_
14
})
_
14
_
14
const safeTxHash = transaction.transactionHash
_
14
const signature = await protocolKitOwner2.signHash(safeTxHash)
_
14
_
14
// Confirm the Safe transaction
_
14
const signatureResponse = await apiKit.confirmTransaction(
_
14
safeTxHash,
_
14
signature.data
_
14
)
Execute the transaction
The Safe transaction is now ready to be executed. This can be done using the
Safe{Wallet}
(opens in a new tab)
web interface, the
Protocol Kit
, the
Safe CLI
or any other tool that's available.
In this guide, the first signer will get the transaction from the service by calling the
getTransaction
method and execute it by passing the transaction with all the signatures to the
executeTransaction
method.
_
10
const safeTransaction = await apiKit.getTransaction(safeTxHash)
_
10
const executeTxResponse = await protocolKitOwner1.executeTransaction(safeTransaction)
Recap and further reading
After following this guide, you are able to create, sign, and execute Safe transactions with the Protocol Kit and share the signatures with the different signers using the API Kit.
Multichain Safe deployment
Signatures
Was this page helpful?
Report issue
## /sdk/protocol-kit/guides/signatures
SDK
Protocol Kit
Guides
Signatures
Safe Signatures
This guide shows how Safe signatures work and how to generate them using the Protocol Kit.
Setup
Safe accounts can be configured with different threshold values and types of owners. Safe owners can be any Ethereum address, such as:
Externally-owned accounts (EOAs). They generate an ECDSA signature to approve Safe transactions.
Smart accounts that implement the
EIP-1271
(opens in a new tab)
for signature validation, like Safe does. They use a different signature algorithm depending on the implementation of the smart account.
In the following guides, there are different accounts involved that will be used as an example:
Who
Description
Address for this example
SAFE_3_4_ADDRESS
3/4 Safe (3 signatures required out of 4 owners)
0xb3b3862D8e38a1E965eb350B09f2167B2371D652
OWNER_1_ADDRESS
EOA and owner of
SAFE_3_4_ADDRESS
0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1
OWNER_2_ADDRESS
EOA and owner of
SAFE_3_4_ADDRESS
0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0
SAFE_1_1_ADDRESS
1/1 Safe and owner of
SAFE_3_4_ADDRESS
0x215033cdE0619D60B7352348F4598316Cc39bC6E
OWNER_3_ADDRESS
EOA and owner of
SAFE_1_1_ADDRESS
0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b
SAFE_2_3_ADDRESS
2/3 Safe and owner of
SAFE_3_4_ADDRESS
0xf75D61D6C27a7CC5788E633c1FC130f0F4a62D33
OWNER_4_ADDRESS
EOA and owner of
SAFE_2_3_ADDRESS
0xE11BA2b4D45Eaed5996Cd0823791E0C93114882d
OWNER_5_ADDRESS
EOA and owner of
SAFE_2_3_ADDRESS
0xd03ea8624C8C5987235048901fB614fDcA89b117
We need to instantiate all the signers based on the Safe owner accounts.
_
10
// https://chainlist.org/?search=sepolia&testnets=true
_
10
const RPC_URL = 'https://ethereum-sepolia-rpc.publicnode.com'
_
10
_
10
// Initialize signers
_
10
const OWNER_1_PRIVATE_KEY = // ...
_
10
const OWNER_2_PRIVATE_KEY = // ...
_
10
const OWNER_3_PRIVATE_KEY = // ...
_
10
const OWNER_4_PRIVATE_KEY = // ...
_
10
const OWNER_5_PRIVATE_KEY = // ...
Guides
Transaction signatures
The
Transaction signatures
guide explains how transactions are signed by Safe owners using the Protocol Kit.
Message signatures
Using the Protocol Kit, the
Message signatures
guide explains how to generate and sign messages, including plain string messages and EIP-712 JSON messages.
Execute transactions
Transactions
Was this page helpful?
Report issue
## /sdk/protocol-kit/guides/signatures/transactions
SDK
Protocol Kit
Guides
Signatures
Transactions
Transaction signatures
This guide explains how transactions are signed by the Safe owners using the Protocol Kit.
ℹ️
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 transaction
The
createTransaction
method in the Protocol Kit allows the creation of new Safe transactions and returns an instance of the
EthSafeTransaction
class.
_
10
// Create a transaction to send 0.01 ETH
_
10
const safeTransactionData: SafeTransactionDataPartial = {
_
10
to: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',
_
10
value: '100000000000000000', // 0.01 ETH
_
10
data: '0x'
_
10
}
_
10
_
10
let safeTransaction = await protocolKit.createTransaction({
_
10
transactions: [safeTransactionData]
_
10
})
The returned
safeTransaction
object contains the transaction data (
safeTransaction.data
) and a map of the owner-signature pairs (
safeTransaction.signatures
). The structure is similar to the
EthSafeMessage
class but applied for transactions instead of messages.
We use
let
to initialize the
safeTransaction
variable because we will add the signatures later.
_
10
class EthSafeTransaction implements SafeTransaction {
_
10
data: SafeTransactionData
_
10
signatures: Map<string, SafeSignature> = new Map()
_
10
...
_
10
// Other properties and methods
_
10
}
Sign the transaction
Once the
safeTransaction
object is created, we need to collect the signatures from the signers who will sign it.
Following our
setup
, we will sign a Safe transaction from
SAFE_3_4_ADDRESS
, the main Safe account in this guide. To do that, we first need to sign the same transaction with its owners:
OWNER_1_ADDRESS
,
OWNER_2_ADDRESS
,
SAFE_1_1_ADDRESS
, and
SAFE_2_3_ADDRESS
.
ECDSA signature
This applies to
OWNER_1_ADDRESS
and
OWNER_2_ADDRESS
accounts, as both are EOAs.
The
signTransaction
method takes the
safeTransaction
together with a
SigningMethod
and adds the new signature to the
safeTransaction.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 safeTransaction with OWNER_1_ADDRESS
_
25
// After this, the safeTransaction contains the signature from OWNER_1_ADDRESS
_
25
safeTransaction = await protocolKit.signTransaction(
_
25
safeTransaction,
_
25
SigningMethod.ETH_SIGN
_
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 safeTransaction with OWNER_2_ADDRESS
_
25
// After this, the safeTransaction contains the signature from OWNER_1_ADDRESS and OWNER_2_ADDRESS
_
25
safeTransaction = await protocolKit.signTransaction(
_
25
safeTransaction,
_
25
SigningMethod.ETH_SIGN_TYPED_DATA_V4
_
25
)
At this point, the
safeTransaction
object should look like this:
_
15
EthSafeTransaction {
_
15
signatures: Map(2) {
_
15
'0x90f8bf6a479f320ead074411a4b0e7944ea8c9c1' => EthSafeSignature {
_
15
signer: '0x90F8bf6A479f320ead074411a4B0e7944Ea8c9C1',
_
15
data: '0x969308e2abeda61a0c9c41b3c615012f50dd7456ca76ea39a18e3b975abeb67f275b07810dd59fc928f3f9103e52557c1578c7c5c171ffc983afa5306466b1261f',
_
15
isContractSignature: false
_
15
},
_
15
'0xffcf8fdee72ac11b5c542428b35eef5769c409f0' => EthSafeSignature {
_
15
signer: '0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0',
_
15
data: '0x4d63c79cf9d743782bc31ad58c1a316020b39839ab164caee7ecac9829f685cc44ec0d066a5dfe646b2ffeeb37575df131daf9c96ced41b8c7c4aea8dc5461801c',
_
15
isContractSignature: false
_
15
}
_
15
},
_
15
data: { ... }
_
15
}
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.
An ECDSA signature comprises two 32-byte integers (
r
,
s
) and an extra byte for recovery (
v
), totaling 65 bytes. In hexadecimal string format, each byte is represented by two characters. Hence, a 65-byte Ethereum signature will be 130 characters long. Including the
0x
prefix commonly used with signatures, the total character count for such a signature would be 132.
Two more characters are required to represent a byte (8 bits) in hexadecimal. Each hexadecimal character represents four bits. Therefore, two hexadecimal characters (2 x 4 bits) can represent a byte (8 bits).
The final part of the signature, either
1f
or
1c
, indicates the signature type.
Safe supports the following
v
values:
0
: Contract signature.
1
: Approved hash.
{27, 28} + 4
: Ethereum adjusted ECDSA recovery byte for EIP-191 signed message.
Regarding the EIP-191 signed message, the
v
value is adjusted to the ECDSA
v + 4
. If the generated value is
28
and adjusted to
0x1f
, the signature verification will fail as it should be
0x20
('28 + 4 = 32
) instead. If
v > 30
, then the default
v
(
27
,
28
) was adjusted because of the
eth_sign` implementation. This calculation is automatically done by the Safe{Core} SDK.
Other: Ethereum adjusted ECDSA recovery byte for raw signed hash.
The hexadecimal value
1f
equals the decimal number
31
. If the decimal value is greater than
30
, it
indicates
(opens in a new tab)
that the signature is an
eth_sign
signature.
The hexadecimal value
1c
equals the decimal number
28
, indicating that the signature is a typed data signature.
The initial signature should look like this:
0x969308e2abeda61a0c9c41b3c615012f50dd7456ca76ea39a18e3b975abeb67f275b07810dd59fc928f3f9103e52557c1578c7c5c171ffc983afa5306466b1261f
:
Type
Description
Bytes
Value
Hex
Hex string characters
1
0x
Signature
Signature bytes
64
969308e2abeda61a0c9c41b3c615012f50dd7456ca76ea39a18e3b975abeb67f275b07810dd59fc928f3f9103e52557c1578c7c5c171ffc983afa5306466b126
Signature Type
1f hex is 31 in decimal
1
1f
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 transaction.
_
19
// Create a new transaction object
_
19
let transactionSafe1_1 = await protocolKit.createTransaction({
_
19
transactions: [safeTransactionData]
_
19
})
_
19
_
19
// Connect OWNER_3_ADDRESS and SAFE_1_1_ADDRESS
_
19
protocolKit = await protocolKit.connect({
_
19
provider: RPC_URL,
_
19
signer: OWNER_3_PRIVATE_KEY,
_
19
safeAddress: SAFE_1_1_ADDRESS
_
19
})
_
19
_
19
// Sign the transactionSafe1_1 with OWNER_3_ADDRESS
_
19
// After this, transactionSafe1_1 contains the signature from OWNER_3_ADDRESS
_
19
transactionSafe1_1 = await protocolKit.signTransaction(
_
19
transactionSafe1_1,
_
19
SigningMethod.SAFE_SIGNATURE,
_
19
SAFE_3_4_ADDRESS // Parent Safe address
_
19
)
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.
At this point, the
transactionSafe1_1
object should look like this:
_
10
EthSafeTransaction {
_
10
signatures: Map(1) {
_
10
'0x22d491bde2303f2f43325b2108d26f1eaba1e32b' => EthSafeSignature {
_
10
signer: '0x22d491Bde2303f2f43325b2108D26f1eAbA1e32b',
_
10
data: '0x5edb6ffe67dd935d93d07c634970944ba0b096f767b92018ad635e8b28effeea5a1e512f1ad6f886690e0e30a3fae2c8c61d3f83d24d43276acdb3254b92ea5b1f',
_
10
isContractSignature: false
_
10
}
_
10
},
_
10
data: { ...}
_
10
}
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
SAFE_1_1_ADDRESS
to our initial transaction.
_
10
// Build the contract signature of SAFE_1_1_ADDRESS
_
10
const signatureSafe1_1 = await buildContractSignature(
_
10
Array.from(transactionSafe1_1.signatures.values()),
_
10
SAFE_1_1_ADDRESS
_
10
)
_
10
_
10
// Add the signatureSafe1_1 to safeTransaction
_
10
// After this, the safeTransaction contains the signature from OWNER_1_ADDRESS, OWNER_2_ADDRESS and SAFE_1_1_ADDRESS
_
10
safeTransaction.addSignature(signatureSafe1_1)
The
signatureSafe1_1
object should look like this:
_
10
EthSafeSignature {
_
10
signer: '0x215033cdE0619D60B7352348F4598316Cc39bC6E',
_
10
data: '0x5edb6ffe67dd935d93d07c634970944ba0b096f767b92018ad635e8b28effeea5a1e512f1ad6f886690e0e30a3fae2c8c61d3f83d24d43276acdb3254b92ea5b1f',
_
10
isContractSignature: true
_
10
}
The
isContractSignature
flag is now
true
because
signatureSafe1_1
is an EIP-1271 smart contract signature from the
SAFE_1_1_ADDRESS
account.
The
signatureSafe1_1.data
signature should look like this:
_
10
0x000000000000000000000000215033cdE0619D60B7352348F4598316Cc39bC6E00000000000000000000000000000000000000000000000000000000000000410000000000000000000000000000000000000000000000000000000000000000415edb6ffe67dd935d93d07c634970944ba0b096f767b92018ad635e8b28effeea5a1e512f1ad6f886690e0e30a3fae2c8c61d3f83d24d43276acdb3254b92ea5b1f
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
000000000000000000000000215033cdE0619D60B7352348F4598316Cc39bC6E
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. 41 hex is 65 in decimal
32
0000000000000000000000000000000000000000000000000000000000000041
Signature
Signature bytes that are verified by the signature verifier
65
5edb6ffe67dd935d93d07c634970944ba0b096f767b92018ad635e8b28effeea5a1e512f1ad6f886690e0e30a3fae2c8c61d3f83d24d43276acdb3254b92ea5b1f
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 transaction.
_
33
// Create a new transaction object
_
33
let transactionSafe2_3 = await protocolKit.createTransaction({
_
33
transactions: [safeTransa프로젝트로 돌아가기