@ecency/wallets 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ name: Any build
2
+ on:
3
+ push:
4
+ branches:
5
+ - '*'
6
+ jobs:
7
+ build:
8
+ runs-on: ubuntu-latest
9
+ strategy:
10
+ matrix:
11
+ node-version: [20.x]
12
+ steps:
13
+ - uses: actions/checkout@v3
14
+ - name: Use Node.js ${{ matrix.node-version }}
15
+ uses: actions/setup-node@v3
16
+ with:
17
+ node-version: ${{ matrix.node-version }}
18
+ - name: npm install, lint and/or test
19
+ run: |
20
+ yarn
21
+ - name: build
22
+ run: yarn build
23
+ env:
24
+ CI: true
@@ -0,0 +1,20 @@
1
+ name: Publish to npm
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ - app
7
+
8
+ jobs:
9
+ publish:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v1
13
+ - uses: actions/setup-node@v1
14
+ with:
15
+ node-version: 20
16
+ - run: yarn
17
+ - run: yarn build
18
+ - uses: JS-DevTools/npm-publish@v1
19
+ with:
20
+ token: ${{ secrets.NPM_TOKEN }}
@@ -0,0 +1,6 @@
1
+ <component name="InspectionProjectProfileManager">
2
+ <profile version="1.0">
3
+ <option name="myName" value="Project Default" />
4
+ <inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
5
+ </profile>
6
+ </component>
package/.idea/misc.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager">
4
+ <output url="file://$PROJECT_DIR$/out" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,8 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/wallets.iml" filepath="$PROJECT_DIR$/.idea/wallets.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
package/.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="JAVA_MODULE" version="4">
3
+ <component name="NewModuleRootManager" inherit-compiler-output="true">
4
+ <exclude-output />
5
+ <content url="file://$MODULE_DIR$" />
6
+ <orderEntry type="inheritedJdk" />
7
+ <orderEntry type="sourceFolder" forTests="false" />
8
+ </component>
9
+ </module>
package/Readme.md ADDED
@@ -0,0 +1,36 @@
1
+ # Ecency wallets – manage Hive account and attach external wallets to it in Ecency system
2
+
3
+ [![NPM](https://img.shields.io/npm/v/@ecency/wallets.svg)](https://www.npmjs.com/package/@ecency/wallets) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
4
+
5
+ ## Overview
6
+
7
+ `@ecency/wallets` provides an API for managing Hive blockchain wallets and external cryptocurrency wallets within the Ecency ecosystem.
8
+
9
+ ## Installation
10
+
11
+ ```sh
12
+ yarn add @ecency/wallets
13
+ # or
14
+ npm install @ecency/wallets
15
+ ```
16
+
17
+ ## Setup
18
+
19
+ 1. Install `react @tanstack/react-query @hiveio/dhive`
20
+ 2. Use!
21
+
22
+ ## Features
23
+
24
+ This package built on top of [@hiveio/dhive](https://www.npmjs.com/package/@hiveio/dhive) and [okweb3](http://okx.github.io/) packages.
25
+
26
+ Main functionality is creating wallets based on seed phrase([BIP39](https://www.npmjs.com/package/bip39)) and generating addresses with keys on device. Seed phrases and private keys are never sent to any API, all operations happen locally.
27
+
28
+ Supportings tokens: BTC, ETH, SOL, TRX, TON, ATOM, APT – theoretically all child tokens of these systems. Make forks for it.
29
+
30
+ ## Roadmap
31
+
32
+ - Add more Hive wallets operations
33
+ - Allow to sign transactions with external wallets
34
+ - Allow to import existing wallets by phrase or private keys
35
+ - Support of DASH
36
+ - Support of DOGE
@@ -0,0 +1,75 @@
1
+ import { BaseWallet } from '@okxweb3/coin-base';
2
+ import { UseMutationResult } from '@tanstack/react-query';
3
+ import { UseQueryResult } from '@tanstack/react-query';
4
+
5
+ export declare function delay(ms: number): Promise<unknown>;
6
+
7
+ export declare interface EcencyCreateWalletInformation {
8
+ address: string;
9
+ privateKey: string;
10
+ publicKey: string;
11
+ username: string;
12
+ currency: EcencyWalletCurrency;
13
+ }
14
+
15
+ export declare interface EcencyHiveKeys {
16
+ username: string;
17
+ owner: string;
18
+ active: string;
19
+ posting: string;
20
+ memo: string;
21
+ masterPassword: string;
22
+ ownerPubkey: string;
23
+ activePubkey: string;
24
+ postingPubkey: string;
25
+ memoPubkey: string;
26
+ }
27
+
28
+ export declare enum EcencyWalletCurrency {
29
+ BTC = "btc",
30
+ ETH = "eth",
31
+ APT = "atpos",
32
+ ATOM = "cosmos",
33
+ TON = "ton",
34
+ TRON = "tron",
35
+ SOL = "Solana"
36
+ }
37
+
38
+ declare namespace EcencyWalletsPrivateApi {
39
+ export {
40
+ useCreateAccountWithWallets
41
+ }
42
+ }
43
+ export { EcencyWalletsPrivateApi }
44
+
45
+ export declare function getWallet(currency: EcencyWalletCurrency): BaseWallet | undefined;
46
+
47
+ declare interface Payload {
48
+ currency: EcencyWalletCurrency;
49
+ address: string;
50
+ }
51
+
52
+ export declare function useCoinGeckoPriceQuery(currency?: EcencyWalletCurrency): UseQueryResult<number, Error>;
53
+
54
+ declare function useCreateAccountWithWallets(username: string): UseMutationResult<Response, Error, Payload, unknown>;
55
+
56
+ /**
57
+ * Returns information about the actual balance of the given currency address
58
+ * using various of public APIs
59
+ * todo extract URLs to configs
60
+ * @param currency
61
+ * @param address
62
+ * @returns
63
+ */
64
+ export declare function useGetExternalWalletBalanceQuery(currency: EcencyWalletCurrency, address: string): UseQueryResult<number, Error>;
65
+
66
+ export declare function useHiveKeysQuery(username: string): UseQueryResult<EcencyHiveKeys, Error>;
67
+
68
+ export declare function useSeedPhrase(): UseQueryResult<string, Error>;
69
+
70
+ export declare function useWalletCreate(username: string, currency: EcencyWalletCurrency): {
71
+ createWallet: UseMutationResult<EcencyCreateWalletInformation, Error, void, unknown>;
72
+ importWallet: () => void;
73
+ };
74
+
75
+ export { }
@@ -0,0 +1,274 @@
1
+ import { useCallback as b } from "react";
2
+ import { useQuery as l, useQueryClient as g, useMutation as f } from "@tanstack/react-query";
3
+ import { BtcWallet as T } from "@okxweb3/coin-bitcoin";
4
+ import { EthWallet as R } from "@okxweb3/coin-ethereum";
5
+ import { TrxWallet as S } from "@okxweb3/coin-tron";
6
+ import { TonWallet as P } from "@okxweb3/coin-ton";
7
+ import { SolWallet as O } from "@okxweb3/coin-solana";
8
+ import { AtomWallet as v } from "@okxweb3/coin-cosmos";
9
+ import { AptosWallet as k } from "@okxweb3/coin-aptos";
10
+ import A, { mnemonicToSeed as D } from "bip39";
11
+ import { LRUCache as N } from "lru-cache";
12
+ import { PrivateKey as u } from "@hiveio/dhive";
13
+ var o = /* @__PURE__ */ ((e) => (e.BTC = "btc", e.ETH = "eth", e.APT = "atpos", e.ATOM = "cosmos", e.TON = "ton", e.TRON = "tron", e.SOL = "Solana", e))(o || {});
14
+ function K(e) {
15
+ return new Promise((t) => setTimeout(t, e));
16
+ }
17
+ function j(e) {
18
+ switch (e) {
19
+ case o.BTC:
20
+ return new T();
21
+ case o.ETH:
22
+ return new R();
23
+ case o.TRON:
24
+ return new S();
25
+ case o.TON:
26
+ return new P();
27
+ case o.SOL:
28
+ return new O();
29
+ case o.ATOM:
30
+ return new v();
31
+ case o.APT:
32
+ return new k();
33
+ default:
34
+ return;
35
+ }
36
+ }
37
+ function ae(e, t) {
38
+ return l({
39
+ queryKey: ["ecency-wallets", "external-wallet-balance", e, t],
40
+ queryFn: async () => {
41
+ switch (e) {
42
+ case o.BTC:
43
+ const s = await (await fetch(
44
+ `https://mempool.space/api/address/${t}`
45
+ )).json();
46
+ return (s.chain_stats.funded_txo_sum - s.chain_stats.spent_txo_sum) / 1e8;
47
+ case o.ETH:
48
+ return +(await (await fetch("https://eth.llamarpc.com", {
49
+ method: "POST",
50
+ body: JSON.stringify({
51
+ jsonrpc: "2.0",
52
+ id: "1",
53
+ method: "eth_getBalance",
54
+ params: [t, "latest"]
55
+ })
56
+ })).json()).result / 1e18;
57
+ case o.SOL:
58
+ return (await (await fetch(
59
+ "https://api.mainnet-beta.solana.com",
60
+ {
61
+ method: "POST",
62
+ body: JSON.stringify({
63
+ jsonrpc: "2.0",
64
+ id: "1",
65
+ method: "getBalance",
66
+ params: [t]
67
+ })
68
+ }
69
+ )).json()).result.value / 1e9;
70
+ case o.TRON:
71
+ return (await (await fetch(
72
+ `https://api.trongrid.io/v1/accounts/${t}`
73
+ )).json()).data[0].balance / 1e6;
74
+ case o.TON:
75
+ return (await (await fetch(
76
+ `https://tonapi.io/v1/blockchain/getAccount?account=${t}`
77
+ )).json()).balance / 1e9;
78
+ case o.APT:
79
+ const w = (await (await fetch(
80
+ `https://fullnode.mainnet.aptoslabs.com/v1/accounts/${t}/resources`
81
+ )).json()).find(
82
+ (h) => h.type.includes("coin::CoinStore")
83
+ );
84
+ return w ? parseInt(w.data.coin.value) / 1e8 : 0;
85
+ case o.ATOM:
86
+ return +(await (await fetch(
87
+ `https://rest.cosmos.directory/cosmoshub/auth/accounts/${t}`
88
+ )).json()).result.value.coins[0].amount / 1e6;
89
+ }
90
+ }
91
+ });
92
+ }
93
+ function d() {
94
+ return l({
95
+ queryKey: ["ecency-wallets", "seed"],
96
+ queryFn: async () => A.generateMnemonic(128)
97
+ });
98
+ }
99
+ const _ = {
100
+ max: 500,
101
+ // how long to live in ms
102
+ ttl: 1e3 * 60 * 5,
103
+ // return stale items before removing from cache?
104
+ allowStale: !1,
105
+ updateAgeOnGet: !1,
106
+ updateAgeOnHas: !1
107
+ }, y = new N(_), m = Symbol("undefined"), W = (e, t) => y.set(e, t === void 0 ? m : t), q = (e) => {
108
+ const t = y.get(e);
109
+ return t === m ? void 0 : t;
110
+ };
111
+ function se(e) {
112
+ return l({
113
+ queryKey: ["ecency-wallets", "coingecko-price", e],
114
+ queryFn: async () => {
115
+ let t = e;
116
+ switch (e) {
117
+ case o.BTC:
118
+ t = "binance-wrapped-btc";
119
+ break;
120
+ case o.ETH:
121
+ t = "ethereum";
122
+ break;
123
+ case o.SOL:
124
+ t = "solana";
125
+ break;
126
+ default:
127
+ t = e;
128
+ }
129
+ let n = q("gecko"), s;
130
+ if (n)
131
+ s = n;
132
+ else {
133
+ const a = await (await fetch(
134
+ "https://api.coingecko.com/api/v3/simple/price",
135
+ {
136
+ method: "POST",
137
+ body: JSON.stringify({
138
+ params: {
139
+ ids: [t],
140
+ vs_currencies: "usd"
141
+ }
142
+ })
143
+ }
144
+ )).json();
145
+ W("gecko", a === void 0 ? m : a), s = a;
146
+ }
147
+ return 1 / +s[Object.keys(s)[0]].usd;
148
+ },
149
+ enabled: !!e
150
+ });
151
+ }
152
+ function L(e) {
153
+ const { data: t } = d();
154
+ return l({
155
+ queryKey: [
156
+ "ecenc-wallets",
157
+ "hive-keys",
158
+ e,
159
+ D(t ?? "")
160
+ ],
161
+ queryFn: async () => {
162
+ if (!t)
163
+ throw new Error("[Ecency][Wallets] - no seed to create Hive account");
164
+ const n = u.fromLogin(e, t, "owner"), s = u.fromLogin(e, t, "active"), c = u.fromLogin(e, t, "posting"), r = u.fromLogin(e, t, "memo");
165
+ return {
166
+ username: e,
167
+ owner: n.toString(),
168
+ active: s.toString(),
169
+ posting: c.toString(),
170
+ memo: r.toString(),
171
+ ownerPubkey: n.createPublic().toString(),
172
+ activePubkey: s.createPublic().toString(),
173
+ postingPubkey: c.createPublic().toString(),
174
+ memoPubkey: r.createPublic().toString()
175
+ };
176
+ }
177
+ });
178
+ }
179
+ const B = {
180
+ [o.BTC]: "m/44'/0'/0'/0/0",
181
+ // Bitcoin (BIP44)
182
+ [o.ETH]: "m/44'/60'/0'/0/0",
183
+ // Ethereum (BIP44)
184
+ [o.SOL]: "m/44'/501'/0'/0/0",
185
+ // Solana (BIP44)
186
+ [o.TON]: "m/44'/396'/0'/0/0",
187
+ // TON (BIP44)
188
+ [o.TRON]: "m/44'/195'/0'/0/0",
189
+ // Tron (BIP44)
190
+ [o.APT]: "m/44'/637'/0'/0/0",
191
+ // Aptos (BIP44)
192
+ [o.ATOM]: "m/44'/118'/0'/0/0"
193
+ // Cosmos (BIP44)
194
+ };
195
+ function ce(e, t) {
196
+ const { data: n } = d(), s = g(), c = f({
197
+ mutationKey: ["ecency-wallets", "create-wallet", e, t],
198
+ mutationFn: async () => {
199
+ if (!n)
200
+ throw new Error("[Ecency][Wallets] - No seed to create a wallet");
201
+ const a = j(t), i = await (a == null ? void 0 : a.getDerivedPrivateKey({
202
+ mnemonic: n,
203
+ hdPath: B[t]
204
+ }));
205
+ await K(1e3);
206
+ const p = await (a == null ? void 0 : a.getNewAddress({
207
+ privateKey: i
208
+ }));
209
+ return {
210
+ privateKey: i,
211
+ address: p.address,
212
+ publicKey: p.publicKey,
213
+ username: e,
214
+ currency: t
215
+ };
216
+ },
217
+ onSuccess: (a) => {
218
+ s.setQueryData(
219
+ ["ecency-wallets", "wallets", a.username],
220
+ (i) => new Map(i ? Array.from(i.entries()) : []).set(a.currency, a)
221
+ );
222
+ }
223
+ }), r = b(() => {
224
+ }, []);
225
+ return {
226
+ createWallet: c,
227
+ importWallet: r
228
+ };
229
+ }
230
+ const H = {};
231
+ function x(e) {
232
+ const { data: t } = l({
233
+ queryKey: ["ecency-wallets", "wallets", e]
234
+ }), { data: n } = L(e);
235
+ return f({
236
+ mutationKey: ["ecency-wallets", "create-account-with-wallets", e],
237
+ mutationFn: ({ currency: s, address: c }) => fetch(H + "/private-api/wallets-add", {
238
+ method: "POST",
239
+ body: JSON.stringify({
240
+ username: e,
241
+ token: s,
242
+ address: c,
243
+ meta: {
244
+ ownerPublicKey: n == null ? void 0 : n.ownerPubkey,
245
+ activePublicKey: n == null ? void 0 : n.activePubkey,
246
+ postingPublicKey: n == null ? void 0 : n.postingPubkey,
247
+ memoPublicKey: n == null ? void 0 : n.memoPubkey,
248
+ ...Array.from((t == null ? void 0 : t.entries()) ?? []).reduce(
249
+ (r, [a, i]) => ({
250
+ ...r,
251
+ [a]: i.address
252
+ }),
253
+ {}
254
+ )
255
+ }
256
+ })
257
+ })
258
+ });
259
+ }
260
+ const re = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
261
+ __proto__: null,
262
+ useCreateAccountWithWallets: x
263
+ }, Symbol.toStringTag, { value: "Module" }));
264
+ export {
265
+ o as EcencyWalletCurrency,
266
+ re as EcencyWalletsPrivateApi,
267
+ K as delay,
268
+ j as getWallet,
269
+ se as useCoinGeckoPriceQuery,
270
+ ae as useGetExternalWalletBalanceQuery,
271
+ L as useHiveKeysQuery,
272
+ d as useSeedPhrase,
273
+ ce as useWalletCreate
274
+ };
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@ecency/wallets",
3
+ "private": false,
4
+ "version": "1.0.0",
5
+ "type": "module",
6
+ "main": "./dist/ecency-wallets.umd.js",
7
+ "module": "./dist/ecency-wallets.es.js",
8
+ "scripts": {
9
+ "dev": "vite",
10
+ "build": "tsc && vite build",
11
+ "preview": "vite preview"
12
+ },
13
+ "devDependencies": {
14
+ "@hiveio/dhive": "^1.3.2",
15
+ "@tanstack/react-query": "^5.66.11",
16
+ "@types/node": "^22.13.8",
17
+ "@types/react": "^19.0.10",
18
+ "@vitejs/plugin-react": "^4.3.4",
19
+ "eslint": "^9.21.0",
20
+ "lru-cache": "^11.0.2",
21
+ "prettier": "^3.5.2",
22
+ "react": "^19.0.0",
23
+ "typescript": "~5.7.2",
24
+ "vite": "^6.2.0",
25
+ "vite-plugin-dts": "^4.5.1",
26
+ "vite-plugin-node-polyfills": "^0.23.0"
27
+ },
28
+ "dependencies": {
29
+ "@okxweb3/coin-aptos": "^1.2.0",
30
+ "@okxweb3/coin-base": "^1.1.2",
31
+ "@okxweb3/coin-bitcoin": "^1.2.0",
32
+ "@okxweb3/coin-cosmos": "^1.1.0",
33
+ "@okxweb3/coin-ethereum": "^1.0.12",
34
+ "@okxweb3/coin-solana": "^1.1.0",
35
+ "@okxweb3/coin-ton": "^1.1.1-beta.1",
36
+ "@okxweb3/coin-tron": "^1.1.0",
37
+ "@okxweb3/crypto-lib": "^1.0.10",
38
+ "bip39": "^3.1.0"
39
+ }
40
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "useDefineForClassFields": true,
5
+ "module": "ESNext",
6
+ "lib": ["ES2020", "DOM", "DOM.Iterable"],
7
+ "skipLibCheck": true,
8
+
9
+ /* Bundler mode */
10
+ "moduleResolution": "bundler",
11
+ "allowImportingTsExtensions": true,
12
+ "isolatedModules": true,
13
+ "moduleDetection": "force",
14
+ "noEmit": true,
15
+
16
+ /* Linting */
17
+ "strict": true,
18
+ "noUnusedLocals": true,
19
+ "noUnusedParameters": true,
20
+ "noFallthroughCasesInSwitch": true,
21
+ "noUncheckedSideEffectImports": true,
22
+ "paths": {
23
+ "@/*": ["./src/*"]
24
+ }
25
+ },
26
+ "include": ["src"]
27
+ }
package/vite.config.ts ADDED
@@ -0,0 +1,45 @@
1
+ import path, { resolve } from "path";
2
+ import { defineConfig } from "vite";
3
+ import react from "@vitejs/plugin-react";
4
+ import dtsPlugin from "vite-plugin-dts";
5
+ import { nodePolyfills } from "vite-plugin-node-polyfills";
6
+
7
+ // https://vitejs.dev/guide/build.html#library-mode
8
+ export default defineConfig({
9
+ build: {
10
+ lib: {
11
+ entry: path.resolve(__dirname, "src/index.ts"),
12
+ name: "Ecency Wallets",
13
+ formats: ["es"],
14
+ fileName: (format) => `ecency-wallets.${format}.js`,
15
+ },
16
+ rollupOptions: {
17
+ external: [
18
+ "crypto",
19
+ "react",
20
+ "@hiveio/dhive",
21
+ "@tanstack/react-query",
22
+ "lru-cache",
23
+ "scheduler",
24
+ "react/jsx-runtime",
25
+ "@okxweb3/coin-aptos",
26
+ "@okxweb3/coin-base",
27
+ "@okxweb3/coin-bitcoin",
28
+ "@okxweb3/coin-cosmos",
29
+ "@okxweb3/coin-ethereum",
30
+ "@okxweb3/coin-solana",
31
+ "@okxweb3/coin-ton",
32
+ "@okxweb3/coin-tron",
33
+ "@okxweb3/crypto-lib",
34
+ "bip39",
35
+ ],
36
+ },
37
+ },
38
+ resolve: {
39
+ alias: {
40
+ "@": path.resolve(__dirname, "./src"),
41
+ "near-api-js": "near-api-js/dist/near-api-js.js",
42
+ },
43
+ },
44
+ plugins: [react(), dtsPlugin({ rollupTypes: true }), nodePolyfills()],
45
+ });