diff --git a/vdr/wasm/demo/node/src/main.ts b/vdr/wasm/demo/node/src/main.ts index 07b897b3..a0af1d6f 100644 --- a/vdr/wasm/demo/node/src/main.ts +++ b/vdr/wasm/demo/node/src/main.ts @@ -1,7 +1,7 @@ import fs from "fs"; import secp256k1 from "secp256k1"; -import { LedgerClient, EthrDidRegistry } from "indy2-vdr"; +import { LedgerClient, EthrDidRegistry, SchemaRegistry } from "indy2-vdr"; const chainId = 1337 const nodeAddress = 'http://127.0.0.1:8545' @@ -10,14 +10,27 @@ const didEthrRegistryConfig = { address: '0x0000000000000000000000000000000000018888', specPath: '/Users/indy-besu/smart_contracts/artifacts/contracts/did/EthereumExtDidRegistry.sol/EthereumExtDidRegistry.json' } +const schemaRegistryConfig = { + address: '0x0000000000000000000000000000000000005555', + specPath: '/Users/indy-besu/smart_contracts/artifacts/contracts/cl/SchemaRegistry.sol/SchemaRegistry.json' +} + const trustee = { address: '0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5', secret: Uint8Array.from([ 139, 187, 177, 179, 69, 175, 86, 181, 96, 165, 178, 11, 212, 176, 237, 28, 216, 204, 153, 88, 161, 98, 98, 188, 117, 17, 132, 83, 203, 84, 109, 247 ]) } const identity = { - address: '0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5', - secret: Uint8Array.from([17, 230, 90, 7, 166, 132, 10, 84, 177, 140, 139, 18, 254, 94, 235, 200, 171, 63, 41, 29, 76, 110, 166, 107, 86, 7, 198, 74, 34, 146, 157, 66]) + address: '0xce70ce892768d46caf120b600dec29ed20198982', + secret: Uint8Array.from([126, 218, 51, 235, 106, 56, 168, 226, 49, 234, 92, 61, 233, 13, 242, 75, 137, 130, 228, 222, 148, 239, 14, 63, 135, 13, 140, 163, 134, 166, 49, 50]) +} + +function sign(message: Uint8Array, key: Uint8Array) { + let signature = secp256k1.ecdsaSign(message, key) + return { + recovery_id: signature.recid, + signature: signature.signature + } } async function demo() { @@ -26,6 +39,10 @@ async function demo() { { "address": didEthrRegistryConfig.address, "spec": JSON.parse(fs.readFileSync(didEthrRegistryConfig.specPath, 'utf8')), + }, + { + "address": schemaRegistryConfig.address, + "spec": JSON.parse(fs.readFileSync(schemaRegistryConfig.specPath, 'utf8')), } ] const client = new LedgerClient(chainId, nodeAddress, contractConfigs, null) @@ -33,25 +50,12 @@ async function demo() { console.log('Status: ' + JSON.stringify(status, null, 2)) console.log('2. Publish and Modify DID') - - console.log('2.1 Publish Service DID Attribute') const did = 'did:ethr:' + identity.address const serviceAttribute = {"serviceEndpoint":"http://10.0.0.2","type":"TestService"} let endorsingData = await EthrDidRegistry.buildDidSetAttributeEndorsingData(client, did, serviceAttribute, BigInt(100000)) - let endorsingBytesToSign = endorsingData.getSigningBytes() - let signature = secp256k1.ecdsaSign(endorsingBytesToSign, identity.secret) - let authorSignature = { - recovery_id: signature.recid, - signature: signature.signature - } - + let authorSignature = sign(endorsingData.getSigningBytes(), identity.secret) let transaction = await EthrDidRegistry.buildDidSetAttributeSignedTransaction(client, trustee.address, did, serviceAttribute, BigInt(100000), authorSignature) - let bytesToSign = transaction.getSigningBytes() - signature = secp256k1.ecdsaSign(bytesToSign, trustee.secret) - let transactionSignature = { - recovery_id: signature.recid, - signature: signature.signature - } + let transactionSignature = sign(transaction.getSigningBytes(), trustee.secret) transaction.setSignature(transactionSignature) let txnHash = await client.submitTransaction(transaction) let receipt = await client.getReceipt(txnHash) @@ -60,6 +64,28 @@ async function demo() { console.log('3. Resolve DID Document') const didWithMeta = await EthrDidRegistry.resolveDid(client, did, null) console.log('Resolved DID Document: ' + JSON.stringify(didWithMeta, null, 2)) + + console.log('4. Publish Schema') + const name = (Math.random() + 1).toString(36).substring(7) + const schemaId = `did:ethr:test:${identity.address}/anoncreds/v0/SCHEMA/${name}/1.0.0` + const schema = { + "attrNames": [ "First Name", "Last Name" ], + "issuerId": `did:ethr:test:${identity.address}`, + "name": name, + "version": "1.0.0" + } + let schemaEndorsingData = await SchemaRegistry.buildCreateSchemaEndorsingData(client, schemaId, schema) + authorSignature = sign(schemaEndorsingData.getSigningBytes(), identity.secret) + transaction = await SchemaRegistry.buildCreateSchemaSignedTransaction(client, trustee.address, schemaId, schema, authorSignature) + transactionSignature = sign(transaction.getSigningBytes(), trustee.secret) + transaction.setSignature(transactionSignature) + txnHash = await client.submitTransaction(transaction) + receipt = await client.getReceipt(txnHash) + console.log(' Schema Transaction receipt: ' + receipt) + + console.log('5. Resolve Schema') + const resolvedSchema = await SchemaRegistry.resolveSchema(client, schemaId) + console.log(' Resolved Schema: ' + JSON.stringify(resolvedSchema, null, 2)) } async function main() { diff --git a/vdr/wrappers/python/demo/test.py b/vdr/wrappers/python/demo/test.py index 28fb55c1..a3643bc8 100644 --- a/vdr/wrappers/python/demo/test.py +++ b/vdr/wrappers/python/demo/test.py @@ -1,5 +1,8 @@ import asyncio import json +import secrets +import string + from eth_keys import keys from indy2_vdr import * @@ -9,16 +12,18 @@ node_address = 'http://127.0.0.1:8545' # address of deployed IndyDidRegistry smart contract did_contact_address = '0x0000000000000000000000000000000000018888' +schema_contact_address = '0x0000000000000000000000000000000000005555' # Path to the compiled IndyDidRegistry smart contract did_contact_spec_path = '/Users/indy-besu/smart_contracts/artifacts/contracts/did/EthereumExtDidRegistry.sol/EthereumExtDidRegistry.json' +schema_contact_spec_path = '/Users/indy-besu/smart_contracts/artifacts/contracts/cl/SchemaRegistry.sol/SchemaRegistry.json' # Account address to use for sending transactions trustee = { "address": '0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5', "secret": '8bbbb1b345af56b560a5b20bd4b0ed1cd8cc9958a16262bc75118453cb546df7' } identity = { - "address": '0xf0e2db6c8dc6c681bb5d6ad121a107f300e9b2b5', - "secret": '11e65a07a6840a54b18c8b12fe5eebc8ab3f291d4c6ea66b5607c64a22929d42' + "address": '0xce70ce892768d46caf120b600dec29ed20198982', + "secret": '7eda33eb6a38a8e231ea5c3de90df24b8982e4de94ef0e3f870d8ca386a63132' } @@ -32,13 +37,14 @@ def sign(secret: str, data: bytes): async def demo(): print("1. Init client") contract_configs = [ - ContractConfig(did_contact_address, did_contact_spec_path, None) + ContractConfig(did_contact_address, did_contact_spec_path, None), + ContractConfig(schema_contact_address, schema_contact_spec_path, None), ] client = LedgerClient(chain_id, node_address, contract_configs, None) status = await client.ping() - print('Status: ' + str(status)) + print(' Status: ' + str(status)) - print("2. Publish and Modify DID") + print("2. Publish DID") did = 'did:ethr:' + identity['address'] service_attribute = {"serviceEndpoint": "http://10.0.0.2", "type": "TestService"} endorsing_data = await build_did_set_attribute_endorsing_data(client, did, json.dumps(service_attribute), 1000) @@ -49,14 +55,38 @@ async def demo(): trustee_signature = sign(trustee["secret"], transaction.get_signing_bytes()) transaction.set_signature(trustee_signature) txn_hash = await client.submit_transaction(transaction) - print('Transaction hash: ' + bytes(txn_hash).hex()) + print(' Transaction hash: ' + bytes(txn_hash).hex()) receipt = await client.get_receipt(txn_hash) - print('Transaction receipt: ' + str(receipt)) + print(' Transaction receipt: ' + str(receipt)) print("3. Resolve DID Document") resolved_did_doc = await resolve_did(client, did, None) - print('Resolved DID Document:' + resolved_did_doc) + print(' Resolved DID Document:' + resolved_did_doc) + + print("4. Publish Schema") + name = ''.join(secrets.choice(string.ascii_uppercase + string.digits) for _ in range(6)) + schema_id = 'did:ethr:test:' + identity["address"] + '/anoncreds/v0/SCHEMA/' + name + '/1.0.0' + schema = { + "attrNames": ["First Name", "Last Name"], + "issuerId": 'did:ethr:test:' + identity["address"], + "name": name, + "version": "1.0.0" + } + endorsing_data = await build_create_schema_endorsing_data(client, schema_id, json.dumps(schema)) + identity_signature = sign(identity["secret"], endorsing_data.get_signing_bytes()) + transaction = await build_create_schema_signed_transaction(client, trustee["address"], schema_id, + json.dumps(schema), + identity_signature) + trustee_signature = sign(trustee["secret"], transaction.get_signing_bytes()) + transaction.set_signature(trustee_signature) + txn_hash = await client.submit_transaction(transaction) + print(' Transaction hash: ' + bytes(txn_hash).hex()) + receipt = await client.get_receipt(txn_hash) + print(' Transaction receipt: ' + str(receipt)) + print("5. Resolve Schema") + resolved_schema = await resolve_schema(client, schema_id) + print(' Resolved Schema:' + resolved_schema) if __name__ == "__main__": asyncio.get_event_loop().run_until_complete(demo())