Skip to content

Commit

Permalink
Merge pull request #52 from provenant-dev/feat/add-registries
Browse files Browse the repository at this point in the history
Adding Schemas, Registries and Challenges interfaces
  • Loading branch information
rodolfomiranda authored Jun 16, 2023
2 parents 5cedb79 + e751be8 commit e6b0b2f
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 11 deletions.
4 changes: 3 additions & 1 deletion examples/signify-react-ts/src/TestsComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import { Delegation } from "./test_components/Delegation";
import { Witnesses } from "./test_components/Witnesses";
import { Multisig } from "./test_components/Multisig";
import { Credentials } from "./test_components/Credentials";
import { Challenges } from "./test_components/Challenges";
export function TestsComponent() {

return (
<>
<>
< Challenges />
< Credentials />
< Salty />
< Randy />
Expand Down
66 changes: 66 additions & 0 deletions examples/signify-react-ts/src/test_components/Challenges.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import { SignifyClient, ready, Serder, Diger, MtrDex, CredentialTypes } from "signify-ts";
import { strict as assert } from "assert";
import { useState, useEffect } from 'react';


export function Challenges() {
const [testResult, setTestResult] = useState('');
useEffect(() => {
ready().then(() => {
console.log("signify client is ready")
})
}, [])

return (
<>
<div className="card">
<button
onClick={async () => {
try {
const url = "http://localhost:3901"
const bran1 = '0123456789abcdefghijk'
const bran2 = '1123456789abcdefghijk'
const client1 = new SignifyClient(url, bran1)
// await client1.boot()
await client1.connect()
const identifiers1 = client1.identifiers()
let challenges1 = client1.challenges()
let challenge1_small = await challenges1.get_challenge(128)
assert.equal(challenge1_small.words.length, 12)
let challenge1_big = await challenges1.get_challenge(256)
assert.equal(challenge1_big.words.length, 24)
let op1 = await identifiers1.create('alex', {})
let aid1 = op1['response']

const client2 = new SignifyClient(url, bran2)
// await client2.boot()
await client2.connect()
const identifiers2 = client2.identifiers()
const challenges2 = client2.challenges()
let op2 = await identifiers2.create('rodo', {})
let aid2 = op2['response']
console.log(aid2)


let challenge_to_send = await challenges1.send_challenge('alex', aid2.d, challenge1_small)
console.log(challenge_to_send)
await setTimeout(() => { }, 3000)//TODO: better way of checking if the challenge was received

let challenge_to_receive = await challenges2.accept_challenge('rodo', aid1.d, challenge_to_send.d)
console.log(challenge_to_receive)
assert.equal(challenge_to_receive.status,202)
setTestResult("Passed")
}
catch (e) {
console.log(e)
setTestResult("Failed")
}
}} >Challenges Test</button>{testResult}
</div>
</>
)
}


1 change: 1 addition & 0 deletions src/keri/app/habery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {Serder} from "../core/serder";
export class TraitCodex {
EstOnly: string = 'EO' // Only allow establishment events
DoNotDelegate: string = 'DND' // Dot not allow delegated identifiers
NoBackers: string = 'NB' // Do not allow backers
}

export const TraitDex = new TraitCodex()
Expand Down
149 changes: 139 additions & 10 deletions src/keri/app/signify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { incept, rotate, interact, reply } from "../core/eventing"
import { b, Serials, Versionage } from "../core/core";
import { Tholder } from "../core/tholder";
import { MtrDex } from "../core/matter";
import { TraitDex } from "./habery";

const KERIA_BOOT_URL = "http://localhost:3903"

Expand Down Expand Up @@ -256,30 +257,42 @@ export class SignifyClient {
credentials() {
return new Credentials(this)
}

registries() {
return new Registries(this)
}

schemas() {
return new Schemas(this)
}

challenges() {
return new Challenges(this)
}
}

class Identifier {
public client: SignifyClient
constructor(client: SignifyClient) {
this.client = client
}

//GET IdentifierCollectionEnd
async list_identifiers() {
let path = `/identifiers`
let data = null
let method = 'GET'
let res = await this.client.fetch(path, method, data, undefined)
return await res.json()
}

//GET
async get_identifier(name: string) {
let path = `/identifiers/${name}`
let data = null
let method = 'GET'
let res = await this.client.fetch(path, method, data, undefined)
return await res.json()
}

//POST
async create(name: string,
kargs: {
transferable: boolean,
Expand Down Expand Up @@ -421,7 +434,7 @@ class Identifier {
let res = await this.client.fetch("/identifiers", "POST", jsondata, undefined)
return res.json()
}

//PUT IdentifierResourceEnd
async interact(name: string, data: any | undefined = undefined) {

let hab = await this.get_identifier(name)
Expand All @@ -447,7 +460,7 @@ class Identifier {
return res.json()

}

//PUT IdentifierResourceEnd
async rotate(
name: string,
kargs: {
Expand Down Expand Up @@ -532,7 +545,7 @@ class Identifier {
let res = await this.client.fetch("/identifiers/" + name, "PUT", jsondata, undefined)
return res.json()
}

//POST EndRoleCollectionEnd
async addEndRole(name: string, role: string, eid: string | undefined) {
const hab = await this.get_identifier(name)
const pre = hab["prefix"]
Expand All @@ -550,7 +563,7 @@ class Identifier {
return res.json()

}

//POST /end/role/add
makeEndRole(pre: string, role: string, eid: string | undefined) {
const data: any = {
cid: pre,
Expand Down Expand Up @@ -672,12 +685,13 @@ class KeyStates {
}
}


class Credentials {
public client: SignifyClient
constructor(client: SignifyClient) {
this.client = client
}
//CredentialCollectionEnd
//todo rename to list_credentials
async list(aid: string, typ: CredentialTypes, schema: string) {
let path = `/aids/${aid}/credentials`
//if type is not in the credential types, throw an error
Expand All @@ -697,18 +711,133 @@ class Credentials {
let res = await this.client.fetch(path, method, null, undefined)
return await res.json()
}

//CredentialResourceEnd
//rename get_credential
async export(aid: string, said: string) {
let path = `/aids/${aid}/credentials/${said}`
let method = 'GET'
let headers = new Headers({
'Accept': 'application/json+cesr'

})
let res = await this.client.fetch(path, method, null, headers)
return await res.text()

}


}

class Registries {
public client: SignifyClient
constructor(client: SignifyClient) {
this.client = client
}

async list() {
let path = `/registries`
let method = 'GET'
let res = await this.client.fetch(path, method, null, undefined)
return await res.json()

}
async create(name: string, alias: string, toad: number, nonce: string, baks: [string], estOnly: boolean, noBackers: string = TraitDex.NoBackers) {
let path = `/registries`
let method = 'POST'
let data = {
name: name,
alias: alias,
toad: toad,
nonce: nonce,
noBackers: noBackers,
baks: baks,
estOnly: estOnly
}
let res = await this.client.fetch(path, method, data, undefined)
return await res.json()
}

}

class Schemas {
client: SignifyClient
constructor(client: SignifyClient) {
this.client = client
}
//SchemaResourceEnd
async get_schema(said: string) {
let path = `/schemas/${said}`
let method = 'GET'
let res = await this.client.fetch(path, method, null, undefined)
return await res.json()
}

//SchemaCollectionEnd

async list_all_schemas() {
let path = `/schemas`
let method = 'GET'
let res = await this.client.fetch(path, method, null, undefined)
return await res.json()
}



}

class Challenges {
client: SignifyClient
constructor(client: SignifyClient) {
this.client = client
}
//ChallengeCollectionEnd
async get_challenge(strength: number = 128) {
let path = `/challenges?strength=${strength.toString()}`
let method = 'GET'
let res = await this.client.fetch(path, method, null, undefined)
return await res.json()
}
//ChallengeResourceEnd
async send_challenge(name: string, recipient: string, words: [string]) {
let path = `/challenges/${name}`
let method = 'POST'

let hab = await this.client.identifiers().get_identifier(name)
let pre: string = hab["prefix"]
let state = hab["state"]
let sn = Number(state["s"])
let dig = state["d"]

let serder = interact({ pre: pre, dig: dig, sn: sn + 1, data: words, version: undefined, kind: undefined })
let keeper = this.client!.manager!.get(hab)
let sigs = keeper.sign(b(serder.raw))

let data = {
recipient: recipient,
words: words,
exn: serder.ked,
sig: sigs,
}

let resp = await this.client.fetch(path, method, data, undefined)
if (resp.status === 202) {
return serder.ked
}
else {
return resp
}
}
//ChallengeResourceEnd
async accept_challenge(name: string, aid: string, said: string) {
let path = `/challenges/${name}`
let method = 'PUT'
let data = {
aid: aid,
said: said
}
let res = await this.client.fetch(path, method, data, undefined)

return res
}

}

0 comments on commit e6b0b2f

Please sign in to comment.