Creating and Verifying Pods
CCP have provided an example for creating and verifying PODs. The below is a slight refinement of these examples I used to understand what was going on.
Prerequisites
You will need to have setup your tools in line with Setting Up Your Tools from CCP, specifically you will need the following:
node v18(earlier and later versions are not supported)pnpm v8orv9(later versions are likely to work but are untested)
Check your versions with:
Step 1: Setup the Project
First create a folder, for example ~/source/pod-examples, from your shell:
Install `@pcd/pod’ and TypeScript:
Step 2: Generate a Key Pair
Create a file named keygen.ts and add the following at the top:
This imports four libraries including a Cryptographically Secure Pseudo Random Number Generator, a function from the PODs SDK to derive a keypair and then two libraries for accessing the file system.
In the same file, add code to generate some random bytes using the CSPRNG this is effectively a huge number in the order of 2^256:
We need to derive a private key from this huge number, which we do by creating a private signing key:
Important
Any key that is generated for production must be appropriately secured, either by deriving it from an otherwise secure form of cryptographic randomness, or by deriving the random bytes from a recovery phrase or other form of cryptographic data. Alternatively the private key can be stored encrypted with a password in a database.
and the public signing key:
We can then then write these out to files and the console, so that we can use them to create and subsequently verify the POD:
Save the changes to the file, and then run this keygen command with:
Step 3: Create the POD
Create a new file named createPod.ts and add the following imports at the top, again we are pulling in the POD SDK and the utilities to read and write files:
Now comes the interesting bit, we create the POD itself:
This creates an object as follows:
security_levelwhich is an integer (i.e. a whole number) with a value of4.holder_smart_character_addresswhich is a string and represents the public key / address of the holder.issued_datewhich is a date expressed as an ISO 8601 format representing the time the POD was created.expiry_datawhich is a date expressed as an ISO 8601 format representing the point in time when this pod becomes invalid.pod_typeis astringcontaining a namespaced typeId, as this is just a string it could be anything but typically forms a dotted hierarchy from left to right.
Next we do some work to load the key pair from disk:
Then we print the public signing key:
Note
The step above is largely redundant as we already know this public key from the keygen.ts step; it is however useful to have this printed here as a validation and debugging step as an unexpected key would break the verification step below.
Before we create the POD itself:
And serialize it to a JSON string:
We then print the values to the console and write the pod to pod.json:
Save the changes to the file and run it with:
Step 4: Verify the POD
To verify the POD you will need to create a third file verifyPod.ts and again import some functions from the POD SDK and the fs and path libraries:
Do the work to load the POD and the public key from the files:
You can then parse the POD and load it into the SDK to get a POD in-memory:
The POD type has several utility functions on it allowing for POD operations, for example .verifySignature() can be used to verify the POD:
By checking the signerPublicKey from a known trusted source you can validate that it was signed by a trusted party:
In the same spirit of caution and trust, you can compare the podType (or any other known constant values) to ensure that they match the expected type:
Finally extract the security_level value from the POD and echo it to the console:
Save the changes and the file and run it with:
Summary
In this article we have used TypeScript to:
- ✅ Generate a public/private key pair,
- ✅ Create a POD and sign it with the private key,
- ✅ Verify the same POD with the public key.
I’m going to be using this as a Kata to help me better understand the process by repeating the exercise every couple of days. This will help me develop more familiarity with POD and its SDKs and help me build a new mental model, why don’t you join me in this practice by re-implementing it yourself tomorrow.