Skip to content

Commit

Permalink
update Readme with Android APIs (#148)
Browse files Browse the repository at this point in the history
  • Loading branch information
gadphly authored Aug 17, 2023
1 parent 85a4473 commit 70e5458
Showing 1 changed file with 49 additions and 4 deletions.
53 changes: 49 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,70 @@ Trifle SDK is implemented on client side in iOS and Android and on server side i

```
// App start up
// swift
let trifle = try Trifle(reverseDomain: abc)
// android
// nop Trifle is an object
// Check if a key already exists.
// If no key exists, generate a public key pair
// swift
let keyHandle = try trifle.generateKeyHandle()
// android
var keyHandle = TrifleApi.generateKeyHandle(alias: abc)
// Storing keys. Keys are codable.
// swift
let encoder = JSONEncoder()
let jsonKeyHandle = try encoder.encode(keyHandle)
// android
val encoder = Gson()
let jsonKeyHandle = try encoder.toJson(keyHandle)
// Load the key from storage when we need to use it
// swift
let decoder = JSONDecoder()
let decoded = try decoder.decode(TrifleKeyHandle.self, from: jsonKeyHandle)
// android
// Check the validity of loaded key
// TODO: keyHandle.isValid()
// swift
let valid = trifle.isValid(keyHandle: keyHandle)
// android
TBD
// Destroy key that is no longer in use or is invalid
// TODO: keyHandle.destroy()
// swift
let status = trifle.delete(keyHandle: keyHandle)
// android
TBD
// Check if loaded key already has a cert. If yes, skip to checking for cert validity
// Else if key does not have a cert OR if a new cert must be generated (eg because of existing
// cert is already expired, or app needs to re-attest, app is re-installed, app is restored
// from backup, ... etc)
// Create cert request
// swift
let certReq = try trifle.generateMobileCertificateRequest(entity: entity, keyHandle: keyHandle)
// android
val certReq = TrifleApi.generateMobileCertificateRequest(entity = entity, keyHandle = keyHandle)
// Serialize to proto to be sent over wire
// swift
let encoded = try certReq.serialize()
// android
val encoded = certReq.serialize()
// Send certificate request to Certificate Authority endpoint. Response will be [Data]
let response: [Data]
// Iterate over each Data to convert to TrifleCertificate
// swift
let certs = try response.map({ try TrifleCertificate.deserialize(data: $0) })
// android
// certs is an array of certificates where [0] will be device certificate
// and the rest of the elements will be intermediate chain.
Expand All @@ -66,28 +92,47 @@ let certs = try response.map({ try TrifleCertificate.deserialize(data: $0) })
// Validate cert matches the certificate request (so generated key)
// and the root (so it has been generated by the right CA).
// swift
let isValid = certs[0].verify(
certificateRequest: certRequest,
intermediateTrifleChain: certs,
rootTrifleCertificate: root)
// android
// TBD var isValid = TrifleApi.verify(csr, certs, anchor)
// Once it passes validation, certReq is no longer needed and it can be deleted
// Store cert along with the respective keyHandle
// To check only for the validity of a stored cert, you can do either of below choices
// Option 1 is a more complete check of the device cert and the full chain
// swift
isValid = certs[0].verify(intermediateChain: certs )
// android
// TBD var isValid = TrifleApi.verify(certs)
// option 2 only checks the validity of the device cert
// swift
isValid = certs[0].verify(intermediateChain: [] )
// android
// TBD var isValid = TrifleApi.verify(certs)
// Sign the data
// swift
let trifleSignedData = try trifle.createSignedData(
data: dataThatIsSigned,
keyHandle: keyHandle,
certificates: certs )
// android
let trifleSignedData = TrifleApi.createSignedData(
dataThatIsSigned,
keyHandle,
certs )
// Serialize to proto to be sent over wire
// swift
let encodedTrifleSignedDataProto = try trifleSignedData.serialize()
// android
val encodedTrifleSignedDataProto = trifleSignedData.serialize()
```

## Key Lifecycle
Expand Down

0 comments on commit 70e5458

Please sign in to comment.