generated from scio-labs/inkathon
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update readme and add accurast impl.
- Loading branch information
Showing
7 changed files
with
213 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
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 |
---|---|---|
|
@@ -22,4 +22,5 @@ export const env = { | |
description: 'Lorem ipsum', | ||
}, | ||
], | ||
ACCU_DEV_WSS: process.env.NEXT_PUBLIC_ACCU_DEV_WSS!, | ||
} |
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,78 @@ | ||
import { useEffect, useMemo, useState } from 'react' | ||
|
||
import { AcurastClient } from '@acurast/dapp' | ||
|
||
import { env } from '@/config/environment' | ||
|
||
const DEV_WSS = env.ACCU_DEV_WSS | ||
|
||
export const useAccurast = () => { | ||
const [acurastClient, setAcurastClient] = useState<any>(null) | ||
const [id, setId] = useState<string>('') | ||
const [keyPair, setKeyPair] = useState() | ||
const [message, setMessage] = useState() | ||
|
||
useEffect(() => { | ||
const init = async () => { | ||
const acurast = new AcurastClient(DEV_WSS) | ||
setAcurastClient(acurast) | ||
|
||
const keyPair = await crypto.subtle.generateKey( | ||
{ | ||
name: 'ECDSA', | ||
namedCurve: 'P-256', | ||
}, | ||
true, | ||
['sign'], | ||
) | ||
|
||
const [privateKeyRaw, publicKeyRaw] = await Promise.all([ | ||
// @ts-expect-error can't find the argument that matches call | ||
crypto.subtle | ||
.exportKey('jwk', keyPair.privateKey) | ||
.then((jwk) => Buffer.from(jwk.d, 'base64')), | ||
crypto.subtle | ||
.exportKey('raw', keyPair.publicKey) | ||
.then((arrayBuffer) => Buffer.from(arrayBuffer)), | ||
]) | ||
|
||
const publicKeyCompressedSize = (publicKeyRaw.length - 1) / 2 | ||
const publicKeyCompressed = Buffer.concat([ | ||
new Uint8Array([publicKeyRaw[2 * publicKeyCompressedSize] % 2 ? 3 : 2]), | ||
publicKeyRaw.subarray(1, publicKeyCompressedSize + 1), | ||
]) | ||
const publicKeyHash = await crypto.subtle.digest('SHA-256', publicKeyCompressed) | ||
setId(Buffer.from(publicKeyHash.slice(0, 16)).toString('hex')) | ||
setKeyPair({ | ||
// @ts-expect-error allow | ||
privateKey: privateKeyRaw.toString('hex'), | ||
publicKey: publicKeyRaw.toString('hex'), | ||
}) | ||
|
||
return () => { | ||
acurast.close() | ||
} | ||
} | ||
|
||
init() | ||
}, []) | ||
|
||
const send = (recipient: any, payload: any) => { | ||
acurastClient.send(recipient, payload) | ||
} | ||
|
||
useEffect(() => { | ||
if (acurastClient) { | ||
acurastClient.onMessage((message: any) => { | ||
setMessage({ | ||
// @ts-expect-error allow | ||
sender: Buffer.from(message.sender).toString('hex'), | ||
recipient: Buffer.from(message.recipient).toString('hex'), | ||
payload: Buffer.from(message.payload).toString('hex'), | ||
}) | ||
}) | ||
} | ||
}, [acurastClient]) | ||
|
||
return { id, keyPair, message, send } | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.