Skip to content

Commit

Permalink
feat: submitting message
Browse files Browse the repository at this point in the history
  • Loading branch information
dalechyn committed Nov 3, 2024
1 parent b08e9d9 commit eb5949d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 105 deletions.
76 changes: 31 additions & 45 deletions playground/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import { Actions, Client, Transport } from 'fhub'
// import { CastAdd_toMessageDataProtobuf } from '../../src/Node/Internal/CastAdd/toMessageDataProtobuf'
// import { CastAdd_toMessageProtobuf } from '../../src/Node/Internal/CastAdd/toMessageProtobuf'
// import { Message_fromProtobuf } from '../../src/Node/Internal/Message/fromProtobuf'
import { CastAdd_toHex } from '../../src/Node/Internal/CastAdd/toHex'
// import { Actions as NodeActions } from 'fhub/Node'
// import {
// file_hub_event,
// HubEventType,
// } from '../../src/Node/Internal/Protobufs/hub_event_pb'

const RPC_URL = process.env.RPC_URL ?? 'https://hub-grpc.pinata.cloud'
const FID = (() => {
if (!process.env.FID) throw new Error('Specify your FID in env')
return BigInt(process.env.FID)
})()
const PRIVATE_KEY = (() => {
if (!process.env.PRIVATE_KEY)
throw new Error('Specify your PRIVATE_KEY in env')
return process.env.PRIVATE_KEY as `0x${string}`
})()

const client = Client.create(
Transport.grpcNode({
baseUrl: 'https://nemes.farcaster.xyz:2283',
baseUrl: RPC_URL,
httpVersion: '2',
}),
)
Expand Down Expand Up @@ -52,41 +55,24 @@ const client = Client.create(
// }
//

const cast = await Actions.Cast.getCast(client, {
fid: 11517n,
hash: '0x388ec5079a518ca133ec87aac23e1c4743bcc860',
})

console.dir(cast.meta, { depth: Number.POSITIVE_INFINITY })
console.dir(
CastAdd_toHex(cast),

{ depth: Number.POSITIVE_INFINITY },
)
// console.dir(
// Message_fromProtobuf(
// CastAdd_toMessageProtobuf({
// cast,
// privateKey:
// '0x0000000000000000000000000000000000000000000000000000000000000000',
// }),
// ),
// { depth: Infinity },
// )

// cast creation
// const message = await Actions.Cast.create(client, {
// cast: {
// text: {
// value: 'I sent this cast from my fhub package',
// },
// isLong: false,
// },
// account: {
// fid: 11517n,
// privateKey:
// '0x0000000000000000000000000000000000000000000000000000000000000000',
// },
// const cast = await Actions.Cast.getCast(client, {
// fid: FID,
// hash: '0x388ec5079a518ca133ec87aac23e1c4743bcc860',
// })

// console.log(message)
//cast creation
const message = await Actions.Cast.create(client, {
cast: {
text: {
value: 'I sent this cast from my fhub package',
},
isLong: false,
},
account: {
fid: FID,
privateKey: PRIVATE_KEY,
},
})

// biome-ignore lint/suspicious/noConsoleLog: <explanation>
console.log(message)
46 changes: 1 addition & 45 deletions src/Node/Internal/Actions/Cast/getCast.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,10 @@
import { toBinary } from '@bufbuild/protobuf'
import type { CallOptions } from '@connectrpc/connect'
import {
CastType,
FarcasterNetwork,
MessageData,
makeCastAddData,
makeMessageHash,
} from '@farcaster/core'
import { blake3 } from '@noble/hashes/blake3'
import { Hex } from 'ox'
import type { Client } from '../../../../Internal/Client/types.js'
import type { GlobalErrorType } from '../../../../Internal/Errors/error.js'
import type { Cast } from '../../Cast/types.js'
import { CastAdd_fromMessageProtobuf } from '../../CastAdd/fromMessageProtobuf.js'
import type { CastId } from '../../CastId/types.js'
import { FARCASTER_EPOCH_TIMESTAMP } from '../../Constants.js'
import * as Protobuf from '../../Protobufs/message_pb.js'

export declare namespace Actions_Cast_getCast {
type ParametersType = CastId
Expand All @@ -32,40 +21,7 @@ export async function Actions_Cast_getCast(
options,
)

// console.log('CAST MESSAGE HASH', message.hash)
// console.log(
// 'COMPUTED HASH',
// blake3(toBinary(Protobuf.MessageDataSchema, message.data!), { dkLen: 20 }),
// )
//
// console.log('CAST MESSAGE DATA BYTES', message.hash)

const res = CastAdd_fromMessageProtobuf(message)
// const fcMessageData = await makeCastAddData(
// {
// text: res.text.value,
// type: CastType.CAST,
// embeds: [],
// embedsDeprecated: [],
// mentions: [],
// mentionsPositions: [],
// },
// {
// fid: 11517,
// timestamp: res.timestamp - FARCASTER_EPOCH_TIMESTAMP,
// network: FarcasterNetwork.MAINNET,
// },
// ).then((res) => res._unsafeUnwrap())
// console.log(
// 'COMPUTED HASH V@@@@@@@@@',
// blake3(toBinary(Protobuf.MessageDataSchema, fcMessageData), { dkLen: 20 }),
// )
// console.log('FC MESSAGE DATA', fcMessageData)
// console.log(
// 'COMPUTED farcaster core HASH',
// await makeMessageHash(fcMessageData).then((res) => res._unsafeUnwrap()),
// )
return res
return CastAdd_fromMessageProtobuf(message)
}

Actions_Cast_getCast.parseError = (error: unknown) =>
Expand Down
14 changes: 0 additions & 14 deletions src/Node/Internal/CastAdd/toHex.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { toBinary } from '@bufbuild/protobuf'
import { MessageData } from '@farcaster/core'
import { blake3 } from '@noble/hashes/blake3'
import { Hex, type Types } from 'ox'
import type { GlobalErrorType } from '../../../Internal/Errors/error.js'
import type { Cast } from '../Cast/types.js'
Expand All @@ -10,18 +8,6 @@ import { CastAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js'
export function CastAdd_toHex(
cast: CastAdd_toHex.ParametersType,
): CastAdd_toHex.ReturnType {
// console.log(
// 'databytes2',
// toBinary(Protobuf.MessageDataSchema, CastAdd_toMessageDataProtobuf(cast)),
// )

console.log(
'DECODED,',
MessageData.decode(
toBinary(Protobuf.MessageDataSchema, CastAdd_toMessageDataProtobuf(cast)),
),
)

return Hex.fromBytes(
toBinary(Protobuf.MessageDataSchema, CastAdd_toMessageDataProtobuf(cast)),
)
Expand Down
2 changes: 1 addition & 1 deletion src/Node/Internal/Meta/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type { Meta } from './types.js'
export function Meta_create(
parameters: Meta_create.ParametersType,
): Meta_create.ReturnType {
const hash = blake3(parameters.dataBytes, { dkLen: 20 })
const hash = blake3(Hex.toBytes(parameters.dataBytes), { dkLen: 20 })
const signature = ed.sign(hash, parameters.privateKey.slice(2))
return {
hash: Hex.fromBytes(hash),
Expand Down

0 comments on commit eb5949d

Please sign in to comment.