Example Use Cases and Predicates
The following scripts are not the only useful predicates that are possible, but they represent some additional use cases that demonstrate some of the more advanced capabilities of the Alphabill scripting system.
As a reminder, in all of the examples below, we follow the conventions described in the predicate playground walkthrough, i.e., instructions encoded in the unlock script are executed first, starting with an empty stack, and the stack that results from those operations is passed to, and operated on by the instructions in the predicate.
Pay to an Empty Predicate
Unlock Script:
push true
Bearer predicate:
; Any bearer signature that ends with the stack containing only a single true
; boolean value will do and the bill can be spent.
; This is an empty predicate, containing only comments, and anyone can spend it.
Pay to Public Key
Unlock Script:
push $signature
Bearer predicate:
; We expect the stack to contain only a signature on the transaction itself.
; Push the public key of the bearer
push public key "ed25519" hex "3f75cb8f3e692ac2e9a43bdb3d04d1bf8551b3190768f46dcfa379029a8686dd"
; Use and consume the public key value (topmost element) to verify and
; consume the signature (second element on the stack) on the transaction.
; Pushes the boolean result back to the stack making it the only remaining value.
check_signature
Pay to Public Key Hash
Unlock Script:
push $signature
; Push the actual key pair to the stack.
; The hash is computed by the bearer predicate.
push public key "ed25519" hex "3f75cb8f3e692ac2e9a43bdb3d04d1bf8551b3190768f46dcfa379029a8686dd"Bearer predicate:
Bearer predicate:
; We expect the stack to contain two elements:
; public key value (the topmost element)
; transaction signature (the secont element)
; Duplicate the topmost element (the public key value)
dup
; Calculate the hash value of the top element, and replace the
; topmost element with the calculated digest.
hash "sha256"
; Push a constant hash value to the stack
push hash "sha256" hex "69f9e213169e7f07b72850ed9948d238685d0afe16875d4e6fddf60c576f93b1"
; Compare and replace the two topmost hash values with a boolean
; value (true if they are equal, false otherwise)
equal
; Fail execution if the topmost element is false
verify
; At this point the stack is the same as in the beginning of
; the predicate.
; Use and consume the public key value (topmost element) to
; verify and consume the signature (second element on the stack)
; on the transaction. Pushes the boolean result back to the
; stack making it the only remaining value.
check_signature
Pay to 1-of-2 Public Key Hash
Unlock Script (one key pair):
; Solving the predicate with one key pair.
push $signature
; Push the actual key pair to the stack.
; The hash is computed by the bearer predicate.
push public key "ed25519" hex "3f75cb8f3e692ac2e9a43bdb3d04d1bf8551b3190768f46dcfa379029a8686dd"
Unlock Script (another key pair):
; Solving the predicate with another key pair.
push $signature
; Push the actual key pair to the stack.
; The hash is computed by the bearer predicate.
push public key "ed25519" hex "68888f3f1a132bafc572a1a8eba4e8b66415042cb547dadc0060ceccd2920f45"
Bearer predicate:
; We expect the stack to contain two elements:
; public key value (the topmost element)
; transaction signature (the secont element)
; Duplicate the public key value
dup
; Replace the duplicate public key value with its
; sha-256 hash digest.
hash "sha256"
; (*) Duplicate the calculated hash value
dup
; Push a constant hash value on top of the stack
push hash "sha256" hex "69f9e213169e7f07b72850ed9948d238685d0afe16875d4e6fddf60c576f93b1"
; Compare and replace the two topmost hash values
; with a boolean (true if they are equal, false otherwise)
equal
; Branch (and consume) based on the topmost boolean value
if
; If the two hash values are equal drop the extra
; copy created at (*)
drop
else
; Push another constant hash value to the top of the stack
push hash "sha256" hex "677d5ceaa1d27779d5cb2e5ee68acae672c6594fdf9f50a0b79ca19d9664e807"
; Compare and consume the two topmost hash values
; (the second one created at (*)) and
; push a boolean value to the stack (true if they are
; equal, false otherwise)
equal
; Fail the predicate if the hash values are not equal
verify
end if
; At this point the stack is the same as in the
; beginning of the predicate.
; Use and consume the public key value (topmost element)
; to verify and consume the signature (second element
; on the stack) on the transaction. Pushes the boolean result
; back to the stack making it the only remaining value.
check_signature
Pay to 1-of-2 Public Key Hash (alternative)
Unlock Script:
; We expect the stack to contain two elements:
; public key value (the topmost element)
; transaction signature (the secont element)
; Duplicate the topmost element on the stack (public key value)
dup
; Calculate the sha-256 digest from the topmost element
hash "sha256"
; Push a constant hash value of an accepted public key.
push hash "sha256" hex "69f9e213169e7f07b72850ed9948d238685d0afe16875d4e6fddf60c576f93b1"
; Consume and compare the two topmost hash values and push
; the result back to the stack (true if equal, false otherwise).
equal
; Negate the topmost boolean value
not
; Consume and branch based on the topmost boolean value
if
; Entering this branch means the calculated hash value
; did not match with the given constant.
; Duplicate the tompost element (public key value)
dup
; Calculate the sha-256 digest from the topmost element
hash "sha256"
; Push another constant hash value of an accepted public key.
push hash "sha256" hex "677d5ceaa1d27779d5cb2e5ee68acae672c6594fdf9f50a0b79ca19d9664e807"
; Consume and compare the two topmost hash values and push
; the result back to the stack (true if equal, false otherwise).
equal
; Consume the topmost element and fail if the value is not true.
verify
end if
; At this point the stack is the same as in
; the beginning of the predicate.
; Use and consume the public key value (topmost element)
; to verify and consume the signature (second element on the stack)
; on the transaction. Pushes the boolean result
; back to the stack making it the only remaining value.
check_signature