diff --git a/TODO.md b/TODO.md index 98fcf37..62983a9 100644 --- a/TODO.md +++ b/TODO.md @@ -1 +1,4 @@ message publishing +register signer +register fname +register fid | viem diff --git a/playground/src/index.ts b/playground/src/index.ts index 3b8c2c2..28f4537 100644 --- a/playground/src/index.ts +++ b/playground/src/index.ts @@ -1,4 +1,8 @@ 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, @@ -42,7 +46,47 @@ const client = Client.create( // console.log('recasts', castWithReactions.recasts.length) // events subscriptions -for await (const cast of Actions.Watch.watchCasts(client)) { - // biome-ignore lint/suspicious/noConsoleLog: - console.log(`${cast.fid} casted at ${cast.timestamp}: ${cast.text.value}`) -} +// for await (const cast of Actions.Watch.watchCasts(client)) { +// // biome-ignore lint/suspicious/noConsoleLog: +// console.log(`${cast.fid} casted at ${cast.timestamp}: ${cast.text.value}`) +// } +// + +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: +// '0x243d43a14dc0416c8b9b4cf253b05aa2531a4782e3c709d3456d017945af8dac', +// }, +// }) + +// console.log(message) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 2f59355..f14eb8a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -25,7 +25,7 @@ importers: version: 2.27.9 '@types/bun': specifier: latest - version: 1.1.11 + version: 1.1.13 '@types/node': specifier: ^22.7.4 version: 22.7.4 @@ -61,17 +61,20 @@ importers: src: dependencies: '@bufbuild/protobuf': - specifier: ^2.2.0 - version: 2.2.0 + specifier: ^2.2.2 + version: 2.2.2 '@connectrpc/connect': specifier: beta - version: 2.0.0-beta.2(@bufbuild/protobuf@2.2.0) + version: 2.0.0-beta.2(@bufbuild/protobuf@2.2.2) '@connectrpc/connect-node': specifier: beta - version: 2.0.0-beta.2(@bufbuild/protobuf@2.2.0)(@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.0)) + version: 2.0.0-beta.2(@bufbuild/protobuf@2.2.2)(@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.2)) '@connectrpc/connect-web': specifier: beta - version: 2.0.0-beta.2(@bufbuild/protobuf@2.2.0)(@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.0)) + version: 2.0.0-beta.2(@bufbuild/protobuf@2.2.2)(@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.2)) + '@farcaster/core': + specifier: ^0.15.6 + version: 0.15.6(typescript@5.6.2)(zod@3.23.8) '@noble/ed25519': specifier: ^2.1.0 version: 2.1.0 @@ -191,6 +194,9 @@ packages: '@bufbuild/protobuf@2.2.0': resolution: {integrity: sha512-+imAQkHf7U/Rwvu0wk1XWgsP3WnpCWmK7B48f0XqSNzgk64+grljTKC7pnO/xBiEMUziF7vKRfbBnOQhg126qQ==} + '@bufbuild/protobuf@2.2.2': + resolution: {integrity: sha512-UNtPCbrwrenpmrXuRwn9jYpPoweNXj8X5sMvYgsqYyaH8jQ6LfUJSk3dJLnBK+6sfYPrF4iAIo5sd5HQ+tg75A==} + '@bufbuild/protoc-gen-es@2.2.0': resolution: {integrity: sha512-PmUTtbJJfgcabTsoF59W0bsAr7xO5aGcMe69G8vOq0ogYV1aWmvFKhHKHDtn295pOLhTXmfrDSUNi/OTHuDdpw==} engines: {node: '>=14'} @@ -427,6 +433,13 @@ packages: cpu: [x64] os: [win32] + '@faker-js/faker@7.6.0': + resolution: {integrity: sha512-XK6BTq1NDMo9Xqw/YkYyGjSsg44fbNwYRx7QK2CuoQgyy+f1rrTDHoExVM5PsyXCtfl2vs2vVJ0MN0yN6LppRw==} + engines: {node: '>=14.0.0', npm: '>=6.0.0'} + + '@farcaster/core@0.15.6': + resolution: {integrity: sha512-Vxq2JhqdZYEMjH4PIwPXQkalY46S4Mol2oCSHUafXenDx6WSoQJqF/4an4KyxGQbmA7Cf8+hl6zuNzkkHXEUyQ==} + '@manypkg/find-root@1.1.0': resolution: {integrity: sha512-mki5uBvhHzO8kYYix/WRy2WX8S3B5wdVSc9D6KcU5lQNglP2yt58/VfLuAK49glRXChosY8ap2oJ1qgma3GUVA==} @@ -470,8 +483,8 @@ packages: engines: {node: '>=8.10'} hasBin: true - '@types/bun@1.1.11': - resolution: {integrity: sha512-0N7D/H/8sbf9JMkaG5F3+I/cB4TlhKTkO9EskEWP8XDr8aVcDe4EywSnU4cnyZy6tar1dq70NeFNkqMEUigthw==} + '@types/bun@1.1.13': + resolution: {integrity: sha512-KmQxSBgVWCl6RSuerlLGZlIWfdxkKqat0nxN61+qu4y1KDn0Ll3j7v1Pl8GnaL3a/U6GGWVTJh75ap62kR1E8Q==} '@types/node@12.20.55': resolution: {integrity: sha512-J8xLz7q2OFulZ2cyGTLE1TbbZcjpno7FaN6zdJNrgAdrJ+DZzh/uFR6YrTb4C+nXakvud8Q4+rbhoIWlYQbUFQ==} @@ -526,6 +539,9 @@ packages: balanced-match@1.0.2: resolution: {integrity: sha512-3oSeUO0TMV67hN1AmbXsK4yaqU7tjiHlbxRDZOpH0KW9+CeX4bRAaX0Anxt0tx2MrpRpWwQaPwIlISEJhYU5Pw==} + base-x@4.0.0: + resolution: {integrity: sha512-FuwxlW4H5kh37X/oW59pwTzzTKRzfrrQwhmyspRM7swOEZcHtDZSCt45U6oKgtuFE+WYPblePMVIPR4RZrh/hw==} + better-path-resolve@1.0.0: resolution: {integrity: sha512-pbnl5XzGBdrFU/wT4jqmJVPn2B6UHPBOhzMQkY/SPUPB6QtUXtmBHBIwCbXJol93mOpGMnQyP/+BB19q04xj7g==} engines: {node: '>=4'} @@ -537,11 +553,14 @@ packages: resolution: {integrity: sha512-yQbXgO/OSZVD2IsiLlro+7Hf6Q18EJrKSEsdoMzKePKXct3gvD8oLcOQdIzGupr5Fj+EDe8gO/lxc1BzfMpxvA==} engines: {node: '>=8'} + bs58@5.0.0: + resolution: {integrity: sha512-r+ihvQJvahgYT50JD05dyJNKlmmSlMoOGwn1lCcEzanPglg7TxYjioQUYehQ9mAR/+hOSd2jRc/Z2y5UxBymvQ==} + buf@0.1.1: resolution: {integrity: sha512-mhZY7GswAAd9ZJpBCsf2WaH2WMZwvxgsXD6rozflWgerE6gz2bCT/FvAfzcBXw55R18Jf8Fjzte7bw5xhwVhFg==} - bun-types@1.1.30: - resolution: {integrity: sha512-mGh7NLisOXskBU62DxLS+/nwmLlCYHYAkCzdo4DZ9+fzrpP41hAdOqaN4DO6tQfenHb4pYb0/shw29k4/6I2yQ==} + bun-types@1.1.34: + resolution: {integrity: sha512-br5QygTEL/TwB4uQOb96Ky22j4Gq2WxWH/8Oqv20fk5HagwKXo/akB+LiYgSfzexCt6kkcUaVm+bKiPl71xPvw==} chardet@0.7.0: resolution: {integrity: sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==} @@ -722,6 +741,11 @@ packages: isexe@2.0.0: resolution: {integrity: sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==} + isows@1.0.6: + resolution: {integrity: sha512-lPHCayd40oW98/I0uvgaHKWCSvkzY27LjWLbtzOm64yQ+G3Q5npjjbdppU65iZXkK1Zt+kH9pfegli0AYfwYYw==} + peerDependencies: + ws: '*' + jiti@2.3.3: resolution: {integrity: sha512-EX4oNDwcXSivPrw2qKH2LB5PoFxEvgtv2JgwW0bU858HoLQ+kutSvjLMUqBd0PeJYEinLWhoI9Ol0eYMqj/wNQ==} hasBin: true @@ -777,6 +801,9 @@ packages: ms@2.1.3: resolution: {integrity: sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==} + neverthrow@6.2.2: + resolution: {integrity: sha512-POR1FACqdK9jH0S2kRPzaZEvzT11wsOxLW520PQV/+vKi9dQe+hXq19EiOvYx7lSRaF5VB9lYGsPInynrnN05w==} + node-fetch@2.7.0: resolution: {integrity: sha512-c4FRfUm/dbcWZ7U+1Wq0AwCyFL+3nt2bEw05wfxSz+DWpWsitgmSgYmy2dQdWyKC1694ELPqMs/YzUSNozLt8A==} engines: {node: 4.x || >=6.0.0} @@ -1046,9 +1073,20 @@ packages: resolution: {integrity: sha512-rBJeI5CXAlmy1pV+617WB9J63U6XcazHHF2f2dbJix4XzpUF0RS3Zbj0FGIOCAva5P/d/GBOYaACQ1w+0azUkg==} engines: {node: '>= 4.0.0'} + viem@2.21.40: + resolution: {integrity: sha512-no/mE3l7B0mdUTtvO7z/cTLENttQ/M7+ombqFGXJqsQrxv9wrYsTIGpS3za+FA5a447hY+x9D8Wxny84q1zAaA==} + peerDependencies: + typescript: '>=5.0.4' + peerDependenciesMeta: + typescript: + optional: true + wcwidth@1.0.1: resolution: {integrity: sha512-XHPEwS0q6TaxcvG85+8EYkbiCux2XtWG2mkc47Ng2A77BQu9+DqIOJldST4HgPkuea7dvKSj5VgX3P1d4rW8Tg==} + webauthn-p256@0.0.10: + resolution: {integrity: sha512-EeYD+gmIT80YkSIDb2iWq0lq2zbHo1CxHlQTeJ+KkCILWpVy3zASH3ByD4bopzfk0uCwXxLqKGLqp2W4O28VFA==} + webidl-conversions@3.0.1: resolution: {integrity: sha512-2JAn3z8AR6rjK8Sm8orRC0h/bcl/DqL7tRPdGZ4I1CjdF+EaMLmYxBHyXuKL849eucPFhvBoxMsflfOb8kxaeQ==} @@ -1062,6 +1100,18 @@ packages: wrappy@1.0.2: resolution: {integrity: sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==} + ws@8.18.0: + resolution: {integrity: sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==} + engines: {node: '>=10.0.0'} + peerDependencies: + bufferutil: ^4.0.1 + utf-8-validate: '>=5.0.2' + peerDependenciesMeta: + bufferutil: + optional: true + utf-8-validate: + optional: true + yallist@2.1.2: resolution: {integrity: sha512-ncTzHV7NvsQZkYe1DW7cbDLm0YpzHmZF5r/iyP3ZnQtMiJ+pjzisCiMNI+Sj+xQF5pXhSHxSB3uDbsBTzY/c2A==} @@ -1146,6 +1196,8 @@ snapshots: '@bufbuild/protobuf@2.2.0': {} + '@bufbuild/protobuf@2.2.2': {} + '@bufbuild/protoc-gen-es@2.2.0(@bufbuild/protobuf@2.2.0)': dependencies: '@bufbuild/protoplugin': 2.2.0 @@ -1319,19 +1371,19 @@ snapshots: human-id: 1.0.2 prettier: 2.8.8 - '@connectrpc/connect-node@2.0.0-beta.2(@bufbuild/protobuf@2.2.0)(@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.0))': + '@connectrpc/connect-node@2.0.0-beta.2(@bufbuild/protobuf@2.2.2)(@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.2))': dependencies: - '@bufbuild/protobuf': 2.2.0 - '@connectrpc/connect': 2.0.0-beta.2(@bufbuild/protobuf@2.2.0) + '@bufbuild/protobuf': 2.2.2 + '@connectrpc/connect': 2.0.0-beta.2(@bufbuild/protobuf@2.2.2) - '@connectrpc/connect-web@2.0.0-beta.2(@bufbuild/protobuf@2.2.0)(@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.0))': + '@connectrpc/connect-web@2.0.0-beta.2(@bufbuild/protobuf@2.2.2)(@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.2))': dependencies: - '@bufbuild/protobuf': 2.2.0 - '@connectrpc/connect': 2.0.0-beta.2(@bufbuild/protobuf@2.2.0) + '@bufbuild/protobuf': 2.2.2 + '@connectrpc/connect': 2.0.0-beta.2(@bufbuild/protobuf@2.2.2) - '@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.0)': + '@connectrpc/connect@2.0.0-beta.2(@bufbuild/protobuf@2.2.2)': dependencies: - '@bufbuild/protobuf': 2.2.0 + '@bufbuild/protobuf': 2.2.2 '@esbuild/aix-ppc64@0.23.1': optional: true @@ -1405,6 +1457,22 @@ snapshots: '@esbuild/win32-x64@0.23.1': optional: true + '@faker-js/faker@7.6.0': {} + + '@farcaster/core@0.15.6(typescript@5.6.2)(zod@3.23.8)': + dependencies: + '@faker-js/faker': 7.6.0 + '@noble/curves': 1.6.0 + '@noble/hashes': 1.5.0 + bs58: 5.0.0 + neverthrow: 6.2.2 + viem: 2.21.40(typescript@5.6.2)(zod@3.23.8) + transitivePeerDependencies: + - bufferutil + - typescript + - utf-8-validate + - zod + '@manypkg/find-root@1.1.0': dependencies: '@babel/runtime': 7.25.7 @@ -1460,9 +1528,9 @@ snapshots: ignore: 5.3.2 p-map: 4.0.0 - '@types/bun@1.1.11': + '@types/bun@1.1.13': dependencies: - bun-types: 1.1.30 + bun-types: 1.1.34 '@types/node@12.20.55': {} @@ -1509,6 +1577,8 @@ snapshots: balanced-match@1.0.2: {} + base-x@4.0.0: {} + better-path-resolve@1.0.0: dependencies: is-windows: 1.0.2 @@ -1521,9 +1591,13 @@ snapshots: dependencies: fill-range: 7.1.1 + bs58@5.0.0: + dependencies: + base-x: 4.0.0 + buf@0.1.1: {} - bun-types@1.1.30: + bun-types@1.1.34: dependencies: '@types/node': 20.12.14 '@types/ws': 8.5.12 @@ -1721,6 +1795,10 @@ snapshots: isexe@2.0.0: {} + isows@1.0.6(ws@8.18.0): + dependencies: + ws: 8.18.0 + jiti@2.3.3: {} js-yaml@3.14.1: @@ -1785,6 +1863,8 @@ snapshots: ms@2.1.3: {} + neverthrow@6.2.2: {} + node-fetch@2.7.0: dependencies: whatwg-url: 5.0.0 @@ -1992,11 +2072,34 @@ snapshots: universalify@0.1.2: {} + viem@2.21.40(typescript@5.6.2)(zod@3.23.8): + dependencies: + '@adraffy/ens-normalize': 1.11.0 + '@noble/curves': 1.6.0 + '@noble/hashes': 1.5.0 + '@scure/bip32': 1.5.0 + '@scure/bip39': 1.4.0 + abitype: 1.0.6(typescript@5.6.2)(zod@3.23.8) + isows: 1.0.6(ws@8.18.0) + webauthn-p256: 0.0.10 + ws: 8.18.0 + optionalDependencies: + typescript: 5.6.2 + transitivePeerDependencies: + - bufferutil + - utf-8-validate + - zod + wcwidth@1.0.1: dependencies: defaults: 1.0.4 optional: true + webauthn-p256@0.0.10: + dependencies: + '@noble/curves': 1.6.0 + '@noble/hashes': 1.5.0 + webidl-conversions@3.0.1: {} whatwg-url@5.0.0: @@ -2010,6 +2113,8 @@ snapshots: wrappy@1.0.2: {} + ws@8.18.0: {} + yallist@2.1.2: {} zod-validation-error@3.4.0(zod@3.23.8): diff --git a/src/Actions/Cast.ts b/src/Actions/Cast.ts index fb085e0..49fb66c 100644 --- a/src/Actions/Cast.ts +++ b/src/Actions/Cast.ts @@ -1 +1,2 @@ export { Actions_Cast_getCast as getCast } from '../Internal/Actions/Cast/getCast.js' +export { Actions_Cast_create as create } from '../Internal/Actions/Cast/create.js' diff --git a/src/Internal/Account/types.ts b/src/Internal/Account/types.ts new file mode 100644 index 0000000..cc59134 --- /dev/null +++ b/src/Internal/Account/types.ts @@ -0,0 +1,6 @@ +import type { Types } from 'ox' + +export type Account = { + privateKey: Types.Hex + fid: bigint +} diff --git a/src/Internal/Actions/Cast/create.ts b/src/Internal/Actions/Cast/create.ts new file mode 100644 index 0000000..c8059c1 --- /dev/null +++ b/src/Internal/Actions/Cast/create.ts @@ -0,0 +1,36 @@ +import type { CallOptions } from '@connectrpc/connect' +import { CastAdd_toMessageProtobuf } from '../../../Node/Internal/CastAdd/toMessageProtobuf.js' +import { + Actions as NodeActions, + type Types as NodeTypes, +} from '../../../Node/index.js' +import type { Account } from '../../Account/types.js' +import type { Client } from '../../Client/types.js' +import type { GlobalErrorType } from '../../Errors/error.js' + +export declare namespace Actions_Cast_create { + type ParametersType = { + cast: Omit + account: Account + } + type ReturnType = NodeTypes.Message + type ErrorType = GlobalErrorType +} +export async function Actions_Cast_create( + client: Client, + parameters: Actions_Cast_create.ParametersType, + options?: CallOptions, +): Promise { + const message = CastAdd_toMessageProtobuf({ + cast: { + ...parameters.cast, + fid: parameters.account.fid, + timestamp: Math.floor(Date.now() / 1000), + }, + privateKey: parameters.account.privateKey, + }) + return NodeActions.Submit.submitMessage(client, message, options) +} + +Actions_Cast_create.parseError = (error: unknown) => + error as Actions_Cast_create.ErrorType diff --git a/src/Internal/Actions/Watch/watchCasts.ts b/src/Internal/Actions/Watch/watchCasts.ts index a1b2b66..2f01bd0 100644 --- a/src/Internal/Actions/Watch/watchCasts.ts +++ b/src/Internal/Actions/Watch/watchCasts.ts @@ -23,7 +23,7 @@ export async function* Actions_Watch_watchCasts( )) { if (event.message === undefined || event.message.type !== 'castAdd') continue - yield event.message.cast + yield event.message.data } } diff --git a/src/Node/Constants.ts b/src/Node/Constants.ts new file mode 100644 index 0000000..f734a21 --- /dev/null +++ b/src/Node/Constants.ts @@ -0,0 +1 @@ +export * from './Internal/Constants.js' diff --git a/src/Node/Internal/Actions/Cast/getAllCastMessagesByFid.ts b/src/Node/Internal/Actions/Cast/getAllCastMessagesByFid.ts index f45c816..b03de87 100644 --- a/src/Node/Internal/Actions/Cast/getAllCastMessagesByFid.ts +++ b/src/Node/Internal/Actions/Cast/getAllCastMessagesByFid.ts @@ -1,9 +1,10 @@ import type { CallOptions } from '@connectrpc/connect' -import { Hex, type Types } from 'ox' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Cast_fromMessage } from '../../Cast/fromMessage.js' import type { Cast } from '../../Cast/types.js' +import { CastAdd_fromMessageProtobuf } from '../../CastAdd/fromMessageProtobuf.js' +import { CastRemove_fromMessageProtobuf } from '../../CastRemove/fromMessageProtobuf.js' +import type { CastRemove } from '../../CastRemove/types.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' @@ -16,11 +17,11 @@ export declare namespace Actions_Cast_getAllCastMessagesByFid { type ReturnType = { messages: ( | { type: 'casted'; cast: Cast } - | { type: 'removed'; hash: Types.Hex } + | { type: 'removed'; cast: CastRemove } )[] nextPageToken: NextPageToken } - type ErrorType = Cast_fromMessage.ErrorType | GlobalErrorType + type ErrorType = CastAdd_fromMessageProtobuf.ErrorType | GlobalErrorType } export async function Actions_Cast_getAllCastMessagesByFid( client: Client, @@ -43,9 +44,9 @@ export async function Actions_Cast_getAllCastMessagesByFid( ) return { type: 'removed' as const, - hash: Hex.fromBytes(message.data.body.value.targetHash), + cast: CastRemove_fromMessageProtobuf(message), } - return { type: 'casted', cast: Cast_fromMessage(message) } + return { type: 'casted', cast: CastAdd_fromMessageProtobuf(message) } }), nextPageToken: Pagination_getPageToken(message.nextPageToken), } diff --git a/src/Node/Internal/Actions/Cast/getCast.ts b/src/Node/Internal/Actions/Cast/getCast.ts index cd5e1d6..8cafacd 100644 --- a/src/Node/Internal/Actions/Cast/getCast.ts +++ b/src/Node/Internal/Actions/Cast/getCast.ts @@ -1,15 +1,26 @@ +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 { Cast_fromMessage } from '../../Cast/fromMessage.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 type ReturnType = Cast - type ErrorType = Cast_fromMessage.ErrorType | GlobalErrorType + type ErrorType = CastAdd_fromMessageProtobuf.ErrorType | GlobalErrorType } export async function Actions_Cast_getCast( client: Client, @@ -21,7 +32,40 @@ export async function Actions_Cast_getCast( options, ) - return Cast_fromMessage(message) + // 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 } Actions_Cast_getCast.parseError = (error: unknown) => diff --git a/src/Node/Internal/Actions/Cast/getCastsByFid.ts b/src/Node/Internal/Actions/Cast/getCastsByFid.ts index 6a44c26..c1f17bc 100644 --- a/src/Node/Internal/Actions/Cast/getCastsByFid.ts +++ b/src/Node/Internal/Actions/Cast/getCastsByFid.ts @@ -1,8 +1,8 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Cast_fromMessage } from '../../Cast/fromMessage.js' import type { Cast } from '../../Cast/types.js' +import { CastAdd_fromMessageProtobuf } from '../../CastAdd/fromMessageProtobuf.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, @@ -18,7 +18,7 @@ export declare namespace Actions_Cast_getCastsByFid { casts: Cast[] nextPageToken: NextPageToken } - type ErrorType = Cast_fromMessage.ErrorType | GlobalErrorType + type ErrorType = CastAdd_fromMessageProtobuf.ErrorType | GlobalErrorType } export async function Actions_Cast_getCastsByFid( client: Client, @@ -33,7 +33,9 @@ export async function Actions_Cast_getCastsByFid( options, ) return { - casts: message.messages.map((message) => Cast_fromMessage(message)), + casts: message.messages.map((message) => + CastAdd_fromMessageProtobuf(message), + ), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Cast/getCastsByMention.ts b/src/Node/Internal/Actions/Cast/getCastsByMention.ts index bd46747..d6ac6e7 100644 --- a/src/Node/Internal/Actions/Cast/getCastsByMention.ts +++ b/src/Node/Internal/Actions/Cast/getCastsByMention.ts @@ -1,8 +1,8 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Cast_fromMessage } from '../../Cast/fromMessage.js' import type { Cast } from '../../Cast/types.js' +import { CastAdd_fromMessageProtobuf } from '../../CastAdd/fromMessageProtobuf.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' @@ -15,7 +15,7 @@ export declare namespace Actions_Cast_getCastsByMention { casts: Cast[] nextPageToken: NextPageToken } - type ErrorType = Cast_fromMessage.ErrorType | GlobalErrorType + type ErrorType = CastAdd_fromMessageProtobuf.ErrorType | GlobalErrorType } export async function Actions_Cast_getCastsByMention( client: Client, @@ -31,7 +31,9 @@ export async function Actions_Cast_getCastsByMention( ) return { - casts: message.messages.map((message) => Cast_fromMessage(message)), + casts: message.messages.map((message) => + CastAdd_fromMessageProtobuf(message), + ), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Cast/getCastsByParent.ts b/src/Node/Internal/Actions/Cast/getCastsByParent.ts index b244ce5..04cf8e5 100644 --- a/src/Node/Internal/Actions/Cast/getCastsByParent.ts +++ b/src/Node/Internal/Actions/Cast/getCastsByParent.ts @@ -1,12 +1,12 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Cast_fromMessage } from '../../Cast/fromMessage.js' import type { Cast } from '../../Cast/types.js' +import { CastAdd_fromMessageProtobuf } from '../../CastAdd/fromMessageProtobuf.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' -import { Parent_toMessage } from '../../Parent/toMessage.js' +import { Parent_toProtobuf } from '../../Parent/toProtobuf.js' import type { Parent } from '../../Parent/types.js' export declare namespace Actions_Cast_getCastsByParent { @@ -18,8 +18,8 @@ export declare namespace Actions_Cast_getCastsByParent { nextPageToken: NextPageToken } type ErrorType = - | Cast_fromMessage.ErrorType - | Parent_toMessage.ErrorType + | CastAdd_fromMessageProtobuf.ErrorType + | Parent_toProtobuf.ErrorType | GlobalErrorType } export async function Actions_Cast_getCastsByParent( @@ -29,13 +29,15 @@ export async function Actions_Cast_getCastsByParent( ): Promise { const message = await client.connectRpcClient.getCastsByParent( { - parent: Parent_toMessage(parameters.parent), + parent: Parent_toProtobuf(parameters.parent), ...Pagination_unwrap(parameters), }, options, ) return { - casts: message.messages.map((message) => Cast_fromMessage(message)), + casts: message.messages.map((message) => + CastAdd_fromMessageProtobuf(message), + ), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Event/subscribe.ts b/src/Node/Internal/Actions/Event/subscribe.ts index 4208517..912565d 100644 --- a/src/Node/Internal/Actions/Event/subscribe.ts +++ b/src/Node/Internal/Actions/Event/subscribe.ts @@ -1,7 +1,7 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { HubEvent_fromMessage } from '../../HubEvent/fromMessage.js' +import { HubEvent_fromProtobuf } from '../../HubEvent/fromProtobuf.js' import type { HubEvent } from '../../HubEvent/types.js' export declare namespace Actions_Event_subscribe { @@ -40,7 +40,7 @@ export async function* Actions_Event_subscribe( }, options, )) { - yield HubEvent_fromMessage(message) + yield HubEvent_fromProtobuf(message) } } diff --git a/src/Node/Internal/Actions/Link/getAllLinkMessagesByFid.ts b/src/Node/Internal/Actions/Link/getAllLinkMessagesByFid.ts index 3564b60..048d6da 100644 --- a/src/Node/Internal/Actions/Link/getAllLinkMessagesByFid.ts +++ b/src/Node/Internal/Actions/Link/getAllLinkMessagesByFid.ts @@ -1,7 +1,7 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Link_fromMessage } from '../../Link/fromMessage.js' +import { Link_fromMessageProtobuf } from '../../Link/fromMessageProtobuf.js' import type { Link } from '../../Link/types.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { @@ -26,7 +26,7 @@ export async function Actions_Link_getAllLinkMessagesByFid( options, ) return { - links: message.messages.map(Link_fromMessage), + links: message.messages.map(Link_fromMessageProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Link/getLink.ts b/src/Node/Internal/Actions/Link/getLink.ts index d22d966..4933048 100644 --- a/src/Node/Internal/Actions/Link/getLink.ts +++ b/src/Node/Internal/Actions/Link/getLink.ts @@ -1,7 +1,7 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Link_fromMessage } from '../../Link/fromMessage.js' +import { Link_fromMessageProtobuf } from '../../Link/fromMessageProtobuf.js' import type { Link } from '../../Link/types.js' export declare namespace Actions_Link_getLink { @@ -11,7 +11,7 @@ export declare namespace Actions_Link_getLink { type: string } type ReturnType = Link - type ErrorType = Link_fromMessage.ErrorType | GlobalErrorType + type ErrorType = Link_fromMessageProtobuf.ErrorType | GlobalErrorType } export async function Actions_Link_getLink( client: Client, @@ -29,7 +29,7 @@ export async function Actions_Link_getLink( }, options, ) - return Link_fromMessage(message) + return Link_fromMessageProtobuf(message) } Actions_Link_getLink.parseError = (error: unknown) => diff --git a/src/Node/Internal/Actions/Link/getLinkCompactStateMessageByFid.ts b/src/Node/Internal/Actions/Link/getLinkCompactStateMessageByFid.ts index 411f415..2988132 100644 --- a/src/Node/Internal/Actions/Link/getLinkCompactStateMessageByFid.ts +++ b/src/Node/Internal/Actions/Link/getLinkCompactStateMessageByFid.ts @@ -1,16 +1,17 @@ -import { type MessageJsonType, fromJson, toJson } from '@bufbuild/protobuf' import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { - type FidRequestJson, - FidRequestSchema, - MessagesResponseSchema, -} from '../../Protobufs/request_response_pb.js' +import { LinkCompactState_fromMessageProtobuf } from '../../LinkCompactState/fromMessageProtobuf.js' +import type { LinkCompactState } from '../../LinkCompactState/types.js' +import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' +import type { NextPageToken, Pagination } from '../../Pagination/types.js' +import { Pagination_unwrap } from '../../Pagination/unwrap.js' export declare namespace Actions_Link_getLinkCompactStateMessageByFid { - type ParametersType = Required - type ReturnType = MessageJsonType + type ParametersType = { + fid: bigint + } & Pagination + type ReturnType = { links: LinkCompactState[]; nextPageToken: NextPageToken } // @TODO: proper error handling type ErrorType = GlobalErrorType } @@ -20,10 +21,13 @@ export async function Actions_Link_getLinkCompactStateMessageByFid( options?: CallOptions, ): Promise { const message = await client.connectRpcClient.getLinkCompactStateMessageByFid( - fromJson(FidRequestSchema, parameters), + { fid: parameters.fid, ...Pagination_unwrap(parameters) }, options, ) - return toJson(MessagesResponseSchema, message) + return { + links: message.messages.map(LinkCompactState_fromMessageProtobuf), + nextPageToken: Pagination_getPageToken(message.nextPageToken), + } } Actions_Link_getLinkCompactStateMessageByFid.parseError = (error: unknown) => diff --git a/src/Node/Internal/Actions/Link/getLinksByFid.ts b/src/Node/Internal/Actions/Link/getLinksByFid.ts index b73814d..ff75489 100644 --- a/src/Node/Internal/Actions/Link/getLinksByFid.ts +++ b/src/Node/Internal/Actions/Link/getLinksByFid.ts @@ -1,7 +1,7 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Link_fromMessage } from '../../Link/fromMessage.js' +import { Link_fromMessageProtobuf } from '../../Link/fromMessageProtobuf.js' import type { Link } from '../../Link/types.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' @@ -30,7 +30,7 @@ export async function Actions_Link_getLinksByFid( options, ) return { - links: message.messages.map(Link_fromMessage), + links: message.messages.map(Link_fromMessageProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Link/getLinksByTarget.ts b/src/Node/Internal/Actions/Link/getLinksByTarget.ts index 51db3b9..01805e2 100644 --- a/src/Node/Internal/Actions/Link/getLinksByTarget.ts +++ b/src/Node/Internal/Actions/Link/getLinksByTarget.ts @@ -1,7 +1,7 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Link_fromMessage } from '../../Link/fromMessage.js' +import { Link_fromMessageProtobuf } from '../../Link/fromMessageProtobuf.js' import type { Link } from '../../Link/types.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' @@ -30,7 +30,7 @@ export async function Actions_Link_getLinksByTarget( options, ) return { - links: message.messages.map(Link_fromMessage), + links: message.messages.map(Link_fromMessageProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Reaction/getAllReactionMessagesByFid.ts b/src/Node/Internal/Actions/Reaction/getAllReactionMessagesByFid.ts index 9a02479..19c104d 100644 --- a/src/Node/Internal/Actions/Reaction/getAllReactionMessagesByFid.ts +++ b/src/Node/Internal/Actions/Reaction/getAllReactionMessagesByFid.ts @@ -4,7 +4,7 @@ import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' -import { Reaction_fromMessage } from '../../Reaction/fromMessage.js' +import { Reaction_fromMessageProtobuf } from '../../Reaction/fromMessageProtobuf.js' import type { Reaction } from '../../Reaction/types.js' export declare namespace Actions_Reaction_getAllReactionMessagesByFid { @@ -32,7 +32,7 @@ export async function Actions_Reaction_getAllReactionMessagesByFid( ) return { - messages: message.messages.map(Reaction_fromMessage), + messages: message.messages.map(Reaction_fromMessageProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Reaction/getReaction.ts b/src/Node/Internal/Actions/Reaction/getReaction.ts index d332dce..7e8d7b9 100644 --- a/src/Node/Internal/Actions/Reaction/getReaction.ts +++ b/src/Node/Internal/Actions/Reaction/getReaction.ts @@ -1,16 +1,12 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { Reaction_fromMessage } from '../../Reaction/fromMessage.js' -import { - ReactionTarget_toMessage, - ReactionType_toMessage, -} from '../../Reaction/toMessage.js' -import type { - Reaction, - ReactionTarget, - ReactionType, -} from '../../Reaction/types.js' +import { Reaction_fromMessageProtobuf } from '../../Reaction/fromMessageProtobuf.js' +import type { Reaction } from '../../Reaction/types.js' +import { ReactionTarget_toProtobuf } from '../../ReactionTarget/toProtobuf.js' +import type { ReactionTarget } from '../../ReactionTarget/types.js' +import { ReactionType_toProtobuf } from '../../ReactionType/toProtobuf.js' +import type { ReactionType } from '../../ReactionType/types.js' export declare namespace Actions_Reaction_getReaction { type ParametersType = { @@ -30,12 +26,12 @@ export async function Actions_Reaction_getReaction( const message = await client.connectRpcClient.getReaction( { fid: parameters.fid, - reactionType: ReactionType_toMessage(parameters.reactionType), - target: ReactionTarget_toMessage(parameters.target), + reactionType: ReactionType_toProtobuf(parameters.reactionType), + target: ReactionTarget_toProtobuf(parameters.target), }, options, ) - return Reaction_fromMessage(message) + return Reaction_fromMessageProtobuf(message) } Actions_Reaction_getReaction.parseError = (error: unknown) => diff --git a/src/Node/Internal/Actions/Reaction/getReactionsByCast.ts b/src/Node/Internal/Actions/Reaction/getReactionsByCast.ts index 81f7d8a..3da787c 100644 --- a/src/Node/Internal/Actions/Reaction/getReactionsByCast.ts +++ b/src/Node/Internal/Actions/Reaction/getReactionsByCast.ts @@ -5,9 +5,9 @@ import type { CastId } from '../../CastId/types.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' -import { Reaction_fromMessage } from '../../Reaction/fromMessage.js' -import { ReactionTarget_toMessage } from '../../Reaction/toMessage.js' +import { Reaction_fromMessageProtobuf } from '../../Reaction/fromMessageProtobuf.js' import type { Reaction } from '../../Reaction/types.js' +import { ReactionTarget_toProtobuf } from '../../ReactionTarget/toProtobuf.js' export declare namespace Actions_Reaction_getReactionsByCast { type ParametersType = CastId & Pagination @@ -25,14 +25,14 @@ export async function Actions_Reaction_getReactionsByCast( ): Promise { const message = await client.connectRpcClient.getReactionsByCast( { - target: ReactionTarget_toMessage({ type: 'cast', ...parameters }), + target: ReactionTarget_toProtobuf({ type: 'cast', ...parameters }), ...Pagination_unwrap(parameters), }, options, ) return { - messages: message.messages.map(Reaction_fromMessage), + messages: message.messages.map(Reaction_fromMessageProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Reaction/getReactionsByFid.ts b/src/Node/Internal/Actions/Reaction/getReactionsByFid.ts index c518320..86e4cd0 100644 --- a/src/Node/Internal/Actions/Reaction/getReactionsByFid.ts +++ b/src/Node/Internal/Actions/Reaction/getReactionsByFid.ts @@ -4,8 +4,9 @@ import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' -import { Reaction_fromMessage } from '../../Reaction/fromMessage.js' -import type { Reaction, ReactionType } from '../../Reaction/types.js' +import { Reaction_fromMessageProtobuf } from '../../Reaction/fromMessageProtobuf.js' +import type { Reaction } from '../../Reaction/types.js' +import type { ReactionType } from '../../ReactionType/types.js' export declare namespace Actions_Reaction_getReactionsByFid { type ParametersType = { @@ -33,7 +34,7 @@ export async function Actions_Reaction_getReactionsByFid( ) return { - messages: message.messages.map(Reaction_fromMessage), + messages: message.messages.map(Reaction_fromMessageProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Reaction/getReactionsByTarget.ts b/src/Node/Internal/Actions/Reaction/getReactionsByTarget.ts index 7f53a38..fb3128a 100644 --- a/src/Node/Internal/Actions/Reaction/getReactionsByTarget.ts +++ b/src/Node/Internal/Actions/Reaction/getReactionsByTarget.ts @@ -4,9 +4,10 @@ import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' -import { Reaction_fromMessage } from '../../Reaction/fromMessage.js' -import { ReactionTarget_toMessage } from '../../Reaction/toMessage.js' -import type { Reaction, ReactionTarget } from '../../Reaction/types.js' +import { Reaction_fromMessageProtobuf } from '../../Reaction/fromMessageProtobuf.js' +import type { Reaction } from '../../Reaction/types.js' +import { ReactionTarget_toProtobuf } from '../../ReactionTarget/toProtobuf.js' +import type { ReactionTarget } from '../../ReactionTarget/types.js' export declare namespace Actions_Reaction_getReactionsByTarget { type ReturnType = { @@ -23,13 +24,13 @@ export async function Actions_Reaction_getReactionsByTarget( ): Promise { const message = await client.connectRpcClient.getReactionsByTarget( { - ...ReactionTarget_toMessage(parameters), + ...ReactionTarget_toProtobuf(parameters), ...Pagination_unwrap(parameters), }, options, ) return { - messages: message.messages.map(Reaction_fromMessage), + messages: message.messages.map(Reaction_fromMessageProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/Submit/submitMessage.ts b/src/Node/Internal/Actions/Submit/submitMessage.ts index 861835c..39455f6 100644 --- a/src/Node/Internal/Actions/Submit/submitMessage.ts +++ b/src/Node/Internal/Actions/Submit/submitMessage.ts @@ -1,12 +1,13 @@ -import { type MessageJsonType, fromJson, toJson } from '@bufbuild/protobuf' import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' -import { type MessageJson, MessageSchema } from '../../Protobufs/message_pb.js' +import { Message_fromProtobuf } from '../../Message/fromProtobuf.js' +import type { Message } from '../../Message/types.js' +import type * as MessageProtobuf from '../../Protobufs/message_pb.js' export declare namespace Actions_Submit_submitMessage { - type ParametersType = Required - type ReturnType = MessageJsonType + type ParametersType = MessageProtobuf.Message + type ReturnType = Message // @TODO: proper error handling type ErrorType = GlobalErrorType } @@ -16,10 +17,10 @@ export async function Actions_Submit_submitMessage( options?: CallOptions, ): Promise { const message = await client.connectRpcClient.submitMessage( - fromJson(MessageSchema, parameters), + parameters, options, ) - return toJson(MessageSchema, message) + return Message_fromProtobuf(message) } Actions_Submit_submitMessage.parseError = (error: unknown) => diff --git a/src/Node/Internal/Actions/UserData/getAllUserDataMessagesByFid.ts b/src/Node/Internal/Actions/UserData/getAllUserDataMessagesByFid.ts index 1d99ffe..0d83331 100644 --- a/src/Node/Internal/Actions/UserData/getAllUserDataMessagesByFid.ts +++ b/src/Node/Internal/Actions/UserData/getAllUserDataMessagesByFid.ts @@ -4,7 +4,7 @@ import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' import { Pagination_getPageToken } from '../../Pagination/getPageToken.js' import type { NextPageToken, Pagination } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' -import { UserData_fromMessage } from '../../UserData/fromMessage.js' +import { UserData_fromProtobuf } from '../../UserData/fromProtobuf.js' import type { UserData } from '../../UserData/types.js' export declare namespace Actions_UserData_getAllUserDataMessagesByFid { @@ -13,7 +13,7 @@ export declare namespace Actions_UserData_getAllUserDataMessagesByFid { } & Pagination type ReturnType = { datas: UserData[]; nextPageToken: NextPageToken } // @TODO: proper error handling - type ErrorType = UserData_fromMessage.ErrorType | GlobalErrorType + type ErrorType = UserData_fromProtobuf.ErrorType | GlobalErrorType } export async function Actions_UserData_getAllUserDataMessagesByFid( client: Client, @@ -28,7 +28,7 @@ export async function Actions_UserData_getAllUserDataMessagesByFid( options, ) return { - datas: message.messages.map(UserData_fromMessage), + datas: message.messages.map(UserData_fromProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Actions/UserData/getUserData.ts b/src/Node/Internal/Actions/UserData/getUserData.ts index 02e4ab5..5fa3b71 100644 --- a/src/Node/Internal/Actions/UserData/getUserData.ts +++ b/src/Node/Internal/Actions/UserData/getUserData.ts @@ -2,7 +2,7 @@ import type { CallOptions } from '@connectrpc/connect' import type { Client } from '../../../../Internal/Client/types.js' import type { GlobalErrorType } from '../../../../Internal/Errors/error.js' import type { UserDataType } from '../../Protobufs/message_pb.js' -import { UserData_fromMessage } from '../../UserData/fromMessage.js' +import { UserData_fromProtobuf } from '../../UserData/fromProtobuf.js' import type { UserData } from '../../UserData/types.js' export declare namespace Actions_UserData_getUserData { @@ -21,7 +21,7 @@ export async function Actions_UserData_getUserData( options?: CallOptions, ): Promise { const message = await client.connectRpcClient.getUserData(parameters, options) - return UserData_fromMessage(message) + return UserData_fromProtobuf(message) } Actions_UserData_getUserData.parseError = (error: unknown) => diff --git a/src/Node/Internal/Actions/UserData/getUserDataByFid.ts b/src/Node/Internal/Actions/UserData/getUserDataByFid.ts index 301b63b..9c09825 100644 --- a/src/Node/Internal/Actions/UserData/getUserDataByFid.ts +++ b/src/Node/Internal/Actions/UserData/getUserDataByFid.ts @@ -7,7 +7,7 @@ import type { PaginationWithTimestamps, } from '../../Pagination/types.js' import { Pagination_unwrap } from '../../Pagination/unwrap.js' -import { UserData_fromMessage } from '../../UserData/fromMessage.js' +import { UserData_fromProtobuf } from '../../UserData/fromProtobuf.js' import type { UserData } from '../../UserData/types.js' export declare namespace Actions_UserData_getUserDataByFid { @@ -15,7 +15,7 @@ export declare namespace Actions_UserData_getUserDataByFid { fid: bigint } & PaginationWithTimestamps type ReturnType = { datas: UserData[]; nextPageToken: NextPageToken } - type ErrorType = UserData_fromMessage.ErrorType | GlobalErrorType + type ErrorType = UserData_fromProtobuf.ErrorType | GlobalErrorType } export async function Actions_UserData_getUserDataByFid( client: Client, @@ -30,7 +30,7 @@ export async function Actions_UserData_getUserDataByFid( options, ) return { - datas: message.messages.map(UserData_fromMessage), + datas: message.messages.map(UserData_fromProtobuf), nextPageToken: Pagination_getPageToken(message.nextPageToken), } } diff --git a/src/Node/Internal/Cast/errors.ts b/src/Node/Internal/Cast/errors.ts index 4bd4b8d..b95bad6 100644 --- a/src/Node/Internal/Cast/errors.ts +++ b/src/Node/Internal/Cast/errors.ts @@ -1,16 +1,5 @@ import { BaseError } from 'ox/Errors' -// @TODO: replace by our own BaseError -export class Cast_InvalidMessageTypeError extends BaseError { - override readonly name = 'Cast.InvalidMessageTypeError' - - constructor({ hash }: { hash: string }) { - super(`Message "${hash}" has type other than CAST_ADD.`, { - docsPath: '/errors#invalidmessagetypeerror', - }) - } -} - export class Cast_InvalidEmbedsError extends BaseError { override readonly name = 'Cast.InvalidEmbedsError' diff --git a/src/Node/Internal/Cast/fromMessage.ts b/src/Node/Internal/Cast/fromMessage.ts deleted file mode 100644 index b2ec7c4..0000000 --- a/src/Node/Internal/Cast/fromMessage.ts +++ /dev/null @@ -1,73 +0,0 @@ -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { Embed_fromMessage } from '../Embed/fromMessage.js' -import type { Embed } from '../Embed/types.js' -import { Meta_fromMessage } from '../Meta/fromMessage.js' -import { Parent_fromMessage } from '../Parent/fromMessage.js' -import { CastType, type Message } from '../Protobufs/message_pb.js' -import { - Cast_InvalidEmbedsError, - Cast_InvalidMessageTypeError, -} from './errors.js' -import type { Cast } from './types.js' - -export function Cast_fromMessage( - message: Message, -): Cast_fromMessage.ReturnType { - const meta = Meta_fromMessage(message) - - // @TODO: separate error here - if (!message.data) throw new Error('`data` must be defined in Cast message.') - if (message.data.body.case !== 'castAddBody') - throw new Cast_InvalidMessageTypeError({ hash: meta.hash }) - - const isLong = message.data.body.value.type === CastType.LONG_CAST - const mentions = (() => { - if (message.data.body.value.mentions.length === 0) return undefined - const mentions = [] - for (let i = 0; i < message.data.body.value.mentions.length; i++) { - mentions.push({ - fid: message.data.body.value.mentions[i], - position: message.data.body.value.mentionsPositions[i], - }) - } - return mentions - })() - - const embeds = (() => { - if (message.data.body.value.embeds.length === 0) return undefined - const embedsOrUndefineds = message.data.body.value.embeds.map(({ embed }) => - Embed_fromMessage(embed), - ) - if (embedsOrUndefineds.indexOf(undefined) !== -1) - throw new Cast_InvalidEmbedsError({ hash: meta.hash }) - return embedsOrUndefineds as Embed[] - })() - const parent = Parent_fromMessage(message.data.body.value.parent) - - return { - meta, - isLong, - fid: message.data.fid, - timestamp: message.data.timestamp, - text: { - mentions, - embeds, - value: message.data.body.value.text, - }, - parent, - } -} - -export declare namespace Cast_fromMessage { - type ReturnType = Cast - - type ErrorType = - | Cast_InvalidMessageTypeError - | Meta_fromMessage.ErrorType - | Embed_fromMessage.ErrorType - | Parent_fromMessage.ErrorType - | GlobalErrorType -} - -Cast_fromMessage.parseError = (error: unknown) => - error as Cast_fromMessage.ErrorType diff --git a/src/Node/Internal/Cast/fromProtobuf.ts b/src/Node/Internal/Cast/fromProtobuf.ts new file mode 100644 index 0000000..da6ed73 --- /dev/null +++ b/src/Node/Internal/Cast/fromProtobuf.ts @@ -0,0 +1,67 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import { Embed_fromProtobuf } from '../Embed/fromProtobuf.js' +import type { Embed } from '../Embed/types.js' +import type { Meta } from '../Meta/types.js' +import { Parent_fromProtobuf } from '../Parent/fromProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { Cast_InvalidEmbedsError } from './errors.js' +import type { Cast } from './types.js' + +export function Cast_fromProtobuf( + parameters: Cast_fromProtobuf.ParametersType, +): Cast_fromProtobuf.ReturnType { + const isLong = parameters.body.type === MessageProtobuf.CastType.LONG_CAST + const mentions = (() => { + if (parameters.body.mentions.length === 0) return undefined + const mentions = [] + for (let i = 0; i < parameters.body.mentions.length; i++) { + mentions.push({ + fid: parameters.body.mentions[i], + position: parameters.body.mentionsPositions[i], + }) + } + return mentions + })() + + const embeds = (() => { + if (parameters.body.embeds.length === 0) return undefined + const embedsOrUndefineds = parameters.body.embeds.map(({ embed }) => + Embed_fromProtobuf(embed), + ) + if (embedsOrUndefineds.indexOf(undefined) !== -1) + throw new Cast_InvalidEmbedsError({ hash: parameters.meta.hash }) + return embedsOrUndefineds as Embed[] + })() + const parent = Parent_fromProtobuf(parameters.body.parent) + + return { + meta: parameters.meta, + isLong, + text: { + mentions, + embeds, + value: parameters.body.text, + }, + fid: parameters.fid, + timestamp: FARCASTER_EPOCH_TIMESTAMP + parameters.timestamp, + parent, + } +} + +export declare namespace Cast_fromProtobuf { + type ParametersType = { + body: MessageProtobuf.CastAddBody + meta: Meta + fid: bigint + timestamp: number + } + type ReturnType = Cast + type ErrorType = + | Embed_fromProtobuf.ErrorType + | Parent_fromProtobuf.ErrorType + | GlobalErrorType +} + +Cast_fromProtobuf.parseError = (error: unknown) => + error as Cast_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Cast/toProtobuf.ts b/src/Node/Internal/Cast/toProtobuf.ts new file mode 100644 index 0000000..54aadf6 --- /dev/null +++ b/src/Node/Internal/Cast/toProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Embed_toProtobuf } from '../Embed/toProtobuf.js' +import { Parent_toProtobuf } from '../Parent/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Cast } from './types.js' + +export function Cast_toProtobuf( + cast: Cast_toProtobuf.ParametersType, +): Cast_toProtobuf.ReturnType { + return create(MessageProtobuf.CastAddBodySchema, { + type: cast.isLong + ? MessageProtobuf.CastType.LONG_CAST + : MessageProtobuf.CastType.CAST, + embedsDeprecated: [], + embeds: cast.text.embeds ? cast.text.embeds.map(Embed_toProtobuf) : [], + mentions: cast.text.mentions + ? cast.text.mentions.map((mention) => mention.fid) + : [], + mentionsPositions: cast.text.mentions + ? cast.text.mentions.map((mention) => mention.position) + : [], + parent: cast.parent + ? Parent_toProtobuf(cast.parent) + : { case: undefined, value: undefined }, + text: cast.text.value, + }) +} + +export declare namespace Cast_toProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.CastAddBody + + type ErrorType = GlobalErrorType +} + +Cast_toProtobuf.parseError = (error: unknown) => + error as Cast_toProtobuf.ErrorType diff --git a/src/Node/Internal/Cast/types.ts b/src/Node/Internal/Cast/types.ts index 22b871b..a9cb2ab 100644 --- a/src/Node/Internal/Cast/types.ts +++ b/src/Node/Internal/Cast/types.ts @@ -6,11 +6,11 @@ export type Cast = { meta: Meta fid: bigint timestamp: number - parent: Parent | undefined + parent?: Parent | undefined text: { value: string - embeds: Embed[] | undefined - mentions: + embeds?: Embed[] | undefined + mentions?: | { fid: bigint position: number diff --git a/src/Node/Internal/CastAdd/errors.ts b/src/Node/Internal/CastAdd/errors.ts new file mode 100644 index 0000000..65874fd --- /dev/null +++ b/src/Node/Internal/CastAdd/errors.ts @@ -0,0 +1,12 @@ +import { BaseError } from 'ox/Errors' + +// @TODO: replace by our own BaseError +export class CastAdd_InvalidMessageTypeError extends BaseError { + override readonly name = 'CastAdd.InvalidMessageTypeError' + + constructor({ hash }: { hash: string }) { + super(`Message "${hash}" has type other than CAST_ADD.`, { + docsPath: '/errors#invalidmessagetypeerror', + }) + } +} diff --git a/src/Node/Internal/CastAdd/fromMessageProtobuf.ts b/src/Node/Internal/CastAdd/fromMessageProtobuf.ts new file mode 100644 index 0000000..6f5a79c --- /dev/null +++ b/src/Node/Internal/CastAdd/fromMessageProtobuf.ts @@ -0,0 +1,35 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Cast_fromProtobuf } from '../Cast/fromProtobuf.js' +import type { Cast } from '../Cast/types.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import { CastAdd_InvalidMessageTypeError } from './errors.js' + +export function CastAdd_fromMessageProtobuf( + message: CastAdd_fromMessageProtobuf.ParametersType, +): CastAdd_fromMessageProtobuf.ReturnType { + const meta = Meta_fromProtobuf(message) + // @TODO: separate error here + if (!message.data) + throw new Error('`data` must be defined in CastAdd message.') + if (message.data.body.case !== 'castAddBody') + throw new CastAdd_InvalidMessageTypeError({ hash: meta.hash }) + return Cast_fromProtobuf({ + body: message.data.body.value, + meta, + fid: message.data.fid, + timestamp: message.data.timestamp, + }) +} + +export declare namespace CastAdd_fromMessageProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = Cast + type ErrorType = + | CastAdd_InvalidMessageTypeError + | Meta_fromProtobuf.ErrorType + | GlobalErrorType +} + +CastAdd_fromMessageProtobuf.parseError = (error: unknown) => + error as CastAdd_fromMessageProtobuf.ErrorType diff --git a/src/Node/Internal/CastAdd/toHex.ts b/src/Node/Internal/CastAdd/toHex.ts new file mode 100644 index 0000000..8bed172 --- /dev/null +++ b/src/Node/Internal/CastAdd/toHex.ts @@ -0,0 +1,36 @@ +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' +import * as Protobuf from '../Protobufs/message_pb.js' +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)), + ) +} + +export declare namespace CastAdd_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +CastAdd_toHex.parseError = (error: unknown) => error as CastAdd_toHex.ErrorType diff --git a/src/Node/Internal/CastAdd/toMessageDataProtobuf.ts b/src/Node/Internal/CastAdd/toMessageDataProtobuf.ts new file mode 100644 index 0000000..775bddf --- /dev/null +++ b/src/Node/Internal/CastAdd/toMessageDataProtobuf.ts @@ -0,0 +1,78 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Cast_toProtobuf } from '../Cast/toProtobuf.js' +import type { Cast } from '../Cast/types.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' + +// RECEIVED +// body: { +// case: 'castAddBody', +// value: { +// '$typeName': 'CastAddBody', +// embedsDeprecated: [], +// mentions: [], +// parent: { case: undefined }, +// text: 'my TODO list after buying my own place 🏡:\n' + +// '- basic mattress (~100$)\n' + +// '- fridge (~200$)\n' + +// '- data rack with 10TB SSD, NVIDIA H200, 10Gb/s optics, 512GB ram, AMD EPYC 7502P (~100,000$)\n' + +// '\n' + +// 'did I miss anything?', +// mentionsPositions: [], +// embeds: [], +// type: 0 +// } +// } + +// CONSTRUICTED +// { +// '$typeName': 'MessageData', +// type: 1, +// fid: 11517n, +// timestamp: 121142222, +// network: 1, +// body: { +// case: 'castAddBody', +// value: { +// '$typeName': 'CastAddBody', +// embedsDeprecated: [], +// mentions: [], +// parent: { case: undefined }, +// text: 'my TODO list after buying my own place 🏡:\n' + +// '- basic mattress (~100$)\n' + +// '- fridge (~200$)\n' + +// '- data rack with 10TB SSD, NVIDIA H200, 10Gb/s optics, 512GB ram, AMD EPYC 7502P (~100,000$)\n' + +// '\n' + +// 'did I miss anything?', +// mentionsPositions: [], +// embeds: [], +// type: 0 +// } +// } +// } + +export function CastAdd_toMessageDataProtobuf( + cast: CastAdd_toMessageDataProtobuf.ParametersType, +): CastAdd_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: cast.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.CAST_ADD, + fid: cast.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'castAddBody', + value: Cast_toProtobuf(cast), + }, + }) +} + +export declare namespace CastAdd_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +CastAdd_toMessageDataProtobuf.parseError = (error: unknown) => + error as CastAdd_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/CastAdd/toMessageProtobuf.ts b/src/Node/Internal/CastAdd/toMessageProtobuf.ts new file mode 100644 index 0000000..fcf3d11 --- /dev/null +++ b/src/Node/Internal/CastAdd/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { Cast } from '../Cast/types.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { CastAdd_toHex } from './toHex.js' +import { CastAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function CastAdd_toMessageProtobuf( + parameters: CastAdd_toMessageProtobuf.ParametersType, +): CastAdd_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: CastAdd_toHex(parameters.cast), + privateKey: parameters.privateKey, + }), + ), + data: CastAdd_toMessageDataProtobuf(parameters.cast), + }) +} + +export declare namespace CastAdd_toMessageProtobuf { + type ParametersType = { + cast: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +CastAdd_toMessageProtobuf.parseError = (error: unknown) => + error as CastAdd_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/CastId/fromMessage.ts b/src/Node/Internal/CastId/fromMessage.ts deleted file mode 100644 index aa77ba7..0000000 --- a/src/Node/Internal/CastId/fromMessage.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { Hex } from 'ox' -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import type { CastId as Protobufs_CastId } from '../Protobufs/message_pb.js' -import type { CastId } from './types.js' - -export function CastId_fromMessage( - castId: Protobufs_CastId, -): CastId_fromMessage.ReturnType { - return { - fid: castId.fid, - hash: Hex.fromBytes(castId.hash), - } as const -} - -export declare namespace CastId_fromMessage { - type ReturnType = CastId - - type ErrorType = GlobalErrorType -} - -CastId_fromMessage.parseError = (error: unknown) => - error as CastId_fromMessage.ErrorType diff --git a/src/Node/Internal/CastId/fromProtobuf.ts b/src/Node/Internal/CastId/fromProtobuf.ts new file mode 100644 index 0000000..cd18a94 --- /dev/null +++ b/src/Node/Internal/CastId/fromProtobuf.ts @@ -0,0 +1,22 @@ +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { CastId } from './types.js' + +export function CastId_fromProtobuf( + castId: CastId_fromProtobuf.ParametersType, +): CastId_fromProtobuf.ReturnType { + return { + fid: castId.fid, + hash: Hex.fromBytes(castId.hash), + } as const +} + +export declare namespace CastId_fromProtobuf { + type ParametersType = MessageProtobuf.CastId + type ReturnType = CastId + type ErrorType = GlobalErrorType +} + +CastId_fromProtobuf.parseError = (error: unknown) => + error as CastId_fromProtobuf.ErrorType diff --git a/src/Node/Internal/CastId/toMessage.ts b/src/Node/Internal/CastId/toMessage.ts deleted file mode 100644 index 6e1d19b..0000000 --- a/src/Node/Internal/CastId/toMessage.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { Hex } from 'ox' -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import type { CastId as Protobufs_CastId } from '../Protobufs/message_pb.js' -import type { CastId } from './types.js' - -export function CastId_toMessage(castId: CastId): CastId_toMessage.ReturnType { - return { - $typeName: 'CastId', - fid: castId.fid, - hash: Hex.toBytes(castId.hash), - } as const -} - -export declare namespace CastId_toMessage { - type ReturnType = Protobufs_CastId - - type ErrorType = GlobalErrorType -} - -CastId_toMessage.parseError = (error: unknown) => - error as CastId_toMessage.ErrorType diff --git a/src/Node/Internal/CastId/toProtobuf.ts b/src/Node/Internal/CastId/toProtobuf.ts new file mode 100644 index 0000000..9c7c441 --- /dev/null +++ b/src/Node/Internal/CastId/toProtobuf.ts @@ -0,0 +1,23 @@ +import { create } from '@bufbuild/protobuf' +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { CastId } from './types.js' + +export function CastId_toProtobuf( + castId: CastId_toProtobuf.ParametersType, +): CastId_toProtobuf.ReturnType { + return create(MessageProtobuf.CastIdSchema, { + fid: castId.fid, + hash: Hex.toBytes(castId.hash), + }) +} + +export declare namespace CastId_toProtobuf { + type ParametersType = CastId + type ReturnType = MessageProtobuf.CastId + type ErrorType = GlobalErrorType +} + +CastId_toProtobuf.parseError = (error: unknown) => + error as CastId_toProtobuf.ErrorType diff --git a/src/Node/Internal/CastRemove/fromMessageProtobuf.ts b/src/Node/Internal/CastRemove/fromMessageProtobuf.ts new file mode 100644 index 0000000..83333b0 --- /dev/null +++ b/src/Node/Internal/CastRemove/fromMessageProtobuf.ts @@ -0,0 +1,33 @@ +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { CastRemove } from './types.js' + +export function CastRemove_fromMessageProtobuf( + message: CastRemove_fromMessageProtobuf.ParametersType, +): CastRemove_fromMessageProtobuf.ReturnType { + const meta = Meta_fromProtobuf(message) + + // @TODO: separate error here + if (!message.data) throw new Error('`data` must be defined in Cast message.') + if (message.data.body.case !== 'castRemoveBody') + throw new Error('no cast remove body') + + return { + meta, + hash: Hex.fromBytes(message.data.body.value.targetHash), + fid: message.data.fid, + timestamp: FARCASTER_EPOCH_TIMESTAMP + message.data.timestamp, + } +} + +export declare namespace CastRemove_fromMessageProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = CastRemove + type ErrorType = Meta_fromProtobuf.ErrorType | GlobalErrorType +} + +CastRemove_fromMessageProtobuf.parseError = (error: unknown) => + error as CastRemove_fromMessageProtobuf.ErrorType diff --git a/src/Node/Internal/CastRemove/toHex.ts b/src/Node/Internal/CastRemove/toHex.ts new file mode 100644 index 0000000..0db0f7c --- /dev/null +++ b/src/Node/Internal/CastRemove/toHex.ts @@ -0,0 +1,26 @@ +import { toBinary } from '@bufbuild/protobuf' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as Protobuf from '../Protobufs/message_pb.js' +import { CastRemove_toMessageDataProtobuf } from './toMessageDataProtobuf.js' +import type { CastRemove } from './types.js' + +export function CastRemove_toHex( + cast: CastRemove_toHex.ParametersType, +): CastRemove_toHex.ReturnType { + return Hex.fromBytes( + toBinary( + Protobuf.MessageDataSchema, + CastRemove_toMessageDataProtobuf(cast), + ), + ) +} + +export declare namespace CastRemove_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +CastRemove_toHex.parseError = (error: unknown) => + error as CastRemove_toHex.ErrorType diff --git a/src/Node/Internal/CastRemove/toMessageDataProtobuf.ts b/src/Node/Internal/CastRemove/toMessageDataProtobuf.ts new file mode 100644 index 0000000..57d3a34 --- /dev/null +++ b/src/Node/Internal/CastRemove/toMessageDataProtobuf.ts @@ -0,0 +1,33 @@ +import { create } from '@bufbuild/protobuf' +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { CastRemove } from './types.js' + +export function CastRemove_toMessageDataProtobuf( + castRemoval: CastRemove_toMessageDataProtobuf.ParametersType, +): CastRemove_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: castRemoval.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.CAST_REMOVE, + fid: castRemoval.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'castRemoveBody', + value: create(MessageProtobuf.CastRemoveBodySchema, { + targetHash: Hex.toBytes(castRemoval.hash), + }), + }, + }) +} + +export declare namespace CastRemove_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +CastRemove_toMessageDataProtobuf.parseError = (error: unknown) => + error as CastRemove_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/CastRemove/toMessageProtobuf.ts b/src/Node/Internal/CastRemove/toMessageProtobuf.ts new file mode 100644 index 0000000..0b3ca5f --- /dev/null +++ b/src/Node/Internal/CastRemove/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { CastRemove_toHex } from './toHex.js' +import { CastRemove_toMessageDataProtobuf } from './toMessageDataProtobuf.js' +import type { CastRemove } from './types.js' + +export function CastRemove_toMessageProtobuf( + parameters: CastRemove_toMessageProtobuf.ParametersType, +): CastRemove_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: CastRemove_toHex(parameters.castRemove), + privateKey: parameters.privateKey, + }), + ), + data: CastRemove_toMessageDataProtobuf(parameters.castRemove), + }) +} + +export declare namespace CastRemove_toMessageProtobuf { + type ParametersType = { + castRemove: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +CastRemove_toMessageProtobuf.parseError = (error: unknown) => + error as CastRemove_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/CastRemove/types.ts b/src/Node/Internal/CastRemove/types.ts new file mode 100644 index 0000000..8aff0e1 --- /dev/null +++ b/src/Node/Internal/CastRemove/types.ts @@ -0,0 +1,9 @@ +import type { Types } from 'ox' +import type { Meta } from '../Meta/types.js' + +export type CastRemove = { + meta: Meta + hash: Types.Hex + fid: bigint + timestamp: number +} diff --git a/src/Node/Internal/Constants.ts b/src/Node/Internal/Constants.ts new file mode 100644 index 0000000..0f12e4e --- /dev/null +++ b/src/Node/Internal/Constants.ts @@ -0,0 +1 @@ +export const FARCASTER_EPOCH_TIMESTAMP = 1609459200 diff --git a/src/Node/Internal/Embed/fromMessage.ts b/src/Node/Internal/Embed/fromProtobuf.ts similarity index 51% rename from src/Node/Internal/Embed/fromMessage.ts rename to src/Node/Internal/Embed/fromProtobuf.ts index abd7e69..1ac24d1 100644 --- a/src/Node/Internal/Embed/fromMessage.ts +++ b/src/Node/Internal/Embed/fromProtobuf.ts @@ -1,30 +1,30 @@ import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { CastId_fromMessage } from '../CastId/fromMessage.js' -import type { Embed as Protobufs_Embed } from '../Protobufs/message_pb.js' +import { CastId_fromProtobuf } from '../CastId/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' import { Embed_InvalidEmbedTypeError } from './errors.js' import type { Embed } from './types.js' -export function Embed_fromMessage( - embed: Protobufs_Embed['embed'], -): Embed_fromMessage.ReturnType { +export function Embed_fromProtobuf( + embed: Embed_fromProtobuf.ParametersType, +): Embed_fromProtobuf.ReturnType { if (typeof embed.case === 'undefined') return undefined if (embed.case === 'url') return { type: 'url', url: embed.value } as const if (embed.case === 'castId') return { type: 'cast', - ...CastId_fromMessage(embed.value), + ...CastId_fromProtobuf(embed.value), } as const throw new Embed_InvalidEmbedTypeError({ embed }) } -export declare namespace Embed_fromMessage { +export declare namespace Embed_fromProtobuf { + type ParametersType = MessageProtobuf.Embed['embed'] type ReturnType = Embed | undefined - type ErrorType = | Embed_InvalidEmbedTypeError - | CastId_fromMessage.ErrorType + | CastId_fromProtobuf.ErrorType | GlobalErrorType } -Embed_fromMessage.parseError = (error: unknown) => - error as Embed_fromMessage.ErrorType +Embed_fromProtobuf.parseError = (error: unknown) => + error as Embed_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Embed/toProtobuf.ts b/src/Node/Internal/Embed/toProtobuf.ts new file mode 100644 index 0000000..dd5444b --- /dev/null +++ b/src/Node/Internal/Embed/toProtobuf.ts @@ -0,0 +1,34 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { CastId_toProtobuf } from '../CastId/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Embed } from './types.js' + +export function Embed_toProtobuf( + embed: Embed_toProtobuf.ParametersType, +): Embed_toProtobuf.ReturnType { + if (embed.type === 'cast') + return create(MessageProtobuf.EmbedSchema, { + embed: { + case: 'castId', + value: CastId_toProtobuf(embed), + }, + }) + if (embed.type === 'url') + return create(MessageProtobuf.EmbedSchema, { + embed: { + case: 'url', + value: embed.url, + }, + }) + throw new Error() +} + +export declare namespace Embed_toProtobuf { + type ParametersType = Embed + type ReturnType = MessageProtobuf.Embed + + type ErrorType = GlobalErrorType +} +Embed_toProtobuf.parseError = (error: unknown) => + error as Embed_toProtobuf.ErrorType diff --git a/src/Node/Internal/FrameActionBody/fromMessage.ts b/src/Node/Internal/FrameActionBody/fromProtobuf.ts similarity index 54% rename from src/Node/Internal/FrameActionBody/fromMessage.ts rename to src/Node/Internal/FrameActionBody/fromProtobuf.ts index 03a706e..e80eb5f 100644 --- a/src/Node/Internal/FrameActionBody/fromMessage.ts +++ b/src/Node/Internal/FrameActionBody/fromProtobuf.ts @@ -1,22 +1,22 @@ import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { CastId_fromMessage } from '../CastId/fromMessage.js' -import { Meta_fromMessage } from '../Meta/fromMessage.js' -import type { Message as Protobufs_Message } from '../Protobufs/message_pb.js' +import { CastId_fromProtobuf } from '../CastId/fromProtobuf.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' import type { FrameActionBody } from './types.js' -export function FrameActionBody_fromMessage( - message: Protobufs_Message, -): FrameActionBody_fromMessage.ReturnType { +export function FrameActionBody_fromProtobuf( + message: FrameActionBody_fromProtobuf.ParametersType, +): FrameActionBody_fromProtobuf.ReturnType { //@TODO: errors if (!message.data) throw new Error('no data') if (message.data.body.case !== 'frameActionBody') throw new Error('bad case') return { - meta: Meta_fromMessage(message), + meta: Meta_fromProtobuf(message), url: message.data.body.value.url.toString(), buttonIndex: message.data.body.value.buttonIndex as 1 | 2 | 3 | 4, castId: message.data.body.value.castId - ? CastId_fromMessage(message.data.body.value.castId) + ? CastId_fromProtobuf(message.data.body.value.castId) : undefined, inputText: message.data.body.value.inputText.toString(), state: message.data.body.value.state.toString(), @@ -25,11 +25,11 @@ export function FrameActionBody_fromMessage( } as const } -export declare namespace FrameActionBody_fromMessage { +export declare namespace FrameActionBody_fromProtobuf { + type ParametersType = MessageProtobuf.Message type ReturnType = FrameActionBody - type ErrorType = GlobalErrorType } -FrameActionBody_fromMessage.parseError = (error: unknown) => - error as FrameActionBody_fromMessage.ErrorType +FrameActionBody_fromProtobuf.parseError = (error: unknown) => + error as FrameActionBody_fromProtobuf.ErrorType diff --git a/src/Node/Internal/HubEvent/fromMessage.ts b/src/Node/Internal/HubEvent/fromMessage.ts deleted file mode 100644 index 4bf536a..0000000 --- a/src/Node/Internal/HubEvent/fromMessage.ts +++ /dev/null @@ -1,62 +0,0 @@ -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { Message_fromMessage } from '../Message/fromMessage.js' -import { - HubEventType, - type HubEvent as ProtobufHubEvent, -} from '../Protobufs/hub_event_pb.js' -import type { HubEvent } from './types.js' - -export function HubEvent_fromMessage( - hubEvent: ProtobufHubEvent, -): HubEvent_fromMessage.ReturnType { - if ( - hubEvent.type === HubEventType.MERGE_MESSAGE && - hubEvent.body.case === 'mergeMessageBody' - ) { - return { - type: 'mergeMessage', - id: hubEvent.id, - message: hubEvent.body.value.message - ? Message_fromMessage(hubEvent.body.value.message) - : undefined, - deletedMesssages: - hubEvent.body.value.deletedMessages.map(Message_fromMessage), - } - } - - if ( - hubEvent.type === HubEventType.PRUNE_MESSAGE && - hubEvent.body.case === 'pruneMessageBody' - ) { - return { - type: 'pruneMessage', - id: hubEvent.id, - message: hubEvent.body.value.message - ? Message_fromMessage(hubEvent.body.value.message) - : undefined, - } - } - - if ( - hubEvent.type === HubEventType.REVOKE_MESSAGE && - hubEvent.body.case === 'revokeMessageBody' - ) { - return { - type: 'revokeMessage', - id: hubEvent.id, - message: hubEvent.body.value.message - ? Message_fromMessage(hubEvent.body.value.message) - : undefined, - } - } - throw new Error('Not implemented') -} - -export declare namespace HubEvent_fromMessage { - type ReturnType = HubEvent - - type ErrorType = GlobalErrorType -} - -HubEvent_fromMessage.parseError = (error: unknown) => - error as HubEvent_fromMessage.ErrorType diff --git a/src/Node/Internal/HubEvent/fromProtobuf.ts b/src/Node/Internal/HubEvent/fromProtobuf.ts new file mode 100644 index 0000000..d50ecdd --- /dev/null +++ b/src/Node/Internal/HubEvent/fromProtobuf.ts @@ -0,0 +1,59 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Message_fromProtobuf } from '../Message/fromProtobuf.js' +import * as HubEventProtobuf from '../Protobufs/hub_event_pb.js' +import type { HubEvent } from './types.js' + +export function HubEvent_fromProtobuf( + hubEvent: HubEvent_fromProtobuf.ParametersType, +): HubEvent_fromProtobuf.ReturnType { + if ( + hubEvent.type === HubEventProtobuf.HubEventType.MERGE_MESSAGE && + hubEvent.body.case === 'mergeMessageBody' + ) { + return { + type: 'mergeMessage', + id: hubEvent.id, + message: hubEvent.body.value.message + ? Message_fromProtobuf(hubEvent.body.value.message) + : undefined, + deletedMesssages: + hubEvent.body.value.deletedMessages.map(Message_fromProtobuf), + } + } + + if ( + hubEvent.type === HubEventProtobuf.HubEventType.PRUNE_MESSAGE && + hubEvent.body.case === 'pruneMessageBody' + ) { + return { + type: 'pruneMessage', + id: hubEvent.id, + message: hubEvent.body.value.message + ? Message_fromProtobuf(hubEvent.body.value.message) + : undefined, + } + } + + if ( + hubEvent.type === HubEventProtobuf.HubEventType.REVOKE_MESSAGE && + hubEvent.body.case === 'revokeMessageBody' + ) { + return { + type: 'revokeMessage', + id: hubEvent.id, + message: hubEvent.body.value.message + ? Message_fromProtobuf(hubEvent.body.value.message) + : undefined, + } + } + throw new Error('Not implemented') +} + +export declare namespace HubEvent_fromProtobuf { + type ParametersType = HubEventProtobuf.HubEvent + type ReturnType = HubEvent + type ErrorType = GlobalErrorType +} + +HubEvent_fromProtobuf.parseError = (error: unknown) => + error as HubEvent_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Link/errors.ts b/src/Node/Internal/Link/errors.ts new file mode 100644 index 0000000..1cc4243 --- /dev/null +++ b/src/Node/Internal/Link/errors.ts @@ -0,0 +1,11 @@ +import { BaseError } from 'ox/Errors' + +export class Link_InvalidMessageTypeError extends BaseError { + override readonly name = 'Link.InvalidMessageTypeError' + + constructor({ hash }: { hash: string }) { + super(`Message "${hash}" has type other than CAST_ADD.`, { + docsPath: '/errors#invalidmessagetypeerror', + }) + } +} diff --git a/src/Node/Internal/Link/fromMessage.ts b/src/Node/Internal/Link/fromMessage.ts deleted file mode 100644 index bd571c6..0000000 --- a/src/Node/Internal/Link/fromMessage.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { Meta_fromMessage } from '../Meta/fromMessage.js' -import type { Message as Protobufs_Message } from '../Protobufs/message_pb.js' -import type { Link } from './types.js' - -//@TODO: errors -export function Link_fromMessage( - message: Protobufs_Message, -): Link_fromMessage.ReturnType { - if (!message.data) throw new Error('Link must have data') - - if (message.data.body.case !== 'linkBody') throw new Error('Not link message') - if (message.data.body.value.target.case !== 'targetFid') - throw new Error('Invalid target') - - return { - meta: Meta_fromMessage(message), - type: message.data.body.value.type, - displayTimestamp: message.data.body.value.displayTimestamp, - fid: message.data.body.value.target.value, - } -} - -export declare namespace Link_fromMessage { - type ReturnType = Link - - type ErrorType = GlobalErrorType -} -Link_fromMessage.parseError = (error: unknown) => - error as Link_fromMessage.ErrorType diff --git a/src/Node/Internal/Link/fromMessageProtobuf.ts b/src/Node/Internal/Link/fromMessageProtobuf.ts new file mode 100644 index 0000000..e049fdf --- /dev/null +++ b/src/Node/Internal/Link/fromMessageProtobuf.ts @@ -0,0 +1,34 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Link_fromProtobuf } from '../Link/fromProtobuf.js' +import type { Link } from '../Link/types.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import { Link_InvalidMessageTypeError } from './errors.js' + +export function Link_fromMessageProtobuf( + message: Link_fromMessageProtobuf.ParametersType, +): Link_fromMessageProtobuf.ReturnType { + const meta = Meta_fromProtobuf(message) + // @TODO: separate error here + if (!message.data) throw new Error('`data` must be defined in Link message.') + if (message.data.body.case !== 'linkBody') + throw new Link_InvalidMessageTypeError({ hash: meta.hash }) + return Link_fromProtobuf({ + body: message.data.body.value, + meta, + fid: message.data.fid, + timestamp: message.data.timestamp, + }) +} + +export declare namespace Link_fromMessageProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = Link + type ErrorType = + | Link_InvalidMessageTypeError + | Meta_fromProtobuf.ErrorType + | GlobalErrorType +} + +Link_fromMessageProtobuf.parseError = (error: unknown) => + error as Link_fromMessageProtobuf.ErrorType diff --git a/src/Node/Internal/Link/fromProtobuf.ts b/src/Node/Internal/Link/fromProtobuf.ts new file mode 100644 index 0000000..f64c1bb --- /dev/null +++ b/src/Node/Internal/Link/fromProtobuf.ts @@ -0,0 +1,33 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { Meta } from '../Meta/types.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Link } from './types.js' + +//@TODO: errors +export function Link_fromProtobuf( + parameters: Link_fromProtobuf.ParametersType, +): Link_fromProtobuf.ReturnType { + if (parameters.body.target.case !== 'targetFid') + throw new Error('Invalid target') + + return { + meta: parameters.meta, + type: parameters.body.type, + displayTimestamp: parameters.body.displayTimestamp, + fid: parameters.body.target.value, + timestamp: parameters.timestamp, + } +} + +export declare namespace Link_fromProtobuf { + type ParametersType = { + body: MessageProtobuf.LinkBody + meta: Meta + fid: bigint + timestamp: number + } + type ReturnType = Link + type ErrorType = GlobalErrorType +} +Link_fromProtobuf.parseError = (error: unknown) => + error as Link_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Link/toProtobuf.ts b/src/Node/Internal/Link/toProtobuf.ts new file mode 100644 index 0000000..8eadda3 --- /dev/null +++ b/src/Node/Internal/Link/toProtobuf.ts @@ -0,0 +1,26 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Link } from './types.js' + +export function Link_toProtobuf( + link: Link_toProtobuf.ParametersType, +): Link_toProtobuf.ReturnType { + return create(MessageProtobuf.LinkBodySchema, { + type: link.type, + ...(link.displayTimestamp + ? { displayTimestamp: link.displayTimestamp } + : {}), + target: { case: 'targetFid', value: link.fid }, + }) +} + +export declare namespace Link_toProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.LinkBody + + type ErrorType = GlobalErrorType +} + +Link_toProtobuf.parseError = (error: unknown) => + error as Link_toProtobuf.ErrorType diff --git a/src/Node/Internal/Link/types.ts b/src/Node/Internal/Link/types.ts index 38ad31c..71e670d 100644 --- a/src/Node/Internal/Link/types.ts +++ b/src/Node/Internal/Link/types.ts @@ -4,5 +4,6 @@ export type Link = { meta: Meta type: string displayTimestamp: number | undefined + timestamp: number fid: bigint } diff --git a/src/Node/Internal/LinkAdd/toHex.ts b/src/Node/Internal/LinkAdd/toHex.ts new file mode 100644 index 0000000..9122045 --- /dev/null +++ b/src/Node/Internal/LinkAdd/toHex.ts @@ -0,0 +1,22 @@ +import { toBinary } from '@bufbuild/protobuf' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { Link } from '../Link/types.js' +import * as Protobuf from '../Protobufs/message_pb.js' +import { LinkAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function LinkAdd_toHex( + link: LinkAdd_toHex.ParametersType, +): LinkAdd_toHex.ReturnType { + return Hex.fromBytes( + toBinary(Protobuf.MessageDataSchema, LinkAdd_toMessageDataProtobuf(link)), + ) +} + +export declare namespace LinkAdd_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +LinkAdd_toHex.parseError = (error: unknown) => error as LinkAdd_toHex.ErrorType diff --git a/src/Node/Internal/LinkAdd/toMessageDataProtobuf.ts b/src/Node/Internal/LinkAdd/toMessageDataProtobuf.ts new file mode 100644 index 0000000..88d3dc0 --- /dev/null +++ b/src/Node/Internal/LinkAdd/toMessageDataProtobuf.ts @@ -0,0 +1,31 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import { Link_toProtobuf } from '../Link/toProtobuf.js' +import type { Link } from '../Link/types.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' + +export function LinkAdd_toMessageDataProtobuf( + link: LinkAdd_toMessageDataProtobuf.ParametersType, +): LinkAdd_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: link.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.LINK_ADD, + fid: link.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'linkBody', + value: Link_toProtobuf(link), + }, + }) +} + +export declare namespace LinkAdd_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +LinkAdd_toMessageDataProtobuf.parseError = (error: unknown) => + error as LinkAdd_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/LinkAdd/toMessageProtobuf.ts b/src/Node/Internal/LinkAdd/toMessageProtobuf.ts new file mode 100644 index 0000000..727a0f1 --- /dev/null +++ b/src/Node/Internal/LinkAdd/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { Link } from '../Link/types.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { LinkAdd_toHex } from './toHex.js' +import { LinkAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function LinkAdd_toMessageProtobuf( + parameters: LinkAdd_toMessageProtobuf.ParametersType, +): LinkAdd_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: LinkAdd_toHex(parameters.link), + privateKey: parameters.privateKey, + }), + ), + data: LinkAdd_toMessageDataProtobuf(parameters.link), + }) +} + +export declare namespace LinkAdd_toMessageProtobuf { + type ParametersType = { + link: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +LinkAdd_toMessageProtobuf.parseError = (error: unknown) => + error as LinkAdd_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/LinkCompactState/fromMessage.ts b/src/Node/Internal/LinkCompactState/fromMessage.ts deleted file mode 100644 index 451c5f4..0000000 --- a/src/Node/Internal/LinkCompactState/fromMessage.ts +++ /dev/null @@ -1,28 +0,0 @@ -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { Meta_fromMessage } from '../Meta/fromMessage.js' -import type { Message as Protobufs_Message } from '../Protobufs/message_pb.js' -import type { LinkCompactState } from './types.js' - -//@TODO: errors -export function LinkCompactState_fromMessage( - message: Protobufs_Message, -): LinkCompactState_fromMessage.ReturnType { - if (!message.data) throw new Error('Link must have data') - - if (message.data.body.case !== 'linkCompactStateBody') - throw new Error('Not linkCompactStateBody message') - - return { - meta: Meta_fromMessage(message), - type: message.data.body.value.type, - fids: message.data.body.value.targetFids, - } -} - -export declare namespace LinkCompactState_fromMessage { - type ReturnType = LinkCompactState - - type ErrorType = GlobalErrorType -} -LinkCompactState_fromMessage.parseError = (error: unknown) => - error as LinkCompactState_fromMessage.ErrorType diff --git a/src/Node/Internal/LinkCompactState/fromMessageProtobuf.ts b/src/Node/Internal/LinkCompactState/fromMessageProtobuf.ts new file mode 100644 index 0000000..74e766b --- /dev/null +++ b/src/Node/Internal/LinkCompactState/fromMessageProtobuf.ts @@ -0,0 +1,30 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { LinkCompactState } from './types.js' + +//@TODO: errors +export function LinkCompactState_fromMessageProtobuf( + message: LinkCompactState_fromMessageProtobuf.ParametersType, +): LinkCompactState_fromMessageProtobuf.ReturnType { + if (!message.data) throw new Error('Link must have data') + + if (message.data.body.case !== 'linkCompactStateBody') + throw new Error('Not linkCompactStateBody message') + + return { + meta: Meta_fromProtobuf(message), + type: message.data.body.value.type, + fids: message.data.body.value.targetFids, + fid: message.data.fid, + timestamp: message.data.timestamp, + } +} + +export declare namespace LinkCompactState_fromMessageProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = LinkCompactState + type ErrorType = GlobalErrorType +} +LinkCompactState_fromMessageProtobuf.parseError = (error: unknown) => + error as LinkCompactState_fromMessageProtobuf.ErrorType diff --git a/src/Node/Internal/LinkCompactState/toHex.ts b/src/Node/Internal/LinkCompactState/toHex.ts new file mode 100644 index 0000000..3c50e5d --- /dev/null +++ b/src/Node/Internal/LinkCompactState/toHex.ts @@ -0,0 +1,26 @@ +import { toBinary } from '@bufbuild/protobuf' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { LinkCompactState } from '../LinkCompactState/types.js' +import * as Protobuf from '../Protobufs/message_pb.js' +import { LinkCompactStateAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function LinkCompactStateAdd_toHex( + link: LinkCompactStateAdd_toHex.ParametersType, +): LinkCompactStateAdd_toHex.ReturnType { + return Hex.fromBytes( + toBinary( + Protobuf.MessageDataSchema, + LinkCompactStateAdd_toMessageDataProtobuf(link), + ), + ) +} + +export declare namespace LinkCompactStateAdd_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +LinkCompactStateAdd_toHex.parseError = (error: unknown) => + error as LinkCompactStateAdd_toHex.ErrorType diff --git a/src/Node/Internal/LinkCompactState/toMessageDataProtobuf.ts b/src/Node/Internal/LinkCompactState/toMessageDataProtobuf.ts new file mode 100644 index 0000000..32a1b1c --- /dev/null +++ b/src/Node/Internal/LinkCompactState/toMessageDataProtobuf.ts @@ -0,0 +1,33 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import type { LinkCompactState } from '../LinkCompactState/types.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' + +export function LinkCompactStateAdd_toMessageDataProtobuf( + link: LinkCompactStateAdd_toMessageDataProtobuf.ParametersType, +): LinkCompactStateAdd_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: link.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.LINK_COMPACT_STATE, + fid: link.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'linkCompactStateBody', + value: create(MessageProtobuf.LinkCompactStateBodySchema, { + type: link.type, + targetFids: link.fids, + }), + }, + }) +} + +export declare namespace LinkCompactStateAdd_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +LinkCompactStateAdd_toMessageDataProtobuf.parseError = (error: unknown) => + error as LinkCompactStateAdd_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/LinkCompactState/toMessageProtobuf.ts b/src/Node/Internal/LinkCompactState/toMessageProtobuf.ts new file mode 100644 index 0000000..6230a08 --- /dev/null +++ b/src/Node/Internal/LinkCompactState/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { LinkCompactState } from '../LinkCompactState/types.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { LinkCompactStateAdd_toHex } from './toHex.js' +import { LinkCompactStateAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function LinkCompactStateAdd_toMessageProtobuf( + parameters: LinkCompactStateAdd_toMessageProtobuf.ParametersType, +): LinkCompactStateAdd_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: LinkCompactStateAdd_toHex(parameters.link), + privateKey: parameters.privateKey, + }), + ), + data: LinkCompactStateAdd_toMessageDataProtobuf(parameters.link), + }) +} + +export declare namespace LinkCompactStateAdd_toMessageProtobuf { + type ParametersType = { + link: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +LinkCompactStateAdd_toMessageProtobuf.parseError = (error: unknown) => + error as LinkCompactStateAdd_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/LinkCompactState/types.ts b/src/Node/Internal/LinkCompactState/types.ts index 3d8b7b6..26ed8d2 100644 --- a/src/Node/Internal/LinkCompactState/types.ts +++ b/src/Node/Internal/LinkCompactState/types.ts @@ -4,4 +4,6 @@ export type LinkCompactState = { meta: Meta type: string fids: bigint[] + fid: bigint + timestamp: number } diff --git a/src/Node/Internal/LinkRemove/toHex.ts b/src/Node/Internal/LinkRemove/toHex.ts new file mode 100644 index 0000000..be8d739 --- /dev/null +++ b/src/Node/Internal/LinkRemove/toHex.ts @@ -0,0 +1,26 @@ +import { toBinary } from '@bufbuild/protobuf' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { Link } from '../Link/types.js' +import * as Protobuf from '../Protobufs/message_pb.js' +import { LinkRemove_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function LinkRemove_toHex( + link: LinkRemove_toHex.ParametersType, +): LinkRemove_toHex.ReturnType { + return Hex.fromBytes( + toBinary( + Protobuf.MessageDataSchema, + LinkRemove_toMessageDataProtobuf(link), + ), + ) +} + +export declare namespace LinkRemove_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +LinkRemove_toHex.parseError = (error: unknown) => + error as LinkRemove_toHex.ErrorType diff --git a/src/Node/Internal/LinkRemove/toMessageDataProtobuf.ts b/src/Node/Internal/LinkRemove/toMessageDataProtobuf.ts new file mode 100644 index 0000000..14a3e23 --- /dev/null +++ b/src/Node/Internal/LinkRemove/toMessageDataProtobuf.ts @@ -0,0 +1,31 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import { Link_toProtobuf } from '../Link/toProtobuf.js' +import type { Link } from '../Link/types.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' + +export function LinkRemove_toMessageDataProtobuf( + link: LinkRemove_toMessageDataProtobuf.ParametersType, +): LinkRemove_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: link.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.LINK_REMOVE, + fid: link.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'linkBody', + value: Link_toProtobuf(link), + }, + }) +} + +export declare namespace LinkRemove_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +LinkRemove_toMessageDataProtobuf.parseError = (error: unknown) => + error as LinkRemove_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/LinkRemove/toMessageProtobuf.ts b/src/Node/Internal/LinkRemove/toMessageProtobuf.ts new file mode 100644 index 0000000..d4a6c39 --- /dev/null +++ b/src/Node/Internal/LinkRemove/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { Link } from '../Link/types.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { LinkRemove_toHex } from './toHex.js' +import { LinkRemove_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function LinkRemove_toMessageProtobuf( + parameters: LinkRemove_toMessageProtobuf.ParametersType, +): LinkRemove_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: LinkRemove_toHex(parameters.link), + privateKey: parameters.privateKey, + }), + ), + data: LinkRemove_toMessageDataProtobuf(parameters.link), + }) +} + +export declare namespace LinkRemove_toMessageProtobuf { + type ParametersType = { + link: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +LinkRemove_toMessageProtobuf.parseError = (error: unknown) => + error as LinkRemove_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/Message/fromMessage.ts b/src/Node/Internal/Message/fromMessage.ts deleted file mode 100644 index 9f6cacd..0000000 --- a/src/Node/Internal/Message/fromMessage.ts +++ /dev/null @@ -1,77 +0,0 @@ -import { Hex } from 'ox' -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { Cast_fromMessage } from '../Cast/fromMessage.js' -import { FrameActionBody_fromMessage } from '../FrameActionBody/fromMessage.js' -import { Link_fromMessage } from '../Link/fromMessage.js' -import { LinkCompactState_fromMessage } from '../LinkCompactState/fromMessage.js' -import { - MessageType, - type Message as ProtobufMessage, -} from '../Protobufs/message_pb.js' -import { Reaction_fromMessage } from '../Reaction/fromMessage.js' -import { UserData_fromMessage } from '../UserData/fromMessage.js' -import { UsernameProof_fromMessage } from '../UsernameProof/fromMessage.js' -import { Verification_fromMessage } from '../Verification/fromMessage.js' -import { VerificationRemoval_fromMessage } from '../VerificationRemoval/fromMessage.js' -import type { Message } from './types.js' - -export function Message_fromMessage( - message: ProtobufMessage, -): Message_fromMessage.ReturnType { - // @TODO: separate error here - if (!message.data) - throw new Error('`data` must be defined in Message message.') - if (message.data.type === MessageType.CAST_ADD) - return { type: 'castAdd', cast: Cast_fromMessage(message) } - if ( - message.data.type === MessageType.CAST_REMOVE && - message.data.body.case === 'castRemoveBody' - ) - return { - type: 'castRemove', - hash: Hex.fromBytes(message.data.body.value.targetHash), - } - - if (message.data.type === MessageType.REACTION_ADD) - return { type: 'reactionAdd', reaction: Reaction_fromMessage(message) } - if (message.data.type === MessageType.REACTION_REMOVE) - return { type: 'reactionRemove', reaction: Reaction_fromMessage(message) } - if (message.data.type === MessageType.LINK_ADD) - return { type: 'linkAdd', link: Link_fromMessage(message) } - if (message.data.type === MessageType.LINK_REMOVE) - return { type: 'linkRemove', link: Link_fromMessage(message) } - if (message.data.type === MessageType.VERIFICATION_ADD_ETH_ADDRESS) { - return { - type: 'verificationAdd', - verification: Verification_fromMessage(message), - } - } - if (message.data.type === MessageType.VERIFICATION_REMOVE) { - return { - type: 'verificationRemove', - verification: VerificationRemoval_fromMessage(message), - } - } - if (message.data.type === MessageType.USER_DATA_ADD) - return { type: 'userData', data: UserData_fromMessage(message) } - if (message.data.type === MessageType.USERNAME_PROOF) - return { type: 'usernameProof', proof: UsernameProof_fromMessage(message) } - if (message.data.type === MessageType.FRAME_ACTION) - return { type: 'frameAction', action: FrameActionBody_fromMessage(message) } - if (message.data.type === MessageType.LINK_COMPACT_STATE) - return { - type: 'linkCompactState', - state: LinkCompactState_fromMessage(message), - } - throw new Error('Not expected') -} - -export declare namespace Message_fromMessage { - type ReturnType = Message - - // @TODO: errors - type ErrorType = GlobalErrorType -} - -Message_fromMessage.parseError = (error: unknown) => - error as Message_fromMessage.ErrorType diff --git a/src/Node/Internal/Message/fromProtobuf.ts b/src/Node/Internal/Message/fromProtobuf.ts new file mode 100644 index 0000000..a55552a --- /dev/null +++ b/src/Node/Internal/Message/fromProtobuf.ts @@ -0,0 +1,94 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { CastAdd_fromMessageProtobuf } from '../CastAdd/fromMessageProtobuf.js' +import { CastRemove_fromMessageProtobuf } from '../CastRemove/fromMessageProtobuf.js' +import { FrameActionBody_fromProtobuf } from '../FrameActionBody/fromProtobuf.js' +import { Link_fromMessageProtobuf } from '../Link/fromMessageProtobuf.js' +import { LinkCompactState_fromMessageProtobuf } from '../LinkCompactState/fromMessageProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { Reaction_fromMessageProtobuf } from '../Reaction/fromMessageProtobuf.js' +import { UserData_fromProtobuf } from '../UserData/fromProtobuf.js' +import { UsernameProof_fromProtobuf } from '../UsernameProof/fromProtobuf.js' +import { VerificationAdd_fromMessageProtobuf } from '../VerificationAdd/fromMessageProtobuf.js' +import { VerificationRemove_fromMessageProtobuf } from '../VerificationRemoval/fromMessageProtobuf.js' +import type { Message } from './types.js' + +export function Message_fromProtobuf( + message: Message_fromProtobuf.ParametersType, +): Message_fromProtobuf.ReturnType { + // @TODO: separate error here + if (!message.data) + throw new Error('`data` must be defined in Message message.') + if (message.data.type === MessageProtobuf.MessageType.CAST_ADD) + return { type: 'castAdd', data: CastAdd_fromMessageProtobuf(message) } + if ( + message.data.type === MessageProtobuf.MessageType.CAST_REMOVE && + message.data.body.case === 'castRemoveBody' + ) + return { + type: 'castRemove', + data: CastRemove_fromMessageProtobuf(message), + } + + if (message.data.type === MessageProtobuf.MessageType.REACTION_ADD) + return { + type: 'reactionAdd', + data: Reaction_fromMessageProtobuf(message), + } + if (message.data.type === MessageProtobuf.MessageType.REACTION_REMOVE) + return { + type: 'reactionRemove', + data: Reaction_fromMessageProtobuf(message), + } + if (message.data.type === MessageProtobuf.MessageType.LINK_ADD) + return { type: 'linkAdd', data: Link_fromMessageProtobuf(message) } + if (message.data.type === MessageProtobuf.MessageType.LINK_REMOVE) + return { type: 'linkRemove', data: Link_fromMessageProtobuf(message) } + if ( + message.data.type === + MessageProtobuf.MessageType.VERIFICATION_ADD_ETH_ADDRESS + ) { + return { + type: 'verificationAdd', + data: VerificationAdd_fromMessageProtobuf(message), + } + } + if (message.data.type === MessageProtobuf.MessageType.VERIFICATION_REMOVE) { + return { + type: 'verificationRemove', + data: VerificationRemove_fromMessageProtobuf(message), + } + } + // TODO: REFACTOR + + /// + /// + /// + if (message.data.type === MessageProtobuf.MessageType.USER_DATA_ADD) + return { type: 'userDataAdd', data: UserData_fromProtobuf(message) } + if (message.data.type === MessageProtobuf.MessageType.USERNAME_PROOF) + return { type: 'usernameProof', data: UsernameProof_fromProtobuf(message) } + if (message.data.type === MessageProtobuf.MessageType.FRAME_ACTION) + return { type: 'frameAction', data: FrameActionBody_fromProtobuf(message) } + + /// + ///// + ///// + ///// + ///// + if (message.data.type === MessageProtobuf.MessageType.LINK_COMPACT_STATE) + return { + type: 'linkCompactState', + data: LinkCompactState_fromMessageProtobuf(message), + } + throw new Error('Not expected') +} + +export declare namespace Message_fromProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = Message + // @TODO: errors + type ErrorType = GlobalErrorType +} + +Message_fromProtobuf.parseError = (error: unknown) => + error as Message_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Message/types.ts b/src/Node/Internal/Message/types.ts index c08184f..cb4a19b 100644 --- a/src/Node/Internal/Message/types.ts +++ b/src/Node/Internal/Message/types.ts @@ -1,5 +1,5 @@ -import type { Types } from 'ox' import type { Cast } from '../Cast/types.js' +import type { CastRemove } from '../CastRemove/types.js' import type { FrameActionBody } from '../FrameActionBody/types.js' import type { Link } from '../Link/types.js' import type { LinkCompactState } from '../LinkCompactState/types.js' @@ -7,54 +7,46 @@ import type { Reaction } from '../Reaction/types.js' import type { UserData } from '../UserData/types.js' import type { UsernameProof } from '../UsernameProof/types.js' import type { Verification } from '../Verification/types.js' -import type { VerificationRemoval } from '../VerificationRemoval/types.js' +import type { VerificationRemove } from '../VerificationRemoval/types.js' export type Message = | { type: 'castAdd' - cast: Cast + data: Cast } | { type: 'castRemove' - hash: Types.Hex + data: CastRemove } | { type: 'reactionAdd' | 'reactionRemove' - reaction: Reaction + data: Reaction } | { - type: 'userData' + type: 'userDataAdd' data: UserData } | { - type: 'userDataRemove' - data: UserData - } - | { - type: 'linkAdd' - link: Link - } - | { - type: 'linkRemove' - link: Link + type: 'linkAdd' | 'linkRemove' + data: Link } | { type: 'verificationAdd' - verification: Verification + data: Verification } | { type: 'verificationRemove' - verification: VerificationRemoval + data: VerificationRemove } | { type: 'usernameProof' - proof: UsernameProof + data: UsernameProof } | { type: 'frameAction' - action: FrameActionBody + data: FrameActionBody } | { type: 'linkCompactState' - state: LinkCompactState + data: LinkCompactState } diff --git a/src/Node/Internal/Meta/create.ts b/src/Node/Internal/Meta/create.ts new file mode 100644 index 0000000..a4bf32d --- /dev/null +++ b/src/Node/Internal/Meta/create.ts @@ -0,0 +1,32 @@ +import * as ed from '@noble/ed25519' +import { blake3 } from '@noble/hashes/blake3' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +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 signature = ed.sign(hash, parameters.privateKey.slice(2)) + return { + hash: Hex.fromBytes(hash), + hashScheme: 'blake3', + signature: Hex.fromBytes(signature), + signatureScheme: 'ed25519', + signer: Hex.fromBytes(ed.getPublicKey(parameters.privateKey.slice(2))), + dataBytes: parameters.dataBytes, + } as const +} + +export declare namespace Meta_create { + type ParametersType = { + dataBytes: Types.Hex + privateKey: Types.Hex + } + type ReturnType = Meta + + type ErrorType = GlobalErrorType +} + +Meta_create.parseError = (error: unknown) => error as Meta_create.ErrorType diff --git a/src/Node/Internal/Meta/fromMessage.ts b/src/Node/Internal/Meta/fromProtobuf.ts similarity index 68% rename from src/Node/Internal/Meta/fromMessage.ts rename to src/Node/Internal/Meta/fromProtobuf.ts index dd6dea1..b29d2c2 100644 --- a/src/Node/Internal/Meta/fromMessage.ts +++ b/src/Node/Internal/Meta/fromProtobuf.ts @@ -2,11 +2,7 @@ import * as ed from '@noble/ed25519' import * as sha from '@noble/hashes/sha512' import { Hex } from 'ox' import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { - HashScheme, - type Message, - SignatureScheme, -} from '../Protobufs/message_pb.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' import { Meta_InvalidHashSchemeTypeError, Meta_InvalidSignatureError, @@ -16,21 +12,24 @@ import type { Meta } from './types.js' ed.etc.sha512Sync = (...m) => sha.sha512(ed.etc.concatBytes(...m)) -export function Meta_fromMessage( - message: Message, -): Meta_fromMessage.ReturnType { +export function Meta_fromProtobuf( + message: Meta_fromProtobuf.ParametersType, +): Meta_fromProtobuf.ReturnType { + //@TODO: error + if (!message.data) throw new Error('') const hash = Hex.fromBytes(message.hash) const signer = Hex.fromBytes(message.signer) const signature = Hex.fromBytes(message.signature) const hashScheme = (() => { - if (message.hashScheme === HashScheme.NONE) return null - if (message.hashScheme === HashScheme.BLAKE3) return 'blake3' + if (message.hashScheme === MessageProtobuf.HashScheme.BLAKE3) + return 'blake3' as const throw new Meta_InvalidHashSchemeTypeError({ hash }) })() const signatureScheme = (() => { - if (message.signatureScheme === SignatureScheme.NONE) return null - if (message.signatureScheme === SignatureScheme.EIP712) return 'eip712' - if (message.signatureScheme === SignatureScheme.ED25519) return 'ed25519' + if (message.signatureScheme === MessageProtobuf.SignatureScheme.EIP712) + return 'eip712' + if (message.signatureScheme === MessageProtobuf.SignatureScheme.ED25519) + return 'ed25519' throw new Meta_InvalidSignatureSchemeTypeError({ hash }) })() @@ -59,7 +58,8 @@ export function Meta_fromMessage( } as const } -export declare namespace Meta_fromMessage { +export declare namespace Meta_fromProtobuf { + type ParametersType = MessageProtobuf.Message type ReturnType = Meta type ErrorType = @@ -69,5 +69,5 @@ export declare namespace Meta_fromMessage { | GlobalErrorType } -Meta_fromMessage.parseError = (error: unknown) => - error as Meta_fromMessage.ErrorType +Meta_fromProtobuf.parseError = (error: unknown) => + error as Meta_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Meta/toProtobuf.ts b/src/Node/Internal/Meta/toProtobuf.ts new file mode 100644 index 0000000..ceb7019 --- /dev/null +++ b/src/Node/Internal/Meta/toProtobuf.ts @@ -0,0 +1,39 @@ +import * as ed from '@noble/ed25519' +import * as sha from '@noble/hashes/sha512' +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Meta } from './types.js' + +ed.etc.sha512Sync = (...m) => sha.sha512(ed.etc.concatBytes(...m)) + +export function Meta_toProtobuf( + meta: Meta_toProtobuf.ParametersType, +): Meta_toProtobuf.ReturnType { + return { + hash: Hex.toBytes(meta.hash), + signer: Hex.toBytes(meta.signer), + signature: Hex.toBytes(meta.signature), + hashScheme: MessageProtobuf.HashScheme.BLAKE3, + signatureScheme: MessageProtobuf.SignatureScheme.ED25519, + ...(meta.dataBytes ? { dataBytes: Hex.toBytes(meta.dataBytes) } : {}), + } as const +} + +export declare namespace Meta_toProtobuf { + type ParametersType = Meta + type ReturnType = Pick< + MessageProtobuf.Message, + | 'hash' + | 'hashScheme' + | 'dataBytes' + | 'signatureScheme' + | 'signature' + | 'signer' + > + + type ErrorType = GlobalErrorType +} + +Meta_toProtobuf.parseError = (error: unknown) => + error as Meta_toProtobuf.ErrorType diff --git a/src/Node/Internal/Meta/types.ts b/src/Node/Internal/Meta/types.ts index ad565de..fa5564e 100644 --- a/src/Node/Internal/Meta/types.ts +++ b/src/Node/Internal/Meta/types.ts @@ -4,7 +4,7 @@ export type Meta = { hash: Types.Hex signer: Types.Hex signature: Types.Hex - hashScheme: 'blake3' | null - signatureScheme: 'ed25519' | 'eip712' | null + hashScheme: 'blake3' + signatureScheme: 'ed25519' | 'eip712' dataBytes: Types.Hex | undefined } diff --git a/src/Node/Internal/Parent/fromMessage.ts b/src/Node/Internal/Parent/fromProtobuf.ts similarity index 52% rename from src/Node/Internal/Parent/fromMessage.ts rename to src/Node/Internal/Parent/fromProtobuf.ts index a533850..174bead 100644 --- a/src/Node/Internal/Parent/fromMessage.ts +++ b/src/Node/Internal/Parent/fromProtobuf.ts @@ -1,31 +1,32 @@ import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { CastId_fromMessage } from '../CastId/fromMessage.js' -import type { CastAddBody } from '../Protobufs/message_pb.js' +import { CastId_fromProtobuf } from '../CastId/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' import { Parent_InvalidParentTypeError } from './errors.js' import type { Parent } from './types.js' -export function Parent_fromMessage( - parent: CastAddBody['parent'], -): Parent_fromMessage.ReturnType { +export function Parent_fromProtobuf( + parent: Parent_fromProtobuf.ParametersType, +): Parent_fromProtobuf.ReturnType { if (typeof parent.case === 'undefined') return undefined if (parent.case === 'parentUrl') return { type: 'url', url: parent.value } as const if (parent.case === 'parentCastId') return { type: 'cast', - ...CastId_fromMessage(parent.value), + ...CastId_fromProtobuf(parent.value), } as const throw new Parent_InvalidParentTypeError({ parent }) } -export declare namespace Parent_fromMessage { +export declare namespace Parent_fromProtobuf { + type ParametersType = MessageProtobuf.CastAddBody['parent'] type ReturnType = Parent | undefined type ErrorType = | Parent_InvalidParentTypeError - | CastId_fromMessage.ErrorType + | CastId_fromProtobuf.ErrorType | GlobalErrorType } -Parent_fromMessage.parseError = (error: unknown) => - error as Parent_fromMessage.ErrorType +Parent_fromProtobuf.parseError = (error: unknown) => + error as Parent_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Parent/toMessage.ts b/src/Node/Internal/Parent/toProtobuf.ts similarity index 51% rename from src/Node/Internal/Parent/toMessage.ts rename to src/Node/Internal/Parent/toProtobuf.ts index 55c420a..cfdd781 100644 --- a/src/Node/Internal/Parent/toMessage.ts +++ b/src/Node/Internal/Parent/toProtobuf.ts @@ -1,12 +1,12 @@ import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { CastId_toMessage } from '../CastId/toMessage.js' -import type { CastAddBody } from '../Protobufs/message_pb.js' +import { CastId_toProtobuf } from '../CastId/toProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' import { Parent_InvalidParentTypeError } from './errors.js' import type { Parent } from './types.js' -export function Parent_toMessage( - parent: Parent | undefined, -): Parent_toMessage.ReturnType { +export function Parent_toProtobuf( + parent: Parent_toProtobuf.ParametersType, +): Parent_toProtobuf.ReturnType { if (typeof parent === 'undefined') return { case: undefined, @@ -17,19 +17,20 @@ export function Parent_toMessage( if (parent.type === 'cast') return { case: 'parentCastId', - value: CastId_toMessage(parent), + value: CastId_toProtobuf(parent), } as const throw new Parent_InvalidParentTypeError({ parent }) } -export declare namespace Parent_toMessage { - type ReturnType = CastAddBody['parent'] +export declare namespace Parent_toProtobuf { + type ParametersType = Parent | undefined + type ReturnType = MessageProtobuf.CastAddBody['parent'] type ErrorType = | Parent_InvalidParentTypeError - | CastId_toMessage.ErrorType + | CastId_toProtobuf.ErrorType | GlobalErrorType } -Parent_toMessage.parseError = (error: unknown) => - error as Parent_toMessage.ErrorType +Parent_toProtobuf.parseError = (error: unknown) => + error as Parent_toProtobuf.ErrorType diff --git a/src/Node/Internal/Reaction/errors.ts b/src/Node/Internal/Reaction/errors.ts new file mode 100644 index 0000000..0af6425 --- /dev/null +++ b/src/Node/Internal/Reaction/errors.ts @@ -0,0 +1,12 @@ +import { BaseError } from 'ox/Errors' + +// @TODO: replace by our own BaseError +export class Reaction_InvalidMessageTypeError extends BaseError { + override readonly name = 'Reaction.InvalidMessageTypeError' + + constructor({ hash }: { hash: string }) { + super(`Message "${hash}" has type other than CAST_ADD.`, { + docsPath: '/errors#invalidmessagetypeerror', + }) + } +} diff --git a/src/Node/Internal/Reaction/fromMessage.ts b/src/Node/Internal/Reaction/fromMessage.ts deleted file mode 100644 index 2034cca..0000000 --- a/src/Node/Internal/Reaction/fromMessage.ts +++ /dev/null @@ -1,79 +0,0 @@ -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { CastId_fromMessage } from '../CastId/fromMessage.js' -import { Meta_fromMessage } from '../Meta/fromMessage.js' -import { - type Message, - type ReactionBody as Protobufs_Reaction, - ReactionType as Protobufs_ReactionType, -} from '../Protobufs/message_pb.js' -import type { Reaction, ReactionTarget, ReactionType } from './types.js' - -export function ReactionType_fromMessage( - reactionType: Protobufs_ReactionType, -): ReactionType_fromMessage.ReturnType { - return reactionType === Protobufs_ReactionType.RECAST - ? ('recast' as const) - : ('like' as const) -} - -export declare namespace ReactionType_fromMessage { - type ReturnType = ReactionType - - type ErrorType = GlobalErrorType -} - -ReactionType_fromMessage.parseError = (error: unknown) => - error as ReactionType_fromMessage.ErrorType - -export function ReactionTarget_fromMessage( - target: Protobufs_Reaction['target'], -): ReactionTarget_fromMessage.ReturnType { - return target.case === 'targetUrl' - ? { - type: 'url', - url: target.value, - } - : { - type: 'cast', - ...CastId_fromMessage(target.value!), - } -} - -export declare namespace ReactionTarget_fromMessage { - type ReturnType = ReactionTarget - - type ErrorType = GlobalErrorType -} - -ReactionTarget_fromMessage.parseError = (error: unknown) => - error as ReactionTarget_fromMessage.ErrorType - -export function Reaction_fromMessage( - message: Message, -): Reaction_fromMessage.ReturnType { - const meta = Meta_fromMessage(message) - - // @TODO: separate error here - if (!message.data) - throw new Error('`data` must be defined in Reaction message.') - if (message.data.body.case !== 'reactionBody') - // @TODO: error - throw new Error('invalid') - - return { - meta, - fid: message.data.fid, - timestamp: message.data.timestamp, - type: ReactionType_fromMessage(message.data.body.value.type), - target: ReactionTarget_fromMessage(message.data.body.value.target), - } as const -} - -export declare namespace Reaction_fromMessage { - type ReturnType = Reaction - - type ErrorType = GlobalErrorType -} - -Reaction_fromMessage.parseError = (error: unknown) => - error as Reaction_fromMessage.ErrorType diff --git a/src/Node/Internal/Reaction/fromMessageProtobuf.ts b/src/Node/Internal/Reaction/fromMessageProtobuf.ts new file mode 100644 index 0000000..80bbf19 --- /dev/null +++ b/src/Node/Internal/Reaction/fromMessageProtobuf.ts @@ -0,0 +1,35 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import { Reaction_InvalidMessageTypeError } from '../Reaction/errors.js' +import { Reaction_fromProtobuf } from './fromProtobuf.js' +import type { Reaction } from './types.js' + +export function Reaction_fromMessageProtobuf( + message: Reaction_fromMessageProtobuf.ParametersType, +): Reaction_fromMessageProtobuf.ReturnType { + const meta = Meta_fromProtobuf(message) + // @TODO: separate error here + if (!message.data) + throw new Error('`data` must be defined in Reaction message.') + if (message.data.body.case !== 'reactionBody') + throw new Reaction_InvalidMessageTypeError({ hash: meta.hash }) + return Reaction_fromProtobuf({ + body: message.data.body.value, + meta, + fid: message.data.fid, + timestamp: message.data.timestamp, + }) +} + +export declare namespace Reaction_fromMessageProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = Reaction + type ErrorType = + | Reaction_InvalidMessageTypeError + | Meta_fromProtobuf.ErrorType + | GlobalErrorType +} + +Reaction_fromMessageProtobuf.parseError = (error: unknown) => + error as Reaction_fromMessageProtobuf.ErrorType diff --git a/src/Node/Internal/Reaction/fromProtobuf.ts b/src/Node/Internal/Reaction/fromProtobuf.ts new file mode 100644 index 0000000..f2d7aa2 --- /dev/null +++ b/src/Node/Internal/Reaction/fromProtobuf.ts @@ -0,0 +1,32 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { Meta } from '../Meta/types.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import { ReactionTarget_fromProtobuf } from '../ReactionTarget/fromProtobuf.js' +import { ReactionType_fromProtobuf } from '../ReactionType/fromProtobuf.js' +import type { Reaction } from './types.js' + +export function Reaction_fromProtobuf( + parameters: Reaction_fromProtobuf.ParametersType, +): Reaction_fromProtobuf.ReturnType { + return { + meta: parameters.meta, + fid: parameters.fid, + timestamp: parameters.timestamp, + type: ReactionType_fromProtobuf(parameters.body.type), + target: ReactionTarget_fromProtobuf(parameters.body.target), + } as const +} + +export declare namespace Reaction_fromProtobuf { + type ParametersType = { + body: MessageProtobuf.ReactionBody + meta: Meta + fid: bigint + timestamp: number + } + type ReturnType = Reaction + type ErrorType = GlobalErrorType +} + +Reaction_fromProtobuf.parseError = (error: unknown) => + error as Reaction_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Reaction/toMessage.ts b/src/Node/Internal/Reaction/toMessage.ts deleted file mode 100644 index 1892916..0000000 --- a/src/Node/Internal/Reaction/toMessage.ts +++ /dev/null @@ -1,61 +0,0 @@ -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { CastId_toMessage } from '../CastId/toMessage.js' -import { - type ReactionBody as Protobufs_Reaction, - ReactionType as Protobufs_ReactionType, -} from '../Protobufs/message_pb.js' -import type { Reaction, ReactionTarget, ReactionType } from './types.js' - -export function ReactionType_toMessage( - type: ReactionType, -): ReactionType_toMessage.ReturnType { - if (type === 'like') return Protobufs_ReactionType.LIKE - if (type === 'recast') return Protobufs_ReactionType.RECAST - // @TODO: error - throw new Error('unknwo tyep') -} - -export function ReactionTarget_toMessage( - target: ReactionTarget, -): ReactionTarget_toMessage.ReturnType { - return target.type === 'cast' - ? { value: CastId_toMessage(target), case: 'targetCastId' } - : { value: target.url, case: 'targetUrl' } -} - -export function Reaction_toMessage( - reaction: Reaction, -): Reaction_toMessage.ReturnType { - return { - $typeName: 'ReactionBody', - type: ReactionType_toMessage(reaction.type), - target: ReactionTarget_toMessage(reaction.target), - } as const -} - -export declare namespace ReactionType_toMessage { - type ReturnType = Protobufs_ReactionType - - type ErrorType = GlobalErrorType -} - -ReactionType_toMessage.parseError = (error: unknown) => - error as ReactionType_toMessage.ErrorType - -export declare namespace ReactionTarget_toMessage { - type ReturnType = Protobufs_Reaction['target'] - - type ErrorTarget = GlobalErrorType -} - -ReactionTarget_toMessage.parseError = (error: unknown) => - error as ReactionTarget_toMessage.ErrorTarget - -export declare namespace Reaction_toMessage { - type ReturnType = Protobufs_Reaction - - type ErrorType = GlobalErrorType -} - -Reaction_toMessage.parseError = (error: unknown) => - error as Reaction_toMessage.ErrorType diff --git a/src/Node/Internal/Reaction/toProtobuf.ts b/src/Node/Internal/Reaction/toProtobuf.ts new file mode 100644 index 0000000..93e6a9a --- /dev/null +++ b/src/Node/Internal/Reaction/toProtobuf.ts @@ -0,0 +1,25 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { ReactionTarget_toProtobuf } from '../ReactionTarget/toProtobuf.js' +import { ReactionType_toProtobuf } from '../ReactionType/toProtobuf.js' +import type { Reaction } from './types.js' + +export function Reaction_toProtobuf( + reaction: Reaction_toProtobuf.ParametersType, +): Reaction_toProtobuf.ReturnType { + return create(MessageProtobuf.ReactionBodySchema, { + type: ReactionType_toProtobuf(reaction.type), + target: ReactionTarget_toProtobuf(reaction.target), + }) +} + +export declare namespace Reaction_toProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.ReactionBody + + type ErrorType = GlobalErrorType +} + +Reaction_toProtobuf.parseError = (error: unknown) => + error as Reaction_toProtobuf.ErrorType diff --git a/src/Node/Internal/Reaction/types.ts b/src/Node/Internal/Reaction/types.ts index efef363..1b620cc 100644 --- a/src/Node/Internal/Reaction/types.ts +++ b/src/Node/Internal/Reaction/types.ts @@ -1,15 +1,6 @@ -import type { CastId } from '../CastId/types.js' import type { Meta } from '../Meta/types.js' - -export type ReactionType = 'like' | 'recast' -export type ReactionTarget = - | ({ - type: 'cast' - } & CastId) - | { - type: 'url' - url: string - } +import type { ReactionTarget } from '../ReactionTarget//types.js' +import type { ReactionType } from '../ReactionType//types.js' export type Reaction = { meta: Meta diff --git a/src/Node/Internal/ReactionAdd/toHex.ts b/src/Node/Internal/ReactionAdd/toHex.ts new file mode 100644 index 0000000..2c81613 --- /dev/null +++ b/src/Node/Internal/ReactionAdd/toHex.ts @@ -0,0 +1,26 @@ +import { toBinary } from '@bufbuild/protobuf' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as Protobuf from '../Protobufs/message_pb.js' +import type { Reaction } from '../Reaction/types.js' +import { ReactionAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function ReactionAdd_toHex( + cast: ReactionAdd_toHex.ParametersType, +): ReactionAdd_toHex.ReturnType { + return Hex.fromBytes( + toBinary( + Protobuf.MessageDataSchema, + ReactionAdd_toMessageDataProtobuf(cast), + ), + ) +} + +export declare namespace ReactionAdd_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +ReactionAdd_toHex.parseError = (error: unknown) => + error as ReactionAdd_toHex.ErrorType diff --git a/src/Node/Internal/ReactionAdd/toMessageDataProtobuf.ts b/src/Node/Internal/ReactionAdd/toMessageDataProtobuf.ts new file mode 100644 index 0000000..dfe111d --- /dev/null +++ b/src/Node/Internal/ReactionAdd/toMessageDataProtobuf.ts @@ -0,0 +1,31 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { Reaction_toProtobuf } from '../Reaction/toProtobuf.js' +import type { Reaction } from '../Reaction/types.js' + +export function ReactionAdd_toMessageDataProtobuf( + cast: ReactionAdd_toMessageDataProtobuf.ParametersType, +): ReactionAdd_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: cast.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.REACTION_ADD, + fid: cast.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'reactionBody', + value: Reaction_toProtobuf(cast), + }, + }) +} + +export declare namespace ReactionAdd_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +ReactionAdd_toMessageDataProtobuf.parseError = (error: unknown) => + error as ReactionAdd_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/ReactionAdd/toMessageProtobuf.ts b/src/Node/Internal/ReactionAdd/toMessageProtobuf.ts new file mode 100644 index 0000000..4ec434f --- /dev/null +++ b/src/Node/Internal/ReactionAdd/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Reaction } from '../Reaction/types.js' +import { ReactionAdd_toHex } from './toHex.js' +import { ReactionAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function ReactionAdd_toMessageProtobuf( + parameters: ReactionAdd_toMessageProtobuf.ParametersType, +): ReactionAdd_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: ReactionAdd_toHex(parameters.reaction), + privateKey: parameters.privateKey, + }), + ), + data: ReactionAdd_toMessageDataProtobuf(parameters.reaction), + }) +} + +export declare namespace ReactionAdd_toMessageProtobuf { + type ParametersType = { + reaction: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +ReactionAdd_toMessageProtobuf.parseError = (error: unknown) => + error as ReactionAdd_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/ReactionRemove/toHex.ts b/src/Node/Internal/ReactionRemove/toHex.ts new file mode 100644 index 0000000..7fd13bd --- /dev/null +++ b/src/Node/Internal/ReactionRemove/toHex.ts @@ -0,0 +1,26 @@ +import { toBinary } from '@bufbuild/protobuf' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as Protobuf from '../Protobufs/message_pb.js' +import type { Reaction } from '../Reaction/types.js' +import { ReactionRemove_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function ReactionRemove_toHex( + cast: ReactionRemove_toHex.ParametersType, +): ReactionRemove_toHex.ReturnType { + return Hex.fromBytes( + toBinary( + Protobuf.MessageDataSchema, + ReactionRemove_toMessageDataProtobuf(cast), + ), + ) +} + +export declare namespace ReactionRemove_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +ReactionRemove_toHex.parseError = (error: unknown) => + error as ReactionRemove_toHex.ErrorType diff --git a/src/Node/Internal/ReactionRemove/toMessageDataProtobuf.ts b/src/Node/Internal/ReactionRemove/toMessageDataProtobuf.ts new file mode 100644 index 0000000..e13f846 --- /dev/null +++ b/src/Node/Internal/ReactionRemove/toMessageDataProtobuf.ts @@ -0,0 +1,31 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { Reaction_toProtobuf } from '../Reaction/toProtobuf.js' +import type { Reaction } from '../Reaction/types.js' + +export function ReactionRemove_toMessageDataProtobuf( + cast: ReactionRemove_toMessageDataProtobuf.ParametersType, +): ReactionRemove_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: cast.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.REACTION_REMOVE, + fid: cast.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'reactionBody', + value: Reaction_toProtobuf(cast), + }, + }) +} + +export declare namespace ReactionRemove_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +ReactionRemove_toMessageDataProtobuf.parseError = (error: unknown) => + error as ReactionRemove_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/ReactionRemove/toMessageProtobuf.ts b/src/Node/Internal/ReactionRemove/toMessageProtobuf.ts new file mode 100644 index 0000000..d3fa33e --- /dev/null +++ b/src/Node/Internal/ReactionRemove/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Reaction } from '../Reaction/types.js' +import { ReactionRemove_toHex } from './toHex.js' +import { ReactionRemove_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function ReactionRemove_toMessageProtobuf( + parameters: ReactionRemove_toMessageProtobuf.ParametersType, +): ReactionRemove_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: ReactionRemove_toHex(parameters.reaction), + privateKey: parameters.privateKey, + }), + ), + data: ReactionRemove_toMessageDataProtobuf(parameters.reaction), + }) +} + +export declare namespace ReactionRemove_toMessageProtobuf { + type ParametersType = { + reaction: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +ReactionRemove_toMessageProtobuf.parseError = (error: unknown) => + error as ReactionRemove_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/ReactionTarget/fromProtobuf.ts b/src/Node/Internal/ReactionTarget/fromProtobuf.ts new file mode 100644 index 0000000..6e9096f --- /dev/null +++ b/src/Node/Internal/ReactionTarget/fromProtobuf.ts @@ -0,0 +1,27 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { CastId_fromProtobuf } from '../CastId/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { ReactionTarget } from './types.js' + +export function ReactionTarget_fromProtobuf( + target: MessageProtobuf.ReactionBody['target'], +): ReactionTarget_fromProtobuf.ReturnType { + return target.case === 'targetUrl' + ? { + type: 'url', + url: target.value, + } + : { + type: 'cast', + ...CastId_fromProtobuf(target.value!), + } +} + +export declare namespace ReactionTarget_fromProtobuf { + type ReturnType = ReactionTarget + + type ErrorType = GlobalErrorType +} + +ReactionTarget_fromProtobuf.parseError = (error: unknown) => + error as ReactionTarget_fromProtobuf.ErrorType diff --git a/src/Node/Internal/ReactionTarget/toProtobuf.ts b/src/Node/Internal/ReactionTarget/toProtobuf.ts new file mode 100644 index 0000000..b332ed0 --- /dev/null +++ b/src/Node/Internal/ReactionTarget/toProtobuf.ts @@ -0,0 +1,22 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { CastId_toProtobuf } from '../CastId/toProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { ReactionTarget } from './types.js' + +export function ReactionTarget_toProtobuf( + target: ReactionTarget_toProtobuf.ParametersType, +): ReactionTarget_toProtobuf.ReturnType { + return target.type === 'cast' + ? { value: CastId_toProtobuf(target), case: 'targetCastId' } + : { value: target.url, case: 'targetUrl' } +} + +export declare namespace ReactionTarget_toProtobuf { + type ParametersType = ReactionTarget + type ReturnType = MessageProtobuf.ReactionBody['target'] + + type ErrorTarget = GlobalErrorType +} + +ReactionTarget_toProtobuf.parseError = (error: unknown) => + error as ReactionTarget_toProtobuf.ErrorTarget diff --git a/src/Node/Internal/ReactionTarget/types.ts b/src/Node/Internal/ReactionTarget/types.ts new file mode 100644 index 0000000..4ee6ba4 --- /dev/null +++ b/src/Node/Internal/ReactionTarget/types.ts @@ -0,0 +1,10 @@ +import type { CastId } from '../CastId/types.js' + +export type ReactionTarget = + | ({ + type: 'cast' + } & CastId) + | { + type: 'url' + url: string + } diff --git a/src/Node/Internal/ReactionType/fromProtobuf.ts b/src/Node/Internal/ReactionType/fromProtobuf.ts new file mode 100644 index 0000000..7341da5 --- /dev/null +++ b/src/Node/Internal/ReactionType/fromProtobuf.ts @@ -0,0 +1,20 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { ReactionType } from './types.js' + +export function ReactionType_fromProtobuf( + reactionType: ReactionType_fromProtobuf.ParametersType, +): ReactionType_fromProtobuf.ReturnType { + return reactionType === MessageProtobuf.ReactionType.RECAST + ? ('recast' as const) + : ('like' as const) +} + +export declare namespace ReactionType_fromProtobuf { + type ParametersType = MessageProtobuf.ReactionType + type ReturnType = ReactionType + type ErrorType = GlobalErrorType +} + +ReactionType_fromProtobuf.parseError = (error: unknown) => + error as ReactionType_fromProtobuf.ErrorType diff --git a/src/Node/Internal/ReactionType/toProtobuf.ts b/src/Node/Internal/ReactionType/toProtobuf.ts new file mode 100644 index 0000000..fa8d3e1 --- /dev/null +++ b/src/Node/Internal/ReactionType/toProtobuf.ts @@ -0,0 +1,22 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { ReactionType } from './types.js' + +export function ReactionType_toProtobuf( + type: ReactionType_toProtobuf.ParametersType, +): ReactionType_toProtobuf.ReturnType { + if (type === 'like') return MessageProtobuf.ReactionType.LIKE + if (type === 'recast') return MessageProtobuf.ReactionType.RECAST + // @TODO: error + throw new Error('unknwo tyep') +} + +export declare namespace ReactionType_toProtobuf { + type ParametersType = ReactionType + type ReturnType = MessageProtobuf.ReactionType + + type ErrorType = GlobalErrorType +} + +ReactionType_toProtobuf.parseError = (error: unknown) => + error as ReactionType_toProtobuf.ErrorType diff --git a/src/Node/Internal/ReactionType/types.ts b/src/Node/Internal/ReactionType/types.ts new file mode 100644 index 0000000..dd94123 --- /dev/null +++ b/src/Node/Internal/ReactionType/types.ts @@ -0,0 +1 @@ +export type ReactionType = 'like' | 'recast' diff --git a/src/Node/Internal/UserData/fromMessage.ts b/src/Node/Internal/UserData/fromMessage.ts deleted file mode 100644 index 29dbfc2..0000000 --- a/src/Node/Internal/UserData/fromMessage.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Hex } from 'ox' -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { Meta_fromMessage } from '../Meta/fromMessage.js' -import { type Message, UserDataType } from '../Protobufs/message_pb.js' -import { UserData_InvalidMessageTypeError } from './errors.js' -import type { UserData } from './types.js' - -export function UserData_fromMessage( - message: Message, -): UserData_fromMessage.ReturnType { - // @TODO: error here - if (message.data?.body.case !== 'userDataBody') - throw new UserData_InvalidMessageTypeError({ - hash: Hex.fromBytes(message.hash), - }) - const type = (() => { - if (message.data.body.value.type === UserDataType.PFP) { - return 'pfp' - } - if (message.data.body.value.type === UserDataType.BIO) { - return 'bio' - } - if (message.data.body.value.type === UserDataType.DISPLAY) { - return 'display' - } - if (message.data.body.value.type === UserDataType.URL) { - return 'url' - } - if (message.data.body.value.type === UserDataType.USERNAME) { - return 'username' - } - if (message.data.body.value.type === UserDataType.LOCATION) { - return 'location' - } - return 'none' - })() - // @TODO: handle unexpeded type here? - return { - meta: Meta_fromMessage(message), - type, - value: message.data.body.value.value, - } -} - -export declare namespace UserData_fromMessage { - type ReturnType = UserData - - type ErrorType = UserData_InvalidMessageTypeError | GlobalErrorType -} - -UserData_fromMessage.parseError = (error: unknown) => - error as UserData_fromMessage.ErrorType diff --git a/src/Node/Internal/UserData/fromProtobuf.ts b/src/Node/Internal/UserData/fromProtobuf.ts new file mode 100644 index 0000000..f79460e --- /dev/null +++ b/src/Node/Internal/UserData/fromProtobuf.ts @@ -0,0 +1,57 @@ +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { UserData_InvalidMessageTypeError } from './errors.js' +import type { UserData } from './types.js' + +export function UserData_fromProtobuf( + message: UserData_fromProtobuf.ParametersType, +): UserData_fromProtobuf.ReturnType { + // @TODO: error here + if (message.data?.body.case !== 'userDataBody') + throw new UserData_InvalidMessageTypeError({ + hash: Hex.fromBytes(message.hash), + }) + const type = (() => { + if (message.data.body.value.type === MessageProtobuf.UserDataType.PFP) { + return 'pfp' + } + if (message.data.body.value.type === MessageProtobuf.UserDataType.BIO) { + return 'bio' + } + if (message.data.body.value.type === MessageProtobuf.UserDataType.DISPLAY) { + return 'display' + } + if (message.data.body.value.type === MessageProtobuf.UserDataType.URL) { + return 'url' + } + if ( + message.data.body.value.type === MessageProtobuf.UserDataType.USERNAME + ) { + return 'username' + } + if ( + message.data.body.value.type === MessageProtobuf.UserDataType.LOCATION + ) { + return 'location' + } + return 'none' + })() + // @TODO: handle unexpeded type here? + return { + meta: Meta_fromProtobuf(message), + type, + value: message.data.body.value.value, + } +} + +export declare namespace UserData_fromProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = UserData + + type ErrorType = UserData_InvalidMessageTypeError | GlobalErrorType +} + +UserData_fromProtobuf.parseError = (error: unknown) => + error as UserData_fromProtobuf.ErrorType diff --git a/src/Node/Internal/UsernameProof/fromMessage.ts b/src/Node/Internal/UsernameProof/fromMessage.ts deleted file mode 100644 index 0c44da4..0000000 --- a/src/Node/Internal/UsernameProof/fromMessage.ts +++ /dev/null @@ -1,35 +0,0 @@ -import { Hex } from 'ox' -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { Meta_fromMessage } from '../Meta/fromMessage.js' -import type { Message } from '../Protobufs/message_pb.js' -import { UserNameType } from '../Protobufs/username_proof_pb.js' -import type { UsernameProof } from './types.js' - -export function UsernameProof_fromMessage( - message: Message, -): UsernameProof_fromMessage.ReturnType { - // @TODO: error here - if (message.data?.body.case !== 'usernameProofBody') - throw new Error('invalid type') - return { - meta: Meta_fromMessage(message), - type: - message.data.body.value.type === UserNameType.USERNAME_TYPE_FNAME - ? 'fname' - : 'ensL1', - name: message.data.body.value.name.toString(), - owner: message.data.body.value.owner.toString(), - signature: Hex.fromBytes(message.data.body.value.signature), - timestamp: message.data.body.value.timestamp, - fid: message.data.body.value.fid, - } -} - -export declare namespace UsernameProof_fromMessage { - type ReturnType = UsernameProof - - type ErrorType = GlobalErrorType -} - -UsernameProof_fromMessage.parseError = (error: unknown) => - error as UsernameProof_fromMessage.ErrorType diff --git a/src/Node/Internal/UsernameProof/fromProtobuf.ts b/src/Node/Internal/UsernameProof/fromProtobuf.ts new file mode 100644 index 0000000..28a7e4a --- /dev/null +++ b/src/Node/Internal/UsernameProof/fromProtobuf.ts @@ -0,0 +1,36 @@ +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import * as UsernameProofProtobuf from '../Protobufs/username_proof_pb.js' +import type { UsernameProof } from './types.js' + +export function UsernameProof_fromProtobuf( + message: UsernameProof_fromProtobuf.ParametersType, +): UsernameProof_fromProtobuf.ReturnType { + // @TODO: error here + if (message.data?.body.case !== 'usernameProofBody') + throw new Error('invalid type') + return { + meta: Meta_fromProtobuf(message), + type: + message.data.body.value.type === + UsernameProofProtobuf.UserNameType.USERNAME_TYPE_FNAME + ? 'fname' + : 'ensL1', + name: message.data.body.value.name.toString(), + owner: message.data.body.value.owner.toString(), + signature: Hex.fromBytes(message.data.body.value.signature), + timestamp: message.data.body.value.timestamp, + fid: message.data.body.value.fid, + } +} + +export declare namespace UsernameProof_fromProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = UsernameProof + type ErrorType = GlobalErrorType +} + +UsernameProof_fromProtobuf.parseError = (error: unknown) => + error as UsernameProof_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Verification/fromMessage.ts b/src/Node/Internal/Verification/fromMessage.ts deleted file mode 100644 index 28ce668..0000000 --- a/src/Node/Internal/Verification/fromMessage.ts +++ /dev/null @@ -1,39 +0,0 @@ -import { Hex } from 'ox' -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { Meta_fromMessage } from '../Meta/fromMessage.js' -import { - type Message as Protobufs_Message, - Protocol, -} from '../Protobufs/message_pb.js' -import type { Verification } from './types.js' - -export function Verification_fromMessage( - message: Protobufs_Message, -): Verification_fromMessage.ReturnType { - //@TODO: errors - if (!message.data) throw new Error('no data') - if (message.data.body.case !== 'verificationAddAddressBody') - throw new Error('bad case') - - return { - meta: Meta_fromMessage(message), - type: message.data.body.value.verificationType === 0 ? 'eoa' : 'contract', - address: message.data.body.value.address.toString(), - claimSignature: Hex.fromBytes(message.data.body.value.claimSignature), - blockHash: Hex.fromBytes(message.data.body.value.blockHash), - chainId: message.data.body.value.chainId, - protocol: - message.data.body.value.protocol === Protocol.SOLANA - ? 'solana' - : 'ethereum', - } as const -} - -export declare namespace Verification_fromMessage { - type ReturnType = Verification - - type ErrorType = GlobalErrorType -} - -Verification_fromMessage.parseError = (error: unknown) => - error as Verification_fromMessage.ErrorType diff --git a/src/Node/Internal/Verification/fromProtobuf.ts b/src/Node/Internal/Verification/fromProtobuf.ts new file mode 100644 index 0000000..b07437c --- /dev/null +++ b/src/Node/Internal/Verification/fromProtobuf.ts @@ -0,0 +1,39 @@ +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import type { Meta } from '../Meta/types.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Verification } from './types.js' + +export function Verification_fromProtobuf( + parameters: Verification_fromProtobuf.ParametersType, +): Verification_fromProtobuf.ReturnType { + return { + meta: parameters.meta, + fid: parameters.fid, + timestamp: parameters.timestamp, + type: parameters.body.verificationType === 0 ? 'eoa' : 'contract', + address: parameters.body.address.toString(), + claimSignature: Hex.fromBytes(parameters.body.claimSignature), + blockHash: Hex.fromBytes(parameters.body.blockHash), + chainId: parameters.body.chainId, + protocol: + parameters.body.protocol === MessageProtobuf.Protocol.SOLANA + ? 'solana' + : 'ethereum', + } as const +} + +export declare namespace Verification_fromProtobuf { + type ParametersType = { + body: MessageProtobuf.VerificationAddAddressBody + meta: Meta + fid: bigint + timestamp: number + } + type ReturnType = Verification + + type ErrorType = GlobalErrorType +} + +Verification_fromProtobuf.parseError = (error: unknown) => + error as Verification_fromProtobuf.ErrorType diff --git a/src/Node/Internal/Verification/toProtobuf.ts b/src/Node/Internal/Verification/toProtobuf.ts new file mode 100644 index 0000000..47fc9c9 --- /dev/null +++ b/src/Node/Internal/Verification/toProtobuf.ts @@ -0,0 +1,31 @@ +import { create } from '@bufbuild/protobuf' +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Verification } from './types.js' + +export function Verification_toProtobuf( + verification: Verification_toProtobuf.ParametersType, +): Verification_toProtobuf.ReturnType { + return create(MessageProtobuf.VerificationAddAddressBodySchema, { + verificationType: verification.type === 'eoa' ? 0 : 1, + address: Hex.toBytes(Hex.fromString(verification.address)), + claimSignature: Hex.toBytes(verification.claimSignature), + blockHash: Hex.toBytes(verification.blockHash), + chainId: verification.chainId, + protocol: + verification.protocol === 'ethereum' + ? MessageProtobuf.Protocol.ETHEREUM + : MessageProtobuf.Protocol.SOLANA, + }) +} + +export declare namespace Verification_toProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.VerificationAddAddressBody + + type ErrorType = GlobalErrorType +} + +Verification_toProtobuf.parseError = (error: unknown) => + error as Verification_toProtobuf.ErrorType diff --git a/src/Node/Internal/Verification/types.ts b/src/Node/Internal/Verification/types.ts index 406de31..b8749e8 100644 --- a/src/Node/Internal/Verification/types.ts +++ b/src/Node/Internal/Verification/types.ts @@ -9,4 +9,6 @@ export type Verification = { type: 'eoa' | 'contract' chainId: number protocol: 'ethereum' | 'solana' + fid: bigint + timestamp: number } diff --git a/src/Node/Internal/VerificationAdd/errors.ts b/src/Node/Internal/VerificationAdd/errors.ts new file mode 100644 index 0000000..fd14396 --- /dev/null +++ b/src/Node/Internal/VerificationAdd/errors.ts @@ -0,0 +1,12 @@ +import { BaseError } from 'ox/Errors' + +// @TODO: replace by our own BaseError +export class VerificationAdd_InvalidMessageTypeError extends BaseError { + override readonly name = 'VerificationAdd.InvalidMessageTypeError' + + constructor({ hash }: { hash: string }) { + super(`Message "${hash}" has type other than CAST_ADD.`, { + docsPath: '/errors#invalidmessagetypeerror', + }) + } +} diff --git a/src/Node/Internal/VerificationAdd/fromMessageProtobuf.ts b/src/Node/Internal/VerificationAdd/fromMessageProtobuf.ts new file mode 100644 index 0000000..05f42d3 --- /dev/null +++ b/src/Node/Internal/VerificationAdd/fromMessageProtobuf.ts @@ -0,0 +1,35 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import type * as MessageProtobuf from '../Protobufs/message_pb.js' +import { Verification_fromProtobuf } from '../Verification/fromProtobuf.js' +import type { Verification } from '../Verification/types.js' +import { VerificationAdd_InvalidMessageTypeError } from './errors.js' + +export function VerificationAdd_fromMessageProtobuf( + message: VerificationAdd_fromMessageProtobuf.ParametersType, +): VerificationAdd_fromMessageProtobuf.ReturnType { + const meta = Meta_fromProtobuf(message) + // @TODO: separate error here + if (!message.data) + throw new Error('`data` must be defined in VerificationAdd message.') + if (message.data.body.case !== 'verificationAddAddressBody') + throw new VerificationAdd_InvalidMessageTypeError({ hash: meta.hash }) + return Verification_fromProtobuf({ + body: message.data.body.value, + meta, + fid: message.data.fid, + timestamp: message.data.timestamp, + }) +} + +export declare namespace VerificationAdd_fromMessageProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = Verification + type ErrorType = + | VerificationAdd_InvalidMessageTypeError + | Meta_fromProtobuf.ErrorType + | GlobalErrorType +} + +VerificationAdd_fromMessageProtobuf.parseError = (error: unknown) => + error as VerificationAdd_fromMessageProtobuf.ErrorType diff --git a/src/Node/Internal/VerificationAdd/toHex.ts b/src/Node/Internal/VerificationAdd/toHex.ts new file mode 100644 index 0000000..7550980 --- /dev/null +++ b/src/Node/Internal/VerificationAdd/toHex.ts @@ -0,0 +1,26 @@ +import { toBinary } from '@bufbuild/protobuf' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as Protobuf from '../Protobufs/message_pb.js' +import type { Verification } from '../Verification/types.js' +import { VerificationAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function VerificationAdd_toHex( + cast: VerificationAdd_toHex.ParametersType, +): VerificationAdd_toHex.ReturnType { + return Hex.fromBytes( + toBinary( + Protobuf.MessageDataSchema, + VerificationAdd_toMessageDataProtobuf(cast), + ), + ) +} + +export declare namespace VerificationAdd_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +VerificationAdd_toHex.parseError = (error: unknown) => + error as VerificationAdd_toHex.ErrorType diff --git a/src/Node/Internal/VerificationAdd/toMessageDataProtobuf.ts b/src/Node/Internal/VerificationAdd/toMessageDataProtobuf.ts new file mode 100644 index 0000000..aff06fc --- /dev/null +++ b/src/Node/Internal/VerificationAdd/toMessageDataProtobuf.ts @@ -0,0 +1,31 @@ +import { create } from '@bufbuild/protobuf' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { Verification_toProtobuf } from '../Verification/toProtobuf.js' +import type { Verification } from '../Verification/types.js' + +export function VerificationAdd_toMessageDataProtobuf( + verification: VerificationAdd_toMessageDataProtobuf.ParametersType, +): VerificationAdd_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: verification.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.VERIFICATION_ADD_ETH_ADDRESS, + fid: verification.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'verificationAddAddressBody', + value: Verification_toProtobuf(verification), + }, + }) +} + +export declare namespace VerificationAdd_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +VerificationAdd_toMessageDataProtobuf.parseError = (error: unknown) => + error as VerificationAdd_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/VerificationAdd/toMessageProtobuf.ts b/src/Node/Internal/VerificationAdd/toMessageProtobuf.ts new file mode 100644 index 0000000..60365e3 --- /dev/null +++ b/src/Node/Internal/VerificationAdd/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { Verification } from '../Verification/types.js' +import { VerificationAdd_toHex } from './toHex.js' +import { VerificationAdd_toMessageDataProtobuf } from './toMessageDataProtobuf.js' + +export function VerificationAdd_toMessageProtobuf( + parameters: VerificationAdd_toMessageProtobuf.ParametersType, +): VerificationAdd_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: VerificationAdd_toHex(parameters.verification), + privateKey: parameters.privateKey, + }), + ), + data: VerificationAdd_toMessageDataProtobuf(parameters.verification), + }) +} + +export declare namespace VerificationAdd_toMessageProtobuf { + type ParametersType = { + verification: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +VerificationAdd_toMessageProtobuf.parseError = (error: unknown) => + error as VerificationAdd_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/VerificationRemoval/fromMessage.ts b/src/Node/Internal/VerificationRemoval/fromMessage.ts deleted file mode 100644 index a05663c..0000000 --- a/src/Node/Internal/VerificationRemoval/fromMessage.ts +++ /dev/null @@ -1,32 +0,0 @@ -import type { GlobalErrorType } from '../../../Internal/Errors/error.js' -import { - type Message as Protobufs_Message, - Protocol, -} from '../Protobufs/message_pb.js' -import type { VerificationRemoval } from './types.js' - -export function VerificationRemoval_fromMessage( - message: Protobufs_Message, -): VerificationRemoval_fromMessage.ReturnType { - //@TODO: errors - if (!message.data) throw new Error('no data') - if (message.data.body.case !== 'verificationRemoveBody') - throw new Error('bad case') - - return { - address: message.data.body.value.address.toString(), - protocol: - message.data.body.value.protocol === Protocol.SOLANA - ? 'solana' - : 'ethereum', - } as const -} - -export declare namespace VerificationRemoval_fromMessage { - type ReturnType = VerificationRemoval - - type ErrorType = GlobalErrorType -} - -VerificationRemoval_fromMessage.parseError = (error: unknown) => - error as VerificationRemoval_fromMessage.ErrorType diff --git a/src/Node/Internal/VerificationRemoval/fromMessageProtobuf.ts b/src/Node/Internal/VerificationRemoval/fromMessageProtobuf.ts new file mode 100644 index 0000000..cec390b --- /dev/null +++ b/src/Node/Internal/VerificationRemoval/fromMessageProtobuf.ts @@ -0,0 +1,33 @@ +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_fromProtobuf } from '../Meta/fromProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { VerificationRemove } from './types.js' + +export function VerificationRemove_fromMessageProtobuf( + message: VerificationRemove_fromMessageProtobuf.ParametersType, +): VerificationRemove_fromMessageProtobuf.ReturnType { + //@TODO: errors + if (!message.data) throw new Error('no data') + if (message.data.body.case !== 'verificationRemoveBody') + throw new Error('bad case') + + return { + fid: message.data.fid, + timestamp: message.data.timestamp, + meta: Meta_fromProtobuf(message), + address: message.data.body.value.address.toString(), + protocol: + message.data.body.value.protocol === MessageProtobuf.Protocol.SOLANA + ? 'solana' + : 'ethereum', + } as const +} + +export declare namespace VerificationRemove_fromMessageProtobuf { + type ParametersType = MessageProtobuf.Message + type ReturnType = VerificationRemove + type ErrorType = GlobalErrorType +} + +VerificationRemove_fromMessageProtobuf.parseError = (error: unknown) => + error as VerificationRemove_fromMessageProtobuf.ErrorType diff --git a/src/Node/Internal/VerificationRemoval/toHex.ts b/src/Node/Internal/VerificationRemoval/toHex.ts new file mode 100644 index 0000000..28d4fd9 --- /dev/null +++ b/src/Node/Internal/VerificationRemoval/toHex.ts @@ -0,0 +1,26 @@ +import { toBinary } from '@bufbuild/protobuf' +import { Hex, type Types } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import * as Protobuf from '../Protobufs/message_pb.js' +import { VerificationRemove_toMessageDataProtobuf } from './toMessageDataProtobuf.js' +import type { VerificationRemove } from './types.js' + +export function VerificationRemove_toHex( + cast: VerificationRemove_toHex.ParametersType, +): VerificationRemove_toHex.ReturnType { + return Hex.fromBytes( + toBinary( + Protobuf.MessageDataSchema, + VerificationRemove_toMessageDataProtobuf(cast), + ), + ) +} + +export declare namespace VerificationRemove_toHex { + type ParametersType = Omit + type ReturnType = Types.Hex + type ErrorType = GlobalErrorType +} + +VerificationRemove_toHex.parseError = (error: unknown) => + error as VerificationRemove_toHex.ErrorType diff --git a/src/Node/Internal/VerificationRemoval/toMessageDataProtobuf.ts b/src/Node/Internal/VerificationRemoval/toMessageDataProtobuf.ts new file mode 100644 index 0000000..31ecb2f --- /dev/null +++ b/src/Node/Internal/VerificationRemoval/toMessageDataProtobuf.ts @@ -0,0 +1,37 @@ +import { create } from '@bufbuild/protobuf' +import { Hex } from 'ox' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { FARCASTER_EPOCH_TIMESTAMP } from '../Constants.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import type { VerificationRemove } from './types.js' + +export function VerificationRemove_toMessageDataProtobuf( + verificationRemove: VerificationRemove_toMessageDataProtobuf.ParametersType, +): VerificationRemove_toMessageDataProtobuf.ReturnType { + return create(MessageProtobuf.MessageDataSchema, { + timestamp: verificationRemove.timestamp - FARCASTER_EPOCH_TIMESTAMP, + type: MessageProtobuf.MessageType.VERIFICATION_REMOVE, + fid: verificationRemove.fid, + network: MessageProtobuf.FarcasterNetwork.MAINNET, + body: { + case: 'verificationRemoveBody', + value: create(MessageProtobuf.VerificationRemoveBodySchema, { + address: Hex.toBytes(Hex.fromString(verificationRemove.address)), + protocol: + verificationRemove.protocol === 'solana' + ? MessageProtobuf.Protocol.SOLANA + : MessageProtobuf.Protocol.ETHEREUM, + }), + }, + }) +} + +export declare namespace VerificationRemove_toMessageDataProtobuf { + type ParametersType = Omit + type ReturnType = MessageProtobuf.MessageData + + type ErrorType = GlobalErrorType +} + +VerificationRemove_toMessageDataProtobuf.parseError = (error: unknown) => + error as VerificationRemove_toMessageDataProtobuf.ErrorType diff --git a/src/Node/Internal/VerificationRemoval/toMessageProtobuf.ts b/src/Node/Internal/VerificationRemoval/toMessageProtobuf.ts new file mode 100644 index 0000000..116ef5b --- /dev/null +++ b/src/Node/Internal/VerificationRemoval/toMessageProtobuf.ts @@ -0,0 +1,38 @@ +import { create } from '@bufbuild/protobuf' +import type { Types } from 'ox' +import type { Omit } from 'ox/Internal' +import type { GlobalErrorType } from '../../../Internal/Errors/error.js' +import { Meta_create } from '../Meta/create.js' +import { Meta_toProtobuf } from '../Meta/toProtobuf.js' +import * as MessageProtobuf from '../Protobufs/message_pb.js' +import { VerificationRemove_toHex } from './toHex.js' +import { VerificationRemove_toMessageDataProtobuf } from './toMessageDataProtobuf.js' +import type { VerificationRemove } from './types.js' + +export function VerificationRemove_toMessageProtobuf( + parameters: VerificationRemove_toMessageProtobuf.ParametersType, +): VerificationRemove_toMessageProtobuf.ReturnType { + return create(MessageProtobuf.MessageSchema, { + ...Meta_toProtobuf( + Meta_create({ + dataBytes: VerificationRemove_toHex(parameters.castRemove), + privateKey: parameters.privateKey, + }), + ), + data: VerificationRemove_toMessageDataProtobuf(parameters.castRemove), + }) +} + +export declare namespace VerificationRemove_toMessageProtobuf { + type ParametersType = { + castRemove: Omit + privateKey: Types.Hex + } + type ReturnType = MessageProtobuf.Message + + // @TODO: errors + type ErrorType = GlobalErrorType +} + +VerificationRemove_toMessageProtobuf.parseError = (error: unknown) => + error as VerificationRemove_toMessageProtobuf.ErrorType diff --git a/src/Node/Internal/VerificationRemoval/types.ts b/src/Node/Internal/VerificationRemoval/types.ts index 03a996f..b3eeece 100644 --- a/src/Node/Internal/VerificationRemoval/types.ts +++ b/src/Node/Internal/VerificationRemoval/types.ts @@ -1,4 +1,9 @@ -export type VerificationRemoval = { +import type { Meta } from '../Meta/types.js' + +export type VerificationRemove = { + meta: Meta address: string protocol: 'ethereum' | 'solana' + fid: bigint + timestamp: number } diff --git a/src/Node/Types.ts b/src/Node/Types.ts index 8fcc66b..dc9ade3 100644 --- a/src/Node/Types.ts +++ b/src/Node/Types.ts @@ -2,5 +2,6 @@ export type { Cast } from './Internal/Cast/types.js' export type { Embed } from './Internal/Embed/types.js' export type { Parent } from './Internal/Parent/types.js' export type { Meta } from './Internal/Meta/types.js' +export type { Message } from './Internal/Message/types.js' export type { UserData } from './Internal/UserData/types.js' export type { Reaction } from './Internal/Reaction/types.js' diff --git a/src/Node/index.ts b/src/Node/index.ts index 678a223..7d0cb3b 100644 --- a/src/Node/index.ts +++ b/src/Node/index.ts @@ -1,2 +1,3 @@ export * as Actions from './Actions.js' export * as Types from './Types.js' +export * as Constants from './Constants.js' diff --git a/src/package.json b/src/package.json index 1dd0773..b03aaf6 100644 --- a/src/package.json +++ b/src/package.json @@ -31,10 +31,11 @@ } }, "dependencies": { - "@bufbuild/protobuf": "^2.2.0", + "@bufbuild/protobuf": "^2.2.2", "@connectrpc/connect": "beta", "@connectrpc/connect-node": "beta", "@connectrpc/connect-web": "beta", + "@farcaster/core": "^0.15.6", "@noble/ed25519": "^2.1.0", "@noble/hashes": "^1.5.0", "ox": "0.0.0-canary-20241006052626"