Skip to content

Commit

Permalink
feat(lib): add testing ci workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcnk committed Aug 2, 2024
1 parent 9618882 commit 02fed52
Show file tree
Hide file tree
Showing 9 changed files with 181 additions and 23 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -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
Binary file modified bun.lockb
Binary file not shown.
32 changes: 32 additions & 0 deletions packages/accounts/src/private-key-to-account.ts
Original file line number Diff line number Diff line change
@@ -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;
// }
15 changes: 15 additions & 0 deletions packages/accounts/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type CustomSource = {
publicKey: string;
};

export type LocalAccount<source extends string = string> = CustomSource & {
publicKey: string;
source: source;
type: "local";
};

// export type PrivateKeyAccount = Prettify<
// LocalAccount<'privateKey'> & {
// // TODO(v3): This will be redundant.
// sign: NonNullable<CustomSource['sign']>
// }
35 changes: 12 additions & 23 deletions packages/providers/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
import type {
Nullifier,
PublicKey,
SignedFields,
SignedMessage,
TransactionReceipt,
} from "@mina-js/shared";
import type {
AddChainData,
CreateNullifierData,
Expand All @@ -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;
Expand Down Expand Up @@ -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<Address[]>;
}) => Promise<PublicKey[]>;

export type ChainIdRequest = (args: {
method: "mina_chainId";
Expand All @@ -91,7 +80,7 @@ export type SignRequest = (args: {
export type SignFieldsRequest = (args: {
method: "mina_signFields";
params: SignFieldsData;
}) => Promise<SignedFieldsData>;
}) => Promise<SignedFields>;

export type SignTransactionRequest = (args: {
method: "mina_signTransaction";
Expand All @@ -101,12 +90,12 @@ export type SignTransactionRequest = (args: {
export type SendTransactionRequest = (args: {
method: "mina_sendTransaction";
params: SendTransactionData;
}) => Promise<TODO>;
}) => Promise<TransactionReceipt>;

export type CreateNullifierRequest = (args: {
method: "mina_createNullifier";
params: CreateNullifierData;
}) => Promise<TODO>;
}) => Promise<Nullifier>;

export type SwitchChainRequest = (args: {
method: "mina_switchChain";
Expand Down Expand Up @@ -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 = (
Expand Down
24 changes: 24 additions & 0 deletions packages/shared/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
17 changes: 17 additions & 0 deletions packages/shared/src/index.ts
Original file line number Diff line number Diff line change
@@ -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";
18 changes: 18 additions & 0 deletions packages/shared/src/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { z } from "zod";
import type {
NullifierSchema,
PublicKeySchema,
SignedFieldsSchema,
SignedMessageSchema,
TransactionReceiptSchema,
} from "./validation";

export type PublicKey = z.infer<typeof PublicKeySchema>;

export type SignedMessage = z.infer<typeof SignedMessageSchema>;

export type SignedFields = z.infer<typeof SignedFieldsSchema>;

export type Nullifier = z.infer<typeof NullifierSchema>;

export type TransactionReceipt = z.infer<typeof TransactionReceiptSchema>;
48 changes: 48 additions & 0 deletions packages/shared/src/validation.ts
Original file line number Diff line number Diff line change
@@ -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(),
});

0 comments on commit 02fed52

Please sign in to comment.