多重签名账户

通过为账户启用 多重签名 ,你可以使这个账户创建的事务需要 经过多个公钥的签名才能生效。首先你需要为账户配置 阈值(threshold),在 Stellar 网络中,每个操作都有着自己的权限等级,这个等级分为高、中、低三等, 你可以为每个等级配置一个阈值,这个值可以在 1-255 之间。你可以为你的账户添加多个签名者,每个签名者都有着自己的权重,这个权重可以在 1-255 之间,如果你想 从账户中删除一个签名者,那么你只需要将这个签名者的权重设置为 0。任何事务得到的签名权重必须大于或等于它所需要的阈值才能生效。

首先,我们来设置账户的阈值等级,我们将低级权限操作的阈值设为 1,中级权限操作的阈值设置为 2,高级权限操作的阈值设置为 3。付款操作是一个中级权限的操作。 如果你的主公钥权重为 1,那么你需要使用另外一个公钥进行签名,使它获得的权重大于等于 2 才行。

在下面这个示例中,我们将做以下几件事情:

  • 向账户中添加第二个签名账户

  • 为我们的主公钥设置权重,并设置阈值等级

  • 创建一个需要多重签名的付款事务

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from stellar_sdk import Server, TransactionBuilder, Signer, Network, Keypair

server = Server(horizon_url="https://horizon-testnet.stellar.org")
root_keypair = Keypair.from_secret("SA6XHAH4GNLRWWWF6TEVEWNS44CBNFAJWHWOPZCVZOUXSQA7BOYN7XHC")
root_account = server.load_account(account_id=root_keypair.public_key)
secondary_keypair = Keypair.from_secret("SAMZUAAPLRUH62HH3XE7NVD6ZSMTWPWGM6DS4X47HLVRHEBKP4U2H5E7")

secondary_signer = Signer.ed25519_public_key(account_id=secondary_keypair.public_key, weight=1)
transaction = TransactionBuilder(
    source_account=root_account,
    network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
    base_fee=100) \
    .append_set_options_op(
    master_weight=1,  # set master key weight
    low_threshold=1,
    med_threshold=2,  # a payment is medium threshold
    high_threshold=2,  # make sure to have enough weight to add up to the high threshold!
    signer=secondary_signer) \
    .set_timeout(30) \
    .build()

# only need to sign with the root signer as the 2nd signer won't
# be added to the account till after this transaction completes
transaction.sign(root_keypair)
response = server.submit_transaction(transaction)
print(response)

# now create a payment with the account that has two signers
destination = "GBA5SMM5OYAOOPL6R773MV7O3CCLUDVLCWHIVVL3W4XTD3DA5FJ4JSEZ"
transaction = TransactionBuilder(
    source_account=root_account,
    network_passphrase=Network.TESTNET_NETWORK_PASSPHRASE,
    base_fee=100) \
    .append_payment_op(
    destination=destination,
    amount="2000",
    asset_code="XLM") \
    .set_timeout(30) \
    .build()

# now we need to sign the transaction with both the root and the secondary_keypair
transaction.sign(root_keypair)
transaction.sign(secondary_keypair)
response = server.submit_transaction(transaction)
print(response)