-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from NillionNetwork/feat/signature-examples
chore: add signatures examples.
- Loading branch information
Showing
10 changed files
with
217 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,7 @@ | |
"name": "@nillion/client-react-hooks", | ||
"license": "MIT", | ||
"author": "[email protected]", | ||
"version": "0.3.0-rc.0", | ||
"version": "0.3.0-rc.1", | ||
"homepage": "https://nillion.pub/client-ts", | ||
"repository": { | ||
"type": "git", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
"use client"; | ||
|
||
import { useNilInvokeCompute, useNillion } from "@nillion/client-react-hooks"; | ||
import { Uuid } from "@nillion/client-vms"; | ||
import { type ChangeEvent, type FC, useState } from "react"; | ||
|
||
export const EcdsaSign: FC = () => { | ||
const { client } = useNillion(); | ||
const mutation = useNilInvokeCompute(); | ||
const [id, setId] = useState(""); | ||
const isValidUuid = Uuid.safeParse(id).success; | ||
|
||
const tecdsaProgramId = "builtin/tecdsa_sign"; | ||
|
||
function handleChange(event: ChangeEvent<HTMLInputElement>): void { | ||
setId(event.target.value); | ||
} | ||
|
||
function handleClick(): void { | ||
// Assumes addition_division.py | ||
const options = { | ||
programId: tecdsaProgramId, | ||
inputBindings: [ | ||
{ party: "tecdsa_key_party", user: client.id }, | ||
{ party: "tecdsa_digest_message_party", user: client.id }, | ||
], | ||
outputBindings: [{ party: "tecdsa_output_party", users: [client.id] }], | ||
valueIds: [id], | ||
computeTimeValues: [], | ||
}; | ||
mutation.execute(options); | ||
} | ||
|
||
let resultId = ""; | ||
if (mutation.isSuccess) { | ||
resultId = mutation.data; | ||
} else if (mutation.isError) { | ||
resultId = mutation.error.message; | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Invoke Compute</h2> | ||
<ol> | ||
<li>Status: {mutation.status}</li> | ||
<li> Program id: {tecdsaProgramId}</li> | ||
<li> | ||
Id: <input type="text" value={id} onChange={handleChange} /> | ||
</li> | ||
<li>Compute result id: {resultId}</li> | ||
</ol> | ||
<button type="button" disabled={!isValidUuid} onClick={handleClick}> | ||
Execute | ||
</button> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
"use client"; | ||
|
||
import { useNilStoreValues } from "@nillion/client-react-hooks"; | ||
import { NadaValue } from "@nillion/client-vms"; | ||
import { secp256k1 } from "@noble/curves/secp256k1"; | ||
import { sha256 } from "@noble/hashes/sha2"; | ||
import { bytesToHex } from "@noble/hashes/utils"; | ||
import type { FC } from "react"; | ||
|
||
export const StoreSignatureValues: FC = () => { | ||
const storeValuesMutation = useNilStoreValues(); | ||
|
||
let id = ""; | ||
if (storeValuesMutation.isSuccess) { | ||
id = storeValuesMutation.data; | ||
} else if (storeValuesMutation.isError) { | ||
id = storeValuesMutation.error.message; | ||
} | ||
|
||
const privateKey: Uint8Array = secp256k1.utils.randomPrivateKey(); | ||
const digestMessage = sha256("A deep message with a deep number: 42"); | ||
|
||
const options = { | ||
values: [ | ||
{ | ||
name: "tecdsa_private_key", | ||
value: NadaValue.new_ecdsa_private_key(privateKey), | ||
}, | ||
{ | ||
name: "tecdsa_digest_message", | ||
value: NadaValue.new_ecdsa_digest_message(digestMessage), | ||
}, | ||
], | ||
ttl: 1, | ||
}; | ||
|
||
const stringifiedOptions = JSON.stringify({ | ||
...options, | ||
values: options.values.map(({ name, value }) => ({ | ||
name, | ||
value: { | ||
type: value.type_name(), | ||
value: bytesToHex(value.to_byte_array()), | ||
}, | ||
})), | ||
}); | ||
|
||
function handleClick(): void { | ||
storeValuesMutation.execute(options); | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Store Signature Values</h2> | ||
<ol> | ||
<li>Status: {storeValuesMutation.status}</li> | ||
<li>Options: {stringifiedOptions}</li> | ||
<li>Store Id: {id}</li> | ||
</ol> | ||
<button type="button" onClick={handleClick}> | ||
Execute | ||
</button> | ||
</div> | ||
); | ||
}; |
49 changes: 49 additions & 0 deletions
49
examples-nextjs/app/components/update-ecdsa-permissions.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
"use client"; | ||
|
||
import { | ||
useNilUpdatePermissions, | ||
useNillion, | ||
} from "@nillion/client-react-hooks"; | ||
import { UpdatePermissionsBuilder, Uuid } from "@nillion/client-vms"; | ||
import { type ChangeEvent, type FC, useMemo, useState } from "react"; | ||
|
||
export const UpdateEcdsaPermissions: FC = () => { | ||
const { client } = useNillion(); | ||
const mutation = useNilUpdatePermissions(); | ||
const [id, setId] = useState(""); | ||
const isValidUuid = Uuid.safeParse(id).success; | ||
|
||
const tecdsaProgramId = "builtin/tecdsa_sign"; | ||
|
||
const permissions = useMemo(() => { | ||
return UpdatePermissionsBuilder.init(client).grantCompute( | ||
client.id, | ||
tecdsaProgramId, | ||
); | ||
}, [client.id]); | ||
|
||
function handleChange(event: ChangeEvent<HTMLInputElement>): void { | ||
setId(event.target.value); | ||
} | ||
|
||
function handleClick(): void { | ||
const options = { id, permissions }; | ||
mutation.execute(options); | ||
} | ||
|
||
return ( | ||
<div> | ||
<h2>Update ECDSA Sign Permissions</h2> | ||
<ol> | ||
<li>Status: {mutation.status}</li> | ||
<li>New permissions: {JSON.stringify(permissions.toObject())}</li> | ||
<li> | ||
Id: <input type="text" value={id} onChange={handleChange} /> | ||
</li> | ||
</ol> | ||
<button type="button" disabled={!isValidUuid} onClick={handleClick}> | ||
Execute | ||
</button> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.