zhereh-frontend

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 28195804cc71c4cc79e6bd0ed989848e3a3bb997
parent 8030ac52cf169c2f5f6700a77282131bf3340af1
Author: William Muli <willi.wambu@gmail.com>
Date:   Tue,  8 Aug 2023 11:18:06 +0300

use cbor-web for encoding and decoding cbor

Diffstat:
Msrc/utils/content-schema.ts | 53+++++++++++++++++++++--------------------------------
1 file changed, 21 insertions(+), 32 deletions(-)

diff --git a/src/utils/content-schema.ts b/src/utils/content-schema.ts @@ -1,10 +1,12 @@ import type { ContentSchema, SignedSchema } from '../shared/types' +import * as cbor from 'cbor-web' import { walletClient, publicClient } from '../client' -import { recoverMessageAddress, type Chain } from 'viem' +import { recoverMessageAddress, type Chain, hexToBytes } from 'viem' export function generateSchema( description: string, - descriptionFile: File | undefined + walaDescriptionFileHash: string | undefined, + descriptionFile: File | undefined ): ContentSchema { const currentDate = new Date().toUTCString() @@ -21,41 +23,28 @@ export function generateSchema( ] } - if (descriptionFile !== undefined) { - const reader = new FileReader() - reader.onloadend = () => { - const body = btoa(reader.result as string) - console.log(body) - schema.content.push({ - 'content-type': descriptionFile.type, - 'content-transfer-encoding': 'BASE64', - 'content-disposition': `attachment; filename="${descriptionFile.name}"`, - body - }) - } - reader.onerror = (error) => console.error('Error reading file: ', error) - reader.readAsBinaryString(descriptionFile) + if (walaDescriptionFileHash !== undefined && descriptionFile !== undefined) { + schema.content.push({ + 'content-type': descriptionFile.type, + 'content-transfer-encoding': 'BASE64', + 'content-disposition': `attachment; filename="${descriptionFile.name}"`, + 'content-digest-algorithm': 'sha256', + 'content-digest': btoa(walaDescriptionFileHash) + }) } return schema } -async function cborEncodeContent(content: ContentSchema | SignedSchema): Promise<string> { - const response = await fetch('/api/cbor/encode', { - method: 'POST', - body: JSON.stringify({ content }) - }) - - return (await response.json()).hex +function cborEncodeContent(content: ContentSchema | SignedSchema): string { + const encoded = cbor.encodeOne(content, { canonical: true }) + return encoded.toString('hex') } export async function decodeCborHex(hex: string): Promise<SignedSchema> { - const response = await fetch('/api/cbor/decode', { - method: 'POST', - body: JSON.stringify({ hex }) - }) - - return (await response.json()).content + const buffer = hexToBytes(`0x${hex}`).buffer + const decoded: SignedSchema = await cbor.decode(buffer) + return decoded } export async function signMessage(account: `0x${string}`, chain: Chain, message: string) { @@ -72,7 +61,7 @@ export async function createSignedSchema( account: `0x${string}` ): Promise<string> { // Step 1: CBORify the schema - const cborSchemaHex = await cborEncodeContent(schema) + const cborSchemaHex = cborEncodeContent(schema) // Step 2: Create a signature (Ethereum msg signature) on the CBOR itself const signature = await signMessage(account, chain, cborSchemaHex) @@ -84,7 +73,7 @@ export async function createSignedSchema( } // Step 4: CBORify the schema again - const signedSchemaHex = await cborEncodeContent(signedSchema) + const signedSchemaHex = cborEncodeContent(signedSchema) return signedSchemaHex } @@ -95,7 +84,7 @@ export async function verifyContentSchema( chain: Chain, originalHex: string ) { - const cborSchemaHex = await cborEncodeContent(contentSchema) + const cborSchemaHex = cborEncodeContent(contentSchema) console.log(originalHex, cborSchemaHex) const recoveredAddress = await recoverMessageAddress({