Key concepts

参考:Sign a message - Phantom Developer Documentation

  • Keypair: The fundamental element of Solana wallets, comprising a public key (address) and a private key.
  • Message: Any arbitrary data or text that a user wants to sign.
  • Signature: A cryptographic proof generated using the private key, verifying the message's authenticity and ensuring it hasn't been tampered with.
  • Off-chain message signing: Signing messages that don't involve a Solana network transaction. 

1.Login Sign Message

https://solana.com/zh/developers/cookbook/wallets/sign-message

在Phantom获取的private key为 88位的base58编码。

base58==2.1.1
solders==0.26.0

从private key获取Keypair

signer = Keypair.from_base58_string(private_key)

对编码后的message签名,返回base64的签名结果

signature = signer.sign_message(message.encode('utf-8'))
signature_base64 = base64.b64encode(signature.to_bytes()).decode('utf-8')

2. Sign Transaction

https://solana.com/zh/developers/cookbook/transactions/offline-transactions

从private key获取Keypair,同上

# Airdrop to fee_payer
    airdrop_resp = await connection.request_airdrop(fee_payer.pubkey(), 1_000_000_000)
    await connection.confirm_transaction(airdrop_resp.value)

    alice = Keypair()
    # Airdrop to alice
    airdrop_resp = await connection.request_airdrop(alice.pubkey(), 1_000_000_000)
    await connection.confirm_transaction(airdrop_resp.value)
# 2. Sign Transaction
    # Use nacl to sign the message
    fee_payer_signing_key = nacl.signing.SigningKey(bytes(fee_payer)[0:32])
    fee_payer_signature = fee_payer_signing_key.sign(message_bytes).signature

    alice_signing_key = nacl.signing.SigningKey(bytes(alice)[0:32])
    alice_signature = alice_signing_key.sign(message_bytes).signature

    print(f"Fee payer signature: {base58.b58encode(fee_payer_signature).decode()}")
    print(f"Alice signature: {base58.b58encode(alice_signature).decode()}")

     

    Logo

    有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

    更多推荐