diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..f92c5ed --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,15 @@ +name: Run tests +on: + push: + branches: + - main +jobs: + my-job: + name: my-job + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: oven-sh/setup-bun@v2 + - run: bun install + - run: bun run build + - run: bun run test diff --git a/bun.lockb b/bun.lockb index 9f2f726..b7a1fcf 100755 Binary files a/bun.lockb and b/bun.lockb differ diff --git a/packages/accounts/src/private-key-to-account.ts b/packages/accounts/src/private-key-to-account.ts new file mode 100644 index 0000000..d767681 --- /dev/null +++ b/packages/accounts/src/private-key-to-account.ts @@ -0,0 +1,32 @@ +// import MinaSigner from "mina-signer"; + +// /** +// * @description Creates an Account from a private key. +// * +// * @returns A Private Key Account. +// */ +// export function privateKeyToAccount(privateKey: string): PrivateKeyAccount { +// const client = new MinaSigner({ network: "mainnet" }); +// const publicKey = client.derivePublicKey(privateKey); +// const account = toAccount({ +// publicKey, +// async sign({ hash }) { +// return sign({ hash, privateKey, to: "hex" }); +// }, +// async signMessage({ message }) { +// return signMessage({ message, privateKey }); +// }, +// async signTransaction(transaction, { serializer } = {}) { +// return signTransaction({ privateKey, transaction, serializer }); +// }, +// async signTypedData(typedData) { +// return signTypedData({ ...typedData, privateKey }); +// }, +// }); + +// return { +// ...account, +// publicKey, +// source: "privateKey", +// } as PrivateKeyAccount; +// } diff --git a/packages/accounts/src/types.ts b/packages/accounts/src/types.ts new file mode 100644 index 0000000..ae0aada --- /dev/null +++ b/packages/accounts/src/types.ts @@ -0,0 +1,15 @@ +export type CustomSource = { + publicKey: string; +}; + +export type LocalAccount = CustomSource & { + publicKey: string; + source: source; + type: "local"; +}; + +// export type PrivateKeyAccount = Prettify< +// LocalAccount<'privateKey'> & { +// // TODO(v3): This will be redundant. +// sign: NonNullable +// } diff --git a/packages/providers/src/types.ts b/packages/providers/src/types.ts index 68f5ccc..8b880d6 100644 --- a/packages/providers/src/types.ts +++ b/packages/providers/src/types.ts @@ -1,3 +1,10 @@ +import type { + Nullifier, + PublicKey, + SignedFields, + SignedMessage, + TransactionReceipt, +} from "@mina-js/shared"; import type { AddChainData, CreateNullifierData, @@ -11,8 +18,6 @@ import type { // biome-ignore lint/suspicious/noExplicitAny: Deal with it. type TODO = any; -export type Address = `b62${string}`; - export type MinaProviderDetail = { info: MinaProviderInfo; provider: MinaProviderClient; @@ -50,26 +55,10 @@ export type ProviderRpcEvent = | "accountsChanged" | "mina_message"; -// Return types -type SignedMessage = { - publicKey: string; - data: string; - signature: { - field: string; - scalar: string; - }; -}; - -type SignedFieldsData = { - data: (string | number)[]; - publicKey: string; - signature: string; -}; - // Request variants export type AccountsRequest = (args: { method: "mina_accounts"; -}) => Promise; +}) => Promise; export type ChainIdRequest = (args: { method: "mina_chainId"; @@ -91,7 +80,7 @@ export type SignRequest = (args: { export type SignFieldsRequest = (args: { method: "mina_signFields"; params: SignFieldsData; -}) => Promise; +}) => Promise; export type SignTransactionRequest = (args: { method: "mina_signTransaction"; @@ -101,12 +90,12 @@ export type SignTransactionRequest = (args: { export type SendTransactionRequest = (args: { method: "mina_sendTransaction"; params: SendTransactionData; -}) => Promise; +}) => Promise; export type CreateNullifierRequest = (args: { method: "mina_createNullifier"; params: CreateNullifierData; -}) => Promise; +}) => Promise; export type SwitchChainRequest = (args: { method: "mina_switchChain"; @@ -148,7 +137,7 @@ export type ChainChangedListener = ( export type AccountsChangedListener = ( event: "accountsChanged", - callback: (params: { accounts: Address[] }) => void, + callback: (params: { accounts: PublicKey[] }) => void, ) => void; export type MessageListener = ( diff --git a/packages/shared/package.json b/packages/shared/package.json new file mode 100644 index 0000000..eaa1633 --- /dev/null +++ b/packages/shared/package.json @@ -0,0 +1,24 @@ +{ + "name": "@mina-js/shared", + "version": "0.0.1", + "type": "module", + "module": "dist/index.js", + "types": "dist/index.d.ts", + "exports": { + ".": { + "types": "./dist/index.d.ts", + "default": "./dist/index.cjs", + "import": "./dist/index.js" + } + }, + "scripts": { + "build": "tsup", + "test": "bun test" + }, + "dependencies": { + "zod": "3.23.8" + }, + "peerDependencies": { + "typescript": "^5.0.0" + } +} diff --git a/packages/shared/src/index.ts b/packages/shared/src/index.ts new file mode 100644 index 0000000..8d4bd9f --- /dev/null +++ b/packages/shared/src/index.ts @@ -0,0 +1,17 @@ +export type { + SignedMessage, + SignedFields, + Nullifier, + PublicKey, + TransactionReceipt, +} from "./types"; +export type { + SignatureSchema, + SignedMessageSchema, + FieldSchema, + GroupSchema, + NullifierSchema, + PublicKeySchema, + SignedFieldsSchema, + TransactionReceiptSchema, +} from "./validation"; diff --git a/packages/shared/src/types.ts b/packages/shared/src/types.ts new file mode 100644 index 0000000..64ca6a6 --- /dev/null +++ b/packages/shared/src/types.ts @@ -0,0 +1,18 @@ +import type { z } from "zod"; +import type { + NullifierSchema, + PublicKeySchema, + SignedFieldsSchema, + SignedMessageSchema, + TransactionReceiptSchema, +} from "./validation"; + +export type PublicKey = z.infer; + +export type SignedMessage = z.infer; + +export type SignedFields = z.infer; + +export type Nullifier = z.infer; + +export type TransactionReceipt = z.infer; diff --git a/packages/shared/src/validation.ts b/packages/shared/src/validation.ts new file mode 100644 index 0000000..7c96089 --- /dev/null +++ b/packages/shared/src/validation.ts @@ -0,0 +1,48 @@ +import { z } from "zod"; + +export const FieldSchema = z.coerce.bigint(); + +export const GroupSchema = z.object({ + x: FieldSchema, + y: FieldSchema, +}); + +export const PublicKeySchema = z.string().length(55).startsWith("B62"); + +export const SignatureSchema = z + .object({ + field: z.string(), + scalar: z.string(), + }) + .strict(); + +export const SignedMessageSchema = z + .object({ + publicKey: PublicKeySchema, + data: z.string(), + signature: SignatureSchema, + }) + .strict(); + +export const SignedFieldsSchema = z.object({ + data: z.array(z.number()), + publicKey: PublicKeySchema, + signature: z.string(), +}); + +export const NullifierSchema = z.object({ + publicKey: GroupSchema, + public: z.object({ + nullifier: GroupSchema, + s: FieldSchema, + }), + private: z.object({ + c: FieldSchema, + g_r: GroupSchema, + h_m_pk_r: GroupSchema, + }), +}); + +export const TransactionReceiptSchema = z.object({ + hash: z.string(), +});