Skip to content

Instantly share code, notes, and snippets.

@wgwz
Created September 1, 2022 20:55
Show Gist options
  • Save wgwz/185c62fc70b8064c0a44fdcf09110222 to your computer and use it in GitHub Desktop.
Save wgwz/185c62fc70b8064c0a44fdcf09110222 to your computer and use it in GitHub Desktop.
pyld-play
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "ee81ba5d-5f7e-4701-95df-13af154c2e1c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"from pyld import jsonld\n",
"import json"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "abc9bd93-7a7b-4dff-9248-a0db2cdefdf2",
"metadata": {},
"outputs": [],
"source": [
"doc = {\n",
" \"@context\": {\n",
" \"schema\": \"http://schema.org/\",\n",
" \"regen\": \"http://regen.network/\",\n",
" \"regen:additionalCertifications\": { \"@container\": \"@list\" }\n",
" },\n",
" \"@type\": \"regen:C01-CreditBatch\",\n",
" \"regen:vcsRetirementSerialNumber\": \"5531-243019420-243019439-VCU-048-MER-CD-14-934-01012015-31122015-1\",\n",
" \"regen:vcsProjectId\": 934,\n",
" \"regen:additionalCertifications\": [\n",
" {\n",
" \"schema:name\": \"CCB-Gold\",\n",
" \"schema:url\": {\n",
" \"@type\": \"schema:URL\",\n",
" \"@value\": \"https://verra.org/project/ccb-program/\"\n",
" }\n",
" }\n",
" ]\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "b545ae5a-f6cd-4bd1-89a6-56ed59f86e0c",
"metadata": {},
"outputs": [],
"source": [
"normalized = jsonld.normalize(\n",
" doc, {'algorithm': 'URDNA2015', 'format': 'application/n-quads'})"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "b703693b-244a-4451-a70a-956d442664c6",
"metadata": {},
"outputs": [],
"source": [
"out = [x for x in normalized.split('\\n') if len(x)>0]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "9e40de7a-023c-4040-a2c7-dfe9d92df8d1",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['_:c14n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> _:c14n1 .',\n",
" '_:c14n0 <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> .',\n",
" '_:c14n1 <http://schema.org/name> \"CCB-Gold\" .',\n",
" '_:c14n1 <http://schema.org/url> \"https://verra.org/project/ccb-program/\"^^<http://schema.org/URL> .',\n",
" '_:c14n2 <http://regen.network/additionalCertifications> _:c14n0 .',\n",
" '_:c14n2 <http://regen.network/vcsProjectId> \"934\"^^<http://www.w3.org/2001/XMLSchema#integer> .',\n",
" '_:c14n2 <http://regen.network/vcsRetirementSerialNumber> \"5531-243019420-243019439-VCU-048-MER-CD-14-934-01012015-31122015-1\" .',\n",
" '_:c14n2 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://regen.network/C01-CreditBatch> .']"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"out"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "05e84fdb-86de-412d-b7c7-390d4c2dfe0f",
"metadata": {},
"outputs": [],
"source": [
"from pymerkle import MerkleTree\n",
"\n",
"tree = MerkleTree()"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "882cc905-3262-4991-bea0-ce5260ab4162",
"metadata": {},
"outputs": [],
"source": [
"for data in out:\n",
" tree.encrypt(data)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "d0c27b41-dc6b-4d89-83cc-7fc8397f8271",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\n",
" uuid : cdabcf2c-2a35-11ed-be93-c26700a3bae5\n",
"\n",
" hash-type : SHA256\n",
" encoding : UTF-8\n",
" security : ACTIVATED\n",
"\n",
" root-hash : 27d65b5e0a7f49c3508fce7bd2010961281795f0894924cb23e1f1d64b81d4f1\n",
"\n",
" length : 8\n",
" size : 15\n",
" height : 3"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tree"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "17d078f6-18eb-4bf0-9931-92ceb1fb1920",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"b'0897a746b0fa6437bd5673b1893d9803799aa2e47fe152d8f97514d645d446fd'\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from pymerkle.hashing import HashEngine\n",
"challenge = HashEngine(**tree.get_config()).hash('_:c14n1 <http://schema.org/name> \"CCB-Gold\" .')\n",
"print(challenge)\n",
"proof = tree.generate_audit_proof(challenge)\n",
"proof.verify()"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "792d1fdb-1e6f-48a8-9e6d-78b1d371e8c0",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
PyLD==2.0.3
pymerkle==3.0.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment