-
Notifications
You must be signed in to change notification settings - Fork 0
/
altme_on_chain.py
108 lines (96 loc) · 3.64 KB
/
altme_on_chain.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import requests
import logging
import json
logging.basicConfig(level=logging.INFO)
def register_tezid(address, id, network, mode) :
# check if one proof already registered for this address
url = 'https://tezid.net/api/' + network + '/proofs/' + address
r = requests.get(url)
logging.info("check if one proof exists for address %s", address)
if not 199<r.status_code<300 :
logging.error("API call to TezID rejected %s", r.status_code)
return False # API failed
if not r.json() : # this address has no proof registered
if register_proof_type(address, id, network, mode) :
logging.error("The proof %s is now registered for %s", id, address)
return True
else :
return # failed to register
else :
proof_registered = False
for proof in r.json() :
if proof['id'] == id and proof['verified'] :
proof_registered = True
logging.info('The proof %s already exists for %s', id, address)
break
if not proof_registered :
if register_proof_type(address, id, network, mode) :
logging.error("The proof %s is now registered for %s", id, address)
return True
else :
return # failed to register
else :
return True
def register_proof_type(address, proof_type, network, mode) :
#[{"id":"test_type","label":"Test_type","meta":{"issuer":"altme"},"verified":true,"register_date":"2022-12-03T11:16:30Z"}]
url = 'https://tezid.net/api/' + network + '/issuer/altme'
headers = {
'Content-Type' : 'application/json',
'tezid-issuer-key' : mode.tezid_issuer_key
}
data = {
"address": address,
"prooftype": proof_type,
"register": True
}
r = requests.post(url, headers=headers, data=json.dumps(data))
logging.info("Register proof : status code = %s", r.status_code)
if not 199<r.status_code<300 :
logging.error("API call to TezID rejected %s", r.status_code)
return False
else :
return True
def issue_sbt(address, metadata, credential_id, mode) :
metadata_ipfs = add_to_ipfs(metadata, "sbt:" + credential_id , mode)
if metadata_ipfs :
metadata_ipfs_url = "ipfs://" + metadata_ipfs
else :
return None
url = 'https://altme-api.dvl.compell.io/mint'
headers = {
"Content-Type": "application/x-www-form-urlencoded"
}
data = {
"transfer_to" : address,
"ipfs_url" : metadata_ipfs_url
}
resp = requests.post(url, data=data, headers=headers)
if not 199<resp.status_code<300 :
logging.warning("Get access refused, SBT not sent %s", resp.status_code)
return None
return True
def add_to_ipfs(data_dict, name, mode) :
api_key = mode.pinata_api_key
secret = mode.pinata_secret_api_key
headers = {
'Content-Type': 'application/json',
'pinata_api_key': api_key,
'pinata_secret_api_key': secret}
data = {
'pinataMetadata' : {
'name' : name
},
'pinataContent' : data_dict
}
r = requests.post('https://api.pinata.cloud/pinning/pinJSONToIPFS', data=json.dumps(data), headers=headers)
if not 199<r.status_code<300 :
logging.warning("POST access to Pinatta refused")
return None
else :
return r.json()['IpfsHash']
if __name__ == '__main__':
# ghostnet KT1K2i7gcbM9YY4ih8urHBDbmYHLUXTWvDYj
import environment
myenv='local'
mode = environment.currentMode(myenv)
register_tezid("tz1iQNe71wzVCCL5YUSniJekP3qf9cmDosJU", "tezotopia_membershipcard", "ghostnet", mode)