Skip to content

Commit

Permalink
Python code sample doc updates (#553)
Browse files Browse the repository at this point in the history
  • Loading branch information
fundthmcalculus committed Apr 7, 2022
1 parent 322916b commit 756cecd
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
27 changes: 13 additions & 14 deletions docs/reference/services/credential-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ The Credential service supports signing data using [BBS+ Signatures <small>:mate
<!--/codeinclude-->

=== "Python"
<!--codeinclude-->
```python

[Issue Credential](../../../python/samples/vaccine_demo.py) inside_block:issueCredential
```
<!--/codeinclude-->

=== "Go"
```golang
Expand Down Expand Up @@ -67,9 +69,11 @@ The output of this method will be a signed JSON document using BBS+ Signature Su
<!--/codeinclude-->

=== "Python"
<!--codeinclude-->
```python

[Issue From Template](../../../python/samples/templates_demo.py) inside_block:issueFromTemplate
```
<!--/codeinclude-->

=== "Go"
```golang
Expand Down Expand Up @@ -179,18 +183,11 @@ The endpoint to create a proof requires two inputs:
<!--/codeinclude-->

=== "Python"

<!--codeinclude-->
```python
import json
frame_json = json.dumps({
"@context": "https://www.w3.org/2018/credentials/v1",
"type": [ "VerifiableCredential" ],
"@explicit": true,
"issuer": {}
})

presentation = wallet_services.create_proof(document_id, frame_json)
[CreateProof](../../../python/samples/vaccine_demo.py) inside_block:createProof
```
<!--/codeinclude-->

### Verify Proof

Expand All @@ -210,14 +207,16 @@ This endpoint verifies if the submitted data contains a valid proof. The data to
=== "C#"
<!--codeinclude-->
```csharp
[CreateProof](../../../dotnet/Tests/Tests.cs) inside_block:verifyProof
[VerifyProof](../../../dotnet/Tests/Tests.cs) inside_block:verifyProof
```
<!--/codeinclude-->

=== "Python"
<!--codeinclude-->
```python
valid = await wallet_service.verify_proof(presentation)
[VerifyProof](../../../python/samples/vaccine_demo.py) inside_block:verifyProof
```
<!--/codeinclude-->

### Exchange Credentials

Expand Down
6 changes: 4 additions & 2 deletions docs/reference/services/template-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ A template is a saved JSON-LD schema that is used to define/create credentials a
=== "C#"
<!--codeinclude-->
```csharp
[CreateProof](../../../dotnet/Tests/Tests.cs) inside_block:createTemplate
[CreateTemplate](../../../dotnet/Tests/Tests.cs) inside_block:createTemplate
```
<!--/codeinclude-->

=== "Python"
<!--codeinclude-->
```python

[CreateTemplate](../../../python/samples/templates_demo.py) inside_block:createTemplate
```
<!--/codeinclude-->

=== "Go"
```golang
Expand Down
14 changes: 10 additions & 4 deletions docs/reference/services/wallet-service.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ This method allows inserting any JSON data in the wallet.
```
<!--/codeinclude-->
=== "Python"
<!--codeinclude-->
```python
item_id = await wallet_service.insert_item(credential)
[Insert Item Wallet](../../../python/samples/vaccine_demo.py) inside_block:insertItemWallet
```
<!--/codeinclude-->

The output of this method will be a unique `itemId` that can be used as input where required.

## Search / Query
Expand Down Expand Up @@ -64,9 +67,11 @@ The default query used in the commands below returns a full wallet result set. T
<!--/codeinclude-->

=== "Python"
<!--codeinclude-->
```python
item = await wallet_service.search()
[Insert Item Wallet](../../../python/samples/vaccine_demo.py) inside_block:searchWallet
```
<!--/codeinclude-->

### SQL Search

Expand All @@ -92,10 +97,11 @@ To pass custom query to the search function, use the query parameter or the avai
<!--/codeinclude-->

=== "Python"
<!--codeinclude-->
```python
query = "SELECT * FROM c WHERE c.type = 'VerifiableCredential'"
item = await wallet_service.search(query)
[Insert Item Wallet](../../../python/samples/vaccine_demo.py) inside_block:searchWalletSQL
```
<!--/codeinclude-->


### Common SQL Queries
Expand Down
2 changes: 2 additions & 0 deletions python/samples/templates_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ async def templates_demo():
assert template.data.schema_uri is not None

# issue credential from this template
# issueFromTemplate() {
values = json.dumps({"firstName": "Jane", "lastName": "Doe", "age": 42})
issue_response = await credential_service.issue_from_template(
request=IssueFromTemplateRequest(
template_id=template.data.id, values_json=values
)
)
# }
json_document = json.loads(issue_response.document_json)
assert json_document is not None

Expand Down
26 changes: 19 additions & 7 deletions python/samples/vaccine_demo.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import asyncio
from os.path import abspath, join, dirname

from aiohttp import request

from trinsic.account_service import AccountService
from trinsic.credentials_service import CredentialsService
from trinsic.proto.services.account.v1 import SignInRequest
from trinsic.proto.services.universalwallet.v1 import InsertItemRequest
from trinsic.proto.services.universalwallet.v1 import InsertItemRequest, SearchRequest
from trinsic.proto.services.verifiablecredentials.v1 import (
IssueRequest,
CreateProofRequest,
Expand All @@ -26,17 +28,17 @@ def _vaccine_cert_unsigned_path() -> str:

def _vaccine_cert_frame_path() -> str:
return abspath(join(_base_data_path(), "vaccination-certificate-frame.jsonld"))


# }


async def vaccine_demo():
# createAccountService() {
account_service = AccountService(server_config=trinsic_config())
account = await account_service.sign_in()
provider_service = ProviderService(server_config=trinsic_config(account))
# }

# createProviderService() {
provider_service = ProviderService(server_config=trinsic_config(account))
ecosystem = await provider_service.create_ecosystem()
ecosystem_id = ecosystem.ecosystem.id
# }
Expand Down Expand Up @@ -73,17 +75,17 @@ async def vaccine_demo():
allison = fid.readline()
# }

# issueCredential() {
# Sign a credential as the clinic and send it to Allison
with open(_vaccine_cert_unsigned_path(), "r") as fid:
credential_json = "\n".join(fid.readlines())

# issueCredential() {
issue_response = await credentials_service.issue_credential(
request=IssueRequest(document_json=credential_json)
)
# }
credential = issue_response.signed_document_json
print(f"Credential: {credential}")
# }

# checkCredentialStatus() {
# status_id = credential['id']
Expand All @@ -94,14 +96,20 @@ async def vaccine_demo():
# storeCredential() {
# Alice stores the credential in her cloud wallet.
wallet_service.service_options.auth_token = allison
# insertItemWallet() {
insert_response = await wallet_service.insert_item(
request=InsertItemRequest(item_json=credential)
)
# }
item_id = insert_response.item_id
print(f"item id = {item_id}")
# searchWallet() {
wallet_items = await wallet_service.search()
print(f"last wallet item = {wallet_items.items[-1]}")
# }
# searchWalletSQL() {
wallet_items2 = await wallet_service.search(request=SearchRequest(query="SELECT c.id, c.type, c.data FROM c WHERE c.type = 'VerifiableCredential'"))
# }
print(f"last wallet item = {wallet_items.items[-1]}")

# shareCredential() {
# Allison shares the credential with the venue.
Expand All @@ -112,11 +120,13 @@ async def vaccine_demo():
with open(_vaccine_cert_frame_path(), "r") as fid2:
proof_request_json = "\n".join(fid2.readlines())

# createProof() {
proof_response = await credentials_service.create_proof(
request=CreateProofRequest(
reveal_document_json=proof_request_json, item_id=item_id
)
)
# }
credential_proof = proof_response.proof_document_json
print(f"Proof: {credential_proof}")
# }
Expand All @@ -125,9 +135,11 @@ async def vaccine_demo():
# The airline verifies the credential
credentials_service.service_options.auth_token = airline
wallet_service.service_options.auth_token = airline
# verifyProof() {
verify_result = await credentials_service.verify_proof(
request=VerifyProofRequest(proof_document_json=credential_proof)
)
# }
valid = verify_result.is_valid
print(f"Verification result: {valid}")
assert valid is True
Expand Down

0 comments on commit 756cecd

Please sign in to comment.