Skip to content

Commit 1bc6c81

Browse files
Merge pull request #81 from labscommunity/feat--ArweaveKit-Plug-In-System
Feat arweave kit plug in system
2 parents 771f4cb + 5264143 commit 1bc6c81

15 files changed

Lines changed: 192 additions & 7 deletions

__tests__/plugin/plugin.spec.ts

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import * as pluginOne from './pluginOne';
2+
import * as pluginTwo from './pluginTwo';
3+
import { ArweaveKit } from '../../src/lib/wallet';
4+
5+
describe('Plugins', () => {
6+
it('should use plugins', () => {
7+
const arweaveKit = ArweaveKit.use({
8+
name: 'pluginOne',
9+
plugin: pluginOne,
10+
}).use({ name: 'pluginTwo', plugin: pluginTwo });
11+
12+
expect(arweaveKit.pluginOne.hello()).toBe('PluginOne');
13+
expect(arweaveKit.pluginTwo.hello()).toBe('PluginTwo');
14+
});
15+
16+
it('should throw error when same named plugins used', () => {
17+
try {
18+
const arweaveKit = ArweaveKit.use({
19+
name: 'pluginThree',
20+
plugin: pluginOne,
21+
}).use({ name: 'pluginThree', plugin: pluginTwo });
22+
23+
// if not thrown error fail the test
24+
fail('must throw error');
25+
} catch (error: any) {
26+
expect(error.message).toBe(
27+
'Plugin name already exists, please change plugin name.'
28+
);
29+
}
30+
});
31+
32+
it('should throw error when empty name used for plugin', () => {
33+
try {
34+
const arweaveKit = ArweaveKit.use({
35+
name: '',
36+
plugin: pluginOne,
37+
});
38+
39+
// if not thrown error fail the test
40+
fail('must throw error');
41+
} catch (error: any) {
42+
expect(error.message).toBe('Please provide a valid plugin name.');
43+
}
44+
});
45+
46+
it('should contain all functions with plugins', () => {
47+
const arweaveKit = ArweaveKit.use({
48+
name: 'pluginFour',
49+
plugin: pluginOne,
50+
});
51+
52+
const functionNames = [
53+
'pluginFour',
54+
'initArweave',
55+
'createWallet',
56+
'getAddress',
57+
'getBalance',
58+
];
59+
functionNames.forEach((functionName) => {
60+
expect(!!(arweaveKit as any)[functionName]).toBeTruthy();
61+
});
62+
});
63+
});

__tests__/plugin/pluginOne.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function hello(): string {
2+
return 'PluginOne';
3+
}

__tests__/plugin/pluginTwo.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function hello(): string {
2+
return 'PluginTwo';
3+
}

jestconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
"preset": "ts-jest",
33
"testMatch": ["**/*.spec.ts"],
44
"globalTeardown": "<rootDir>/__tests__/globalTeardown.ts",
5-
"globalSetup": "<rootDir>/__tests__/globalSetup.ts"
5+
"globalSetup": "<rootDir>/__tests__/globalSetup.ts",
6+
"moduleNameMapper": {
7+
"^#utils$": "<rootDir>/src/utils.ts"
8+
}
69
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arweavekit",
3-
"version": "1.4.9",
3+
"version": "1.5.0",
44
"description": "Utility library to build full stack permaweb applications",
55
"type": "module",
66
"main": "dist/index.js",
@@ -59,6 +59,9 @@
5959
"warp-contracts": "^1.4.17",
6060
"warp-contracts-plugin-deploy": "^1.0.9"
6161
},
62+
"imports": {
63+
"#utils": "./dist/utils.js"
64+
},
6265
"exports": {
6366
".": {
6467
"import": "./dist/index.js"

src/lib/auth.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
UserDetailsReturnProps,
55
} from '../types/auth';
66
import { Othent as othent } from 'othent';
7+
import { createArweaveKit } from '#utils';
78

89
/***
910
* connect to arconnect wallet
@@ -136,3 +137,19 @@ export const Othent = {
136137
logOut,
137138
userDetails,
138139
};
140+
141+
export const ArweaveKit = createArweaveKit({
142+
connect,
143+
disconnect,
144+
getActiveAddress,
145+
getPermissions,
146+
getWalletNames,
147+
getAllAddresses,
148+
getActivePublicKey,
149+
isInstalled,
150+
logIn,
151+
logOut,
152+
userDetails,
153+
ArConnect,
154+
Othent,
155+
});

src/lib/contract.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { CacheOptions, JWKInterface, Tag } from 'warp-contracts';
22
import * as Types from '../types/contract';
33
import { Othent as othent } from 'othent';
4-
import { appVersionTag } from '../utils';
4+
import { appVersionTag, createArweaveKit } from '../utils';
55
import { ethers } from 'ethers';
66

77
/**
@@ -505,3 +505,12 @@ export async function readContractWOthent(
505505

506506
return res;
507507
}
508+
509+
export const ArweaveKit = createArweaveKit({
510+
createContract,
511+
writeContract,
512+
readContractState,
513+
getContract,
514+
writeContractWOthent,
515+
readContractWOthent,
516+
});

src/lib/encryption.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { JWKInterface } from 'warp-contracts';
22
import * as Types from '../types/encryption';
3+
import { createArweaveKit } from '../utils';
34
import Arweave from 'arweave';
45

56
/**
@@ -345,3 +346,14 @@ export async function decryptDataWithAES(
345346

346347
return decryptedData;
347348
}
349+
350+
export const ArweaveKit = createArweaveKit({
351+
concatenateArrayBuffers,
352+
separateArrayBuffer,
353+
bufferToBase64,
354+
encryptDataWithAES,
355+
encryptAESKeywithRSA,
356+
decryptAESKeywithRSA,
357+
base64ToBuffer,
358+
decryptDataWithAES,
359+
});

src/lib/graphql.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Arweave from 'arweave';
22
import { buildSchema, parse, validate, GraphQLError } from 'graphql';
33

44
import * as Types from '../types/graphql';
5-
import { ARWEAVE_GATEWAYS } from '../utils';
5+
import { createArweaveKit, ARWEAVE_GATEWAYS } from '../utils';
66

77
/**
88
* Query data with GraphQL endpoint
@@ -286,3 +286,11 @@ enum SortOrder {
286286
HEIGHT_DESC
287287
}
288288
`;
289+
290+
export const ArweaveKit = createArweaveKit({
291+
queryGQL,
292+
queryTransactionsGQL,
293+
queryAllTransactionsGQL,
294+
ARWEAVE_GATEWAYS,
295+
graphQlSchemaString,
296+
});

src/lib/transaction.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { JWKInterface } from 'arweave/node/lib/wallet';
44
import * as Types from '../types/transaction';
55
import { Othent as othent } from 'othent';
66
import { ethers } from 'ethers';
7-
import { appVersionTag } from '../utils';
7+
import { createArweaveKit, appVersionTag } from '../utils';
88

99
async function initArweave(params: Types.InitArweaveProps) {
1010
let arweave: Arweave;
@@ -347,3 +347,13 @@ export async function createAndPostTransactionWOthent(
347347
throw new Error('Transaction creation unsuccessful.');
348348
}
349349
}
350+
351+
export const ArweaveKit = createArweaveKit({
352+
initArweave,
353+
createTransaction,
354+
signTransaction,
355+
postTransaction,
356+
getTransactionStatus,
357+
getTransaction,
358+
createAndPostTransactionWOthent,
359+
});

0 commit comments

Comments
 (0)