def transfer_presigned(self, params): sp.set_type( params, sp.TRecord(from_=sp.TAddress, to_=sp.TAddress, value=sp.TNat)) params_hash = sp.blake2b(sp.pack(params)) #unsigned = sp.blake2b(mi.operator("SELF; ADDRESS; CHAIN_ID; PAIR; PAIR; PACK", [sp.TPair(sp.TNat, sp.TBytes)], [sp.TBytes])(sp.pair(self.data.counter, params_hash))) permit_key = sp.pair(params.from_, params_hash) effective_expiry = sp.local("effective_expiry", 0) with sp.if_(self.data.permits.contains(permit_key)): permit_submission_timestamp = self.data.permits[permit_key] with sp.if_( self.data.permit_expiries.contains(permit_key) & self.data.permit_expiries[permit_key].is_some()): effective_expiry.value = self.data.permit_expiries[ permit_key].open_some() with sp.else_(): with sp.if_( self.data.user_expiries.contains(params.from_) & self.data.user_expiries[params.from_].is_some()): effective_expiry.value = self.data.user_expiries[ params.from_].open_some() with sp.else_(): effective_expiry.value = self.data.default_expiry # Deleting permit regardless of whether or not its expired with sp.if_( sp.as_nat(sp.now - permit_submission_timestamp) >= effective_expiry.value): # Expired self.delete_permit(permit_key) sp.result(sp.bool(False)) with sp.else_(): self.delete_permit(permit_key) sp.result(sp.bool(True)) with sp.else_(): sp.result(sp.bool(False))
def test(): alice = sp.test_account("Alice") bob = sp.test_account("Robert") scenario = sp.test_scenario() scenario.h1("Atomic Swap") # Here, two AtomicSwap contracts are created. One with Alice as the owner # and Bob as the counterparty, and the second with the identities reversed. # They are both secured with the same hash secret, so if the secret gets # revealed, then both swaps can happen. hashSecret = sp.blake2b(sp.bytes("0x12345678aabb")) c1 = AtomicSwap(sp.mutez(12), sp.timestamp(50), hashSecret, alice.address, bob.address) c2 = AtomicSwap(sp.mutez(20), sp.timestamp(50), hashSecret, bob.address, alice.address) scenario.h1("c1") scenario += c1 scenario += c1.knownSecret(secret = sp.bytes("0x12345678aa")).run(sender = bob, valid = False) scenario += c1.knownSecret(secret = sp.bytes("0x12345678aabb")).run(sender = bob) scenario.h1("c2") scenario += c2 scenario.h2("C2.export()") scenario.p(c2.export())
def changeStatus(self, params): # verify the secret to ensure that # the owner of the contract calls it sp.verify(sp.blake2b(params.secret) == self.data.hashedSecret) # set the owner self.data.covidStatus = params.covidStatus
def addOwner(self, params): # verify the secret to ensure that # the owner of the contract calls it sp.verify(sp.blake2b(params.secret) == self.data.hashedSecret) # set the owner self.data.owner = sp.some(sp.sender)
def add_secret(self, params): # make sure proof checks out sp.verify(sp.blake2b(params.proof) == self.data.hashedProof) # add secret to user SecretStore self.data.SecretStore.push(params.encryptedData) # increment nonce self.data.nonce = self.data.nonce + 1 # set new hashedSecret self.data.hashedProof = params.nexthashedProof
def test(): hashSecret = sp.blake2b(sp.bytes("0x12345678aabb")) alice = sp.test_account("Alice") bob = sp.test_account("Robert") c1 = AtomicSwap(sp.mutez(12), sp.timestamp(50), hashSecret, alice.address, bob.address) scenario = sp.test_scenario() scenario.h1("Atomic Swap") scenario += c1
def transfer_presigned(self, params): sp.set_type(params, sp.TRecord( from_=sp.TAddress, to_=sp.TAddress, value=sp.TNat)) params_hash = sp.blake2b(sp.pack(params)) #unsigned = sp.blake2b(mi.operator("SELF; ADDRESS; CHAIN_ID; PAIR; PAIR; PACK", [sp.TPair(sp.TNat, sp.TBytes)], [sp.TBytes])(sp.pair(self.data.counter, params_hash))) permit_key = sp.pair(params.from_, params_hash) effective_expiry = sp.local("effective_expiry", 0) sp.if self.data.permits.contains(permit_key): permit_submission_timestamp = self.data.permits[permit_key] sp.if self.data.permit_expiries.contains(permit_key) & self.data.permit_expiries[permit_key].is_some(): effective_expiry.value = self.data.permit_expiries[permit_key].open_some()
def addOwner(self, params): sp.set_type(params.ConfirmedCase,sp.TString) # verify the secret to ensure that # the owner of the contract calls it sp.verify(sp.blake2b(params.secret) == self.data.hashedSecret) sp.verify(params.ConfirmedCase=="No",message="Please isolate yourself and stay strong") # set the owner self.data.owner = sp.some(sp.sender)
def test(): key = '' initialNonce = 1 initialHashedProof = sp.blake2b( sp.bytes("0x62436d677a706263382f2b4f4a4d67367478745233513d3d") ) # 12345678aabc - UTF-8 c1 = SecretStore(initialNonce, initialHashedProof) # show its representation scenario = sp.test_scenario() scenario.h2("Contract") scenario += c1 # now store secret data encrypted with key using aes scenario.h1('Test Store Secret 1') encryptedData = 'abcdefgh' proof = sp.bytes("0x62436d677a706263382f2b4f4a4d67367478745233513d3d" ) # 12345678aabc - UTF-8 # create next token nexthashedProof = sp.blake2b( sp.bytes("0x313233343536373861626263")) # 12345678aabc - UTF-8 c2 = c1.add_secret(proof=proof, nexthashedProof=nexthashedProof, encryptedData=encryptedData).run() scenario += c2 # now store proof data encrypted with key using aes scenario.h1('Test Store Secret 2') encryptedData = 'qwertyuiop' proof = sp.bytes("0x313233343536373861626263") # 0x12345678aabc - UTF-8 # create next token nexthashedProof = sp.blake2b( sp.bytes("0x313233343536373861616264")) # 0x12345678aabd - UTF-8 c2 = c1.add_secret(proof=proof, nexthashedProof=nexthashedProof, encryptedData=encryptedData).run() scenario += c2
def runRace(self, params): # verify the secret to ensure that # the owner of the contract calls it sp.verify(sp.blake2b(params.secret) == self.data.hashedSecret) # select a winner winnerHorse = 1 self.data.winnerHorse = winnerHorse # compute pool of the winning horse sp.for bet in self.data.bets.values(): self.data.winningPool += bet.get(winnerHorse, sp.tez(0))
def transfer_presigned(self, params): sp.set_type(params, sp.TRecord( from_=sp.TAddress, to_=sp.TAddress, value=sp.TNat)) params_hash = sp.blake2b(sp.pack(params)) permit_key = sp.pair(params.from_, params_hash) sp.if self.data.permit_data.permits.contains(permit_key): permit_submission_timestamp = self.data.permit_data.permits[permit_key] effective_expiry = self.getEffectiveExpiry(permit_key) # Deleting permit regardless of whether or not its expired sp.if sp.as_nat(sp.now - permit_submission_timestamp) >= effective_expiry: # Expired self.delete_permit(permit_key) sp.result(sp.bool(False))
def addHorse(self, params): # verify the secret to ensure that # the owner of the contract calls it sp.verify(sp.blake2b(params.secret) == self.data.hashedSecret) # Setting types sp.set_type(params.horseId, sp.TInt) sp.set_type(params.horseName, sp.TString) # Adding the horse self.data.horses[params.horseId] = { "horseName": params.horseName }
def addBackground(self, params): sp.set_type(params.Age,sp.TString) sp.set_type(params.PostalCode,sp.TString) sp.set_type(params.ChronicDisease,sp.TString) sp.set_type(params.CommExposure,sp.TString) # verify the secret to ensure that # the owner of the contract calls it sp.verify(sp.blake2b(params.secret) == self.data.hashedSecret) # set the background self.data.Background[sp.sender]={ "Age":params.Age, "Postal Code":params.PostalCode, "Any Chronic Disease":params.ChronicDisease, "Student/living in communal setting":params.CommExposure }
def test(): scenario = sp.test_scenario() scenario.h1("Escrow") hashSecret = sp.blake2b(sp.bytes("0x01223344")) alice = sp.test_account("Alice") bob = sp.test_account("Bob") c1 = Escrow(alice.address, sp.tez(50), bob.address, sp.tez(4), sp.timestamp(123), hashSecret) scenario += c1 # Alice (owner) is adding some tez to the contract. scenario += c1.addBalanceOwner().run(sender=alice, amount=sp.tez(50)) # Bob (counterparty) is adding some tez to the contract. scenario += c1.addBalanceCounterparty().run(sender=bob, amount=sp.tez(4)) scenario.h3("Erronous secret") # Bob tries to claim the funds with an incorrect secret and fails. scenario += c1.claimCounterparty(secret=sp.bytes("0x01223343")).run( sender=bob, valid=False) scenario.h3("Correct secret") # This time Bob claims the funds with a correct secret and claims the total funds. scenario += c1.claimCounterparty(secret=sp.bytes("0x01223344")).run( sender=bob)
def addSympTest(self, params): # verify the secret to ensure that # the owner of the contract calls it sp.verify(sp.blake2b(params.secret) == self.data.hashedSecret) # Setting types sp.set_type(params.Time,sp.TInt) sp.set_type(params.Cough,sp.TString) sp.set_type(params.DiffBreathing, sp.TString) sp.set_type(params.Fever,sp.TString) sp.set_type(params.LossTasteSmell,sp.TString) sp.set_type(params.Diarrhoea,sp.TString) sp.set_type(params.Bodyache,sp.TString) sp.set_type(params.RunnyNose,sp.TString) sp.set_type(params.Sorethroat,sp.TString) sp.set_type(params.OverseasConfirmed,sp.TString) sp.set_type(params.Duration,sp.TString) # Adding the test results self.data.Records[params.Time] = { "Contact with Confirmed Case/Overseas in previous 14 days": params.OverseasConfirmed, "Cough": params.Cough, "Difficulty Breathing":params.DiffBreathing, "Fever > 37.5 degrees":params.Fever, "Loss of Taste/Smell":params.LossTasteSmell, "Diarrhoea":params.Diarrhoea, "Bodyache":params.Bodyache, "Runny Nose":params.RunnyNose, "Sore Throat":params.Sorethroat, "Duration of symptoms in Days":params.Duration, "Evaluated Risk":"" } #calculate risk sp.for item in self.data.Records[params.Time].items(): sp.if item.key=="Bodyache": sp.if item.value=="Yes": self.data.score += 3
def test(): scenario = sp.test_scenario() scenario.h1("FA1.2 template - Fungible assets") scenario.table_of_contents() # sp.test_account generates ED25519 key-pairs deterministically: admin = sp.test_account("Administrator") alice = sp.test_account("Alice") bob = sp.test_account("Robert") carlos = sp.test_account("Carlos") # Let's display the accounts: scenario.h1("Accounts") scenario.show([admin, alice, bob, carlos]) scenario.h1("Contract") c1 = FA12(admin.address) scenario.h1("Entry points") scenario += c1 scenario.h2("Admin mints a few coins") scenario += c1.mint(address=alice.address, value=12).run(sender=admin) scenario += c1.mint(address=alice.address, value=3).run(sender=admin) scenario += c1.mint(address=alice.address, value=3).run(sender=admin) scenario.verify(c1.data.balances[alice.address].balance == 18) scenario.h2("Alice transfers to Bob") scenario += c1.transfer(from_=alice.address, to_=bob.address, value=4).run(sender=alice) scenario.verify(c1.data.balances[alice.address].balance == 14) scenario.h2( "Bob tries to transfer from Alice but he doesn't have her approval" ) scenario += c1.transfer(from_=alice.address, to_=bob.address, value=4).run(sender=bob, valid=False) scenario.h2("Alice approves Bob and Bob transfers") scenario += c1.approve(spender=bob.address, value=5).run(sender=alice) scenario += c1.transfer(from_=alice.address, to_=bob.address, value=4).run(sender=bob) scenario.h2("Bob tries to over-transfer from Alice") scenario += c1.transfer(from_=alice.address, to_=bob.address, value=4).run(sender=bob, valid=False) scenario.h2("Admin burns Bob token") scenario += c1.burn(address=bob.address, value=1).run(sender=admin) scenario.verify(c1.data.balances[alice.address].balance == 10) scenario.h2("Alice tries to burn Bob token") scenario += c1.burn(address=bob.address, value=1).run(sender=alice, valid=False) scenario.h2( "Admin pauses the contract and Alice cannot transfer anymore") scenario += c1.setPause(True).run(sender=admin) scenario += c1.transfer(from_=alice.address, to_=bob.address, value=4).run(sender=alice, valid=False) scenario.verify(c1.data.balances[alice.address].balance == 10) scenario.h2("Admin transfers while on pause") scenario += c1.transfer(from_=alice.address, to_=bob.address, value=1).run(sender=admin) scenario.h2("Admin unpauses the contract and transfers are allowed") scenario += c1.setPause(False).run(sender=admin) scenario.verify(c1.data.balances[alice.address].balance == 9) scenario += c1.transfer(from_=alice.address, to_=bob.address, value=1).run(sender=alice) scenario.verify(c1.data.totalSupply == 17) scenario.verify(c1.data.balances[alice.address].balance == 8) scenario.verify(c1.data.balances[bob.address].balance == 9) scenario.h2("Permit Submissions") scenario += c1.mint(address=carlos.address, value=10).run(sender=admin) scenario.verify(c1.data.balances[carlos.address].balance == 10) # add permit for transfer of 10 from carlos to bob. alice submits with correct info and also calls. params_bytes_1 = sp.pack( sp.pair(carlos.address, sp.pair(bob.address, 10))) params_bytes_2 = sp.pack( sp.pair(bob.address, sp.pair(carlos.address, 10))) params_hash_1 = sp.blake2b(params_bytes_1) params_hash_2 = sp.blake2b(params_bytes_2) unsigned_1 = sp.pack( sp.pair(sp.pair(sp.chain_id_cst("0x9caecab9"), c1.address), sp.pair(0, params_hash_1))) signature_1 = sp.make_signature(secret_key=carlos.secret_key, message=unsigned_1, message_format="Raw") unsigned_2 = sp.pack( sp.pair(sp.pair(sp.chain_id_cst("0x9caecab9"), c1.address), sp.pair(1, params_hash_2))) signature_2 = sp.make_signature(secret_key=bob.secret_key, message=unsigned_2, message_format="Raw") scenario += c1.permit([ sp.pair(carlos.public_key, sp.pair(signature_1, params_hash_1)), sp.pair(bob.public_key, sp.pair(signature_2, params_hash_2)) ]).run(sender=alice, now=sp.timestamp(1571761674), chain_id=sp.chain_id_cst("0x9caecab9")) scenario.verify(c1.data.counter == 2) scenario.verify_equal( c1.data.permits[(sp.pair(carlos.address, params_hash_1))], sp.timestamp(1571761674)) scenario.verify_equal( c1.data.permits[(sp.pair(bob.address, params_hash_2))], sp.timestamp(1571761674)) scenario.h2("Execute transfer using permit") scenario += c1.transfer(from_=carlos.address, to_=bob.address, value=10).run(sender=bob, now=sp.timestamp(1571761674)) scenario.verify(c1.data.balances[carlos.address].balance == 0) scenario.verify(c1.data.balances[bob.address].balance == 19) # Permit deleted scenario.verify( ~c1.data.permits.contains(sp.pair(carlos.address, params_hash_1))) scenario += c1.transfer(from_=bob.address, to_=carlos.address, value=10).run(sender=carlos, now=sp.timestamp(1571761674)) scenario.verify(c1.data.balances[carlos.address].balance == 10) scenario.verify(c1.data.balances[bob.address].balance == 9) # Permit deleted scenario.verify( ~c1.data.permits.contains(sp.pair(bob.address, params_hash_2))) # Set Expiry to 0 and try to execute transfer at time less than submission_time + default_expiry, expect invalid transfer scenario.h2("Expired Permit") unsigned_3 = sp.pack( sp.pair(sp.pair(sp.chain_id_cst("0x9caecab9"), c1.address), sp.pair(2, params_hash_1))) signature_3 = sp.make_signature(secret_key=carlos.secret_key, message=unsigned_3, message_format="Raw") scenario += c1.permit([ sp.pair(carlos.public_key, sp.pair(signature_3, params_hash_1)) ]).run(sender=alice, now=sp.timestamp(1571761674), chain_id=sp.chain_id_cst("0x9caecab9")) scenario.verify(c1.data.counter == 3) scenario.verify_equal( c1.data.permits[(sp.pair(carlos.address, params_hash_1))], sp.timestamp(1571761674)) scenario += c1.setExpiry(address=carlos.address, seconds=0, permit=sp.some(params_hash_1)).run( sender=carlos, now=sp.timestamp(1571761674)) scenario.verify(c1.data.permit_expiries[sp.pair( carlos.address, params_hash_1)].open_some() == 0) scenario += c1.transfer(from_=carlos.address, to_=bob.address, value=10).run( sender=bob, now=sp.timestamp(1571761680), valid=False) # Uses later time stamp scenario.h2("Delete Expired Permit") scenario += c1.delete_permits([sp.pair(carlos.address, params_hash_1) ]).run(now=sp.timestamp(1571761680)) scenario.verify(~c1.data.permit_expiries.contains( sp.pair(carlos.address, params_hash_1))) scenario.verify( ~c1.data.permits.contains(sp.pair(carlos.address, params_hash_1))) scenario.h1("Views") scenario.h2("Balance") view_balance = Viewer(sp.TNat) scenario += view_balance scenario += c1.getBalance(arg=sp.record(owner=alice.address), target=view_balance.address) scenario.verify_equal(view_balance.data.last, sp.some(8)) scenario.h2("Administrator") view_administrator = Viewer(sp.TAddress) scenario += view_administrator scenario += c1.getAdministrator(target=view_administrator.address) scenario.verify_equal(view_administrator.data.last, sp.some(admin.address)) scenario.h2("Total Supply") view_totalSupply = Viewer(sp.TNat) scenario += view_totalSupply scenario += c1.getTotalSupply(target=view_totalSupply.address) scenario.verify_equal(view_totalSupply.data.last, sp.some(27)) scenario.h2("Allowance") view_allowance = Viewer(sp.TNat) scenario += view_allowance scenario += c1.getAllowance(arg=sp.record(owner=alice.address, spender=bob.address), target=view_allowance.address) scenario.verify_equal(view_allowance.data.last, sp.some(1)) scenario.h2("Counter") view_counter = Viewer(sp.TNat) scenario += view_counter scenario += c1.getCounter(target=view_counter.address) scenario.verify_equal(view_counter.data.last, sp.some(3)) scenario.h2("Default Expiry") view_defaultExpiry = Viewer(sp.TNat) scenario += view_defaultExpiry scenario += c1.getDefaultExpiry(target=view_defaultExpiry.address) scenario.verify_equal(view_defaultExpiry.data.last, sp.some(50000))
def test(): # A test scenario scenario = sp.test_scenario() # The hashed secret # input has to be valid hexadecimal hashedSecret = sp.blake2b(b'426c616168') # Create HTML output for debugging scenario.h1("Tezos betting app") # The test owner of the racing field raceOwner = sp.address("tz1-raceOwner-address-1234") # Initializing the app app = TezosBettingApp("FAST", hashedSecret) # Add the app to the scenario scenario += app # Adding owner scenario += app.addOwner(secret = b'426c616168').run(sender = raceOwner) scenario += app.changeTrack(secret = b'426c616168', track="SLOW") # Adding horses scenario.h2("Horses on the field!") scenario += app.addHorse( secret = b'426c616168', horseId = 0, horseName = "Pickle Rake" ).run(sender = raceOwner) scenario += app.addHorse( secret = b'426c616168', horseId = 1, horseName = "Seabiscuit" ).run(sender = raceOwner) scenario += app.addHorse( secret = b'426c616168', horseId = 2, horseName = "Blaze" ).run(sender = raceOwner) scenario += app.addHorse( secret = b'426c616168', horseId = 3, horseName = "Shadowfax" ).run(sender = raceOwner) # Placing bets scenario.h2("Placing bets") participantOne = sp.address("tz1-participantOne-address-1234") participantTwo = sp.address("tz1-participantTwo-address-1234") scenario += app.placeBet(betHorseId = 2).run( sender = participantOne, amount = sp.tez(3) ) scenario += app.placeBet(betHorseId = 1).run( sender = participantTwo, amount = sp.tez(5) ) # Run the race scenario.h2("Run the race") scenario += app.runRace(secret = b'426c616168').run(sender = raceOwner)
def test(): # A test scenario scenario = sp.test_scenario() # The hashed secret # input has to be valid hexadecimal hashedSecret = sp.blake2b(b'426c616168') # Create HTML output for debugging scenario.h1("COVID-19 Symptom Checker app") # The test owner who uses the app recordsOwner = sp.address("tz1N6VKpVbvXZ17UKm6RH8wJDvo5EWbCgMQB") # Initializing the app app = TezosSymptomChecker(hashedSecret, "Negative") # Add the app to the scenario scenario += app scenario.h2("User added to system") # Adding owner scenario +=app.addOwner( secret = b'426c616168', ConfirmedCase="No" ).run(sender = recordsOwner) # User sets background scenario.h2("User sets personal background") scenario += app.addBackground( secret = b'426c616168', Age = '15', PostalCode = "510122", ChronicDisease="No", CommExposure="Yes" ).run(sender = recordsOwner) # Changes to user's background scenario.h2("User changes personal background") scenario += app.addBackground( secret = b'426c616168', Age = '13-16', PostalCode = "510122", ChronicDisease="Yes", CommExposure="Yes" ).run(sender = recordsOwner) # Changes to user's background scenario.h2("User changes COVID status") scenario += app.changeStatus( secret = b'426c616168', covidStatus = "Positive" ) scenario += app.changeStatus( secret = b'426c616168', covidStatus = "Negative" ) # User takes symptom test scenario.h2("Checking of Symptoms") timeNow = int(time.time()) scenario += app.addSympTest( secret = b'426c616168', OverseasConfirmed="No", Cough="No", DiffBreathing="No", Time=timeNow, Fever="Yes", LossTasteSmell="No", Diarrhoea="Yes", Bodyache="No", RunnyNose="Yes", Sorethroat="No", Duration="5-7 days" ).run(sender = recordsOwner)
def claimCounterparty(self, params): sp.verify(sp.now < self.data.epoch) sp.verify(self.data.hashedSecret == sp.blake2b(params.secret)) self.claim(self.data.counterparty)
def knownSecret(self, params): self.checkAlive(self.data.counterparty) sp.verify(self.data.hashedSecret == sp.blake2b(params.secret)) sp.send(self.data.counterparty, self.data.notional) self.finish()
def makeCommitment(_name, _owner, _nonce): return sp.blake2b(sp.pack(sp.record(name = _name, owner = _owner, nonce = _nonce)))