Test accounts
In tests, you can create and use accounts in two ways:
- Generate test accounts by passing a seed string to
sp.test_account
. This function returns an object with the address of the test account in itsaddress
property. - Create objects of type
sp.address
.
To simulate calling a smart contract from a certain account, pass the address of the account (either the test account's address
property or the sp.address
object) as the _sender
or _source
property in the call to the entrypoint, as in this example:
@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.address("tz1QCVQinE8iVj1H2fckqx6oiM85CNJSK9Sx")
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, _sender=bob, _valid=False)
# Verify that the admin can change the admin
contract.setAdmin(
bob,
_sender=alice,
)
scenario.verify(contract.data.adminAccount == bob)
For more information about calling entrypoints in tests, see Testing contracts.
TIP
Using real addresses in test scenarios can simplify deploying contracts. For example, if you use your real account address as the administrator of a contract and compile the contract, the output storage file has your address in it. You can then use that compiled storage file as the initial storage value for the contract.
However, if you want to create or verify cryptographic signatures with sp.make_signature
and sp.check_signature
in tests, you must use test accounts and not sp.address
types.
- 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.
For an example of creating and checking signatures with sp.make_signature
, see the "Signatures" template in the SmartPy online IDE.
INFO
You can't use sp.test_account
or sp.make_signature
in smart contracts because Michelson can't manipulate secret keys.