Skip to content

Commit

Permalink
fix(signer): add typeguard util for aoSigner
Browse files Browse the repository at this point in the history
  • Loading branch information
atticusofsparta committed Aug 19, 2024
1 parent e7108da commit 0d7f210
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,8 @@
"axios-retry": "^4.3.0",
"eventemitter3": "^5.0.1",
"plimit-lit": "^3.0.1",
"winston": "^3.13.0"
"winston": "^3.13.0",
"zod": "^3.23.8"
},
"lint-staged": {
"**/*.{ts,js,mjs,cjs,md,json}": [
Expand Down
35 changes: 34 additions & 1 deletion src/utils/ao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
import { connect, createDataItemSigner } from '@permaweb/aoconnect';
import { createData } from 'arbundles';
import { z } from 'zod';

import { defaultArweave } from '../common/arweave.js';
import { AOProcess } from '../common/index.js';
Expand Down Expand Up @@ -157,8 +158,40 @@ export async function evolveANT({
return id;
}

export function isAoSigner(value: unknown): value is AoSigner {
const TagSchema = z.object({
name: z.string(),
value: z.union([z.string(), z.number()]),
});

const AoSignerSchema = z
.function()
.args(
z.object({
data: z.union([z.string(), z.instanceof(Buffer)]),
tags: z.array(TagSchema).optional(),
target: z.string().optional(),
anchor: z.string().optional(),
}),
)
.returns(
z.promise(
z.object({
id: z.string(),
raw: z.instanceof(ArrayBuffer),
}),
),
);
try {
AoSignerSchema.parse(value);
return true;
} catch {
return false;
}
}

export function createAoSigner(signer: ContractSigner): AoSigner {
if (typeof signer == 'function') {
if (isAoSigner(signer)) {
return signer;
}

Expand Down

0 comments on commit 0d7f210

Please sign in to comment.