Test accounts
In tests, you can create and use test accounts to test more complex scenarios, as in this example:
smartpy
@sp.module
def main():
class MyContract(sp.Contract):
def __init__(self, admin):
self.data.adminAccount = admin
@sp.entrypoint
def setAdmin(self, newAdmin):
assert sp.sender == self.data.adminAccount, "Must be admin"
self.data.adminAccount = newAdmin
@sp.add_test()
def test():
# Create test accounts
alice = sp.test_account("Alice")
bob = sp.test_account("Bob")
scenario = sp.test_scenario("Admin test", main)
contract = main.MyContract(alice.address)
scenario += contract
# Verify that non-admin account can't change the admin
contract.setAdmin(
bob.address,
_sender = bob,
_valid = False
)
# Verify that the admin can change the admin
contract.setAdmin(
bob.address,
_sender = alice,
)
scenario.verify(contract.data.adminAccount == bob.address)
- sp.test_account(seed) → test_account
Create a deterministic keypair from a seed string. This function always creates the same keypair for the same seed.
python# seed is `Alice` alice = sp.test_account("Alice")
This command produces an object with the following properties:
Property Type Description alice.address sp.address
Gives the public-key-hash alice.public_key_hash sp.key_hash
Gives the public-key-hash alice.public_key sp.key
Gives the public-key alice.secret_key sp.string
Gives the secret-key
- sp.make_signature(secret_key, message, message_format = 'Raw') → sp.signature
Creates a signature compatible with the sp.check_signature(...) function.
The
message_format
parameter accepts two values:Hex
, in which case the message is interpreted as a hexadecimal stringRaw
(the default) in which case the message is asp.bytes
value, usually the result of ansp.pack
call.
INFO
You can't use sp.test_account
or sp.make_signature
in smart contracts because Michelson can't manipulate secret keys.