@poolstream/client 1.0.1

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.
Files changed (48) hide show
  1. package/.vscode/launch.json +14 -0
  2. package/LICENSE +121 -0
  3. package/README.md +157 -0
  4. package/babel.config.js +6 -0
  5. package/dist/balance.js +2 -0
  6. package/dist/balance.js.map +1 -0
  7. package/dist/currency-network-module.js +23 -0
  8. package/dist/currency-network-module.js.map +1 -0
  9. package/dist/index.js +3 -0
  10. package/dist/index.js.map +1 -0
  11. package/dist/info.js +2 -0
  12. package/dist/info.js.map +1 -0
  13. package/dist/network/ripple/ripple.js +50 -0
  14. package/dist/network/ripple/ripple.js.map +1 -0
  15. package/dist/network/solana/solana.js +42 -0
  16. package/dist/network/solana/solana.js.map +1 -0
  17. package/dist/network/stellar/stellar.js +102 -0
  18. package/dist/network/stellar/stellar.js.map +1 -0
  19. package/dist/network/tron/tron.js +63 -0
  20. package/dist/network/tron/tron.js.map +1 -0
  21. package/dist/network.js +2 -0
  22. package/dist/network.js.map +1 -0
  23. package/dist/poolstream.js +91 -0
  24. package/dist/poolstream.js.map +1 -0
  25. package/dist/transaction.js +2 -0
  26. package/dist/transaction.js.map +1 -0
  27. package/dist/walletaddress.js +2 -0
  28. package/dist/walletaddress.js.map +1 -0
  29. package/logo_up.png +0 -0
  30. package/package.json +54 -0
  31. package/src/balance.ts +6 -0
  32. package/src/currency-network-module.ts +39 -0
  33. package/src/index.ts +13 -0
  34. package/src/info.ts +5 -0
  35. package/src/network/ripple/ripple.ts +66 -0
  36. package/src/network/solana/solana.ts +73 -0
  37. package/src/network/stellar/stellar.ts +149 -0
  38. package/src/network/tron/tron.ts +96 -0
  39. package/src/network.ts +4 -0
  40. package/src/poolstream.ts +153 -0
  41. package/src/transaction.ts +23 -0
  42. package/src/walletaddress.ts +7 -0
  43. package/test/network/ripple/ripple.test.ts +31 -0
  44. package/test/network/solana/solana.test.ts +32 -0
  45. package/test/network/stellar/stellar.test.ts +56 -0
  46. package/test/network/tron/tron.test.ts +53 -0
  47. package/tests.js +13 -0
  48. package/tsconfig.json +24 -0
@@ -0,0 +1,91 @@
1
+ import { getCoinNetworkModule } from "./currency-network-module";
2
+ import axios from "axios";
3
+ export class PoolStream {
4
+ constructor(url, options = {}) {
5
+ this.url = url;
6
+ this.options = options;
7
+ }
8
+ async transaction(coin, network, txid) {
9
+ return await axios({
10
+ method: "get",
11
+ headers: { ...this.apiKeyHeader() },
12
+ url: `${this.url}/auth/rest/v1/${coin}/${network}/transaction/${txid}`,
13
+ responseType: "json",
14
+ });
15
+ }
16
+ async info(params) {
17
+ return await axios({
18
+ method: "get",
19
+ headers: { ...this.apiKeyHeader() },
20
+ url: `${this.url}/auth/rest/v1/${params.network}/info`,
21
+ responseType: "json",
22
+ });
23
+ }
24
+ async transactions(params) {
25
+ const query = Object.entries(params.filter ? params.filter : {})
26
+ .filter(([_, value]) => value !== undefined)
27
+ .map(([key, value]) => `${key}=${value}`)
28
+ .join("&");
29
+ return await axios({
30
+ method: "get",
31
+ headers: { ...this.apiKeyHeader() },
32
+ url: `${this.url}/auth/rest/v1/${params.network}${params.contractAddress ? `/${params.contractAddress}` : ""}/transactions?${query}`,
33
+ responseType: "json",
34
+ });
35
+ }
36
+ async balances(params) {
37
+ let query = "?";
38
+ if (typeof params.addresses === "string") {
39
+ query += params.addresses;
40
+ }
41
+ else {
42
+ for (let i = 0; i < params.addresses.length; i++) {
43
+ query = `${i > 0 ? "&" : ""}addresses=${params.addresses}`;
44
+ }
45
+ }
46
+ return await axios({
47
+ method: "get",
48
+ headers: { ...this.apiKeyHeader() },
49
+ url: `${this.url}/auth/rest/v1/${params.network}${params.contractAddress ? `/${params.contractAddress}` : ""}/balances${query}`,
50
+ responseType: "json",
51
+ });
52
+ }
53
+ async networks() {
54
+ return await axios({
55
+ method: "get",
56
+ url: `${this.url}/networks`,
57
+ responseType: "json",
58
+ });
59
+ }
60
+ async submitTransaction(params) {
61
+ return await axios({
62
+ method: "post",
63
+ headers: { ...this.apiKeyHeader() },
64
+ url: `${this.url}/auth/rest/v1/${params.network}${params.contractAddress ? `/${params.contractAddress}` : ""}/submitSignedTransaction`,
65
+ responseType: "json",
66
+ data: { signedTransaction: params.signedTransaction },
67
+ });
68
+ }
69
+ async signedAndSubmitTransaction(params) {
70
+ const signedTransaction = await getCoinNetworkModule(params.network, false, params.contractAddress).signTransaction(params.transaction);
71
+ return await this.submitTransaction({
72
+ network: params.network,
73
+ signedTransaction,
74
+ contractAddress: params.contractAddress,
75
+ });
76
+ }
77
+ async signTransaction(params) {
78
+ return await getCoinNetworkModule(params.network, false, params.contractAddress).signTransaction(params.transaction);
79
+ }
80
+ async generateWalletAddress(network) {
81
+ const walletAddress = await getCoinNetworkModule(network).generateWalletAddress();
82
+ return walletAddress;
83
+ }
84
+ apiKeyHeader() {
85
+ if (this.options.apiKey) {
86
+ return { API_KEY: this.options.apiKey };
87
+ }
88
+ throw new Error("API_KEY is required.");
89
+ }
90
+ }
91
+ //# sourceMappingURL=poolstream.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"poolstream.js","sourceRoot":"","sources":["../src/poolstream.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAGjE,OAAO,KAAK,MAAM,OAAO,CAAC;AAc1B,MAAM,OAAO,UAAU;IACrB,YAAoB,GAAW,EAAU,UAA6B,EAAE;QAApD,QAAG,GAAH,GAAG,CAAQ;QAAU,YAAO,GAAP,OAAO,CAAwB;IAAG,CAAC;IAErE,KAAK,CAAC,WAAW,CACtB,IAAY,EACZ,OAAe,EACf,IAAY;QAEZ,OAAO,MAAM,KAAK,CAAC;YACjB,MAAM,EAAE,KAAK;YACb,OAAO,EAAE,EAAE,GAAG,IAAI,CAAC,YAAY,EAAE,EAAE;YACnC,GAAG,EAAE,GAAG,IAAI,CAAC,GAAG,iBAAiB,IAAI,IAAI,OAAO,gBAAgB,IAAI,EAAE;YACtE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,iBAAiB,EAAE,MAAM,CAAC,iBAAiB,EAAE;SACtD,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,0BAA0B,CAAC,MAIvC;QACC,MAAM,iBAAiB,GAAG,MAAM,oBAAoB,CAClD,MAAM,CAAC,OAAO,EACd,KAAK,EACL,MAAM,CAAC,eAAe,CACvB,CAAC,eAAe,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;QACtC,OAAO,MAAM,IAAI,CAAC,iBAAiB,CAAC;YAClC,OAAO,EAAE,MAAM,CAAC,OAAO;YACvB,iBAAiB;YACjB,eAAe,EAAE,MAAM,CAAC,eAAe;SACxC,CAAC,CAAC;IACL,CAAC;IAEM,KAAK,CAAC,eAAe,CAAC,MAI5B;QACC,OAAO,MAAM,oBAAoB,CAC/B,MAAM,CAAC,OAAO,EACd,KAAK,EACL,MAAM,CAAC,eAAe,CACvB,CAAC,eAAe,CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;IACxC,CAAC;IAEM,KAAK,CAAC,qBAAqB,CAAC,OAAe;QAChD,MAAM,aAAa,GAAkB,MAAM,oBAAoB,CAC7D,OAAO,CACR,CAAC,qBAAqB,EAAE,CAAC;QAC1B,OAAO,aAAa,CAAC;IACvB,CAAC;IACO,YAAY;QAClB,IAAI,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;YACxB,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC;QAC1C,CAAC;QACD,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;IAC1C,CAAC;CACF"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=transaction.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"transaction.js","sourceRoot":"","sources":["../src/transaction.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=walletaddress.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"walletaddress.js","sourceRoot":"","sources":["../src/walletaddress.ts"],"names":[],"mappings":""}
package/logo_up.png ADDED
Binary file
package/package.json ADDED
@@ -0,0 +1,54 @@
1
+ {
2
+ "name": "@poolstream/client",
3
+ "version": "1.0.1",
4
+ "description": "To work with a collection of cryptocurrencies.",
5
+ "main": "dist/index.js",
6
+ "private": false,
7
+ "scripts": {
8
+ "test": "npx jest",
9
+ "build": "tsc"
10
+ },
11
+ "repository": {
12
+ "type": "git",
13
+ "url": "git+ssh://git@github.com/lmfacchini/poolstream.git"
14
+ },
15
+ "keywords": [
16
+ "Cryptocoin",
17
+ "API",
18
+ "services",
19
+ "coin",
20
+ "blockchain",
21
+ "bitcoin",
22
+ "altacoins"
23
+ ],
24
+ "author": "Leonardo Marquini Facchini",
25
+ "license": "CC-BY-SA-4.0",
26
+ "bugs": {
27
+ "url": "https://github.com/lmfacchini/poolstream/issues"
28
+ },
29
+ "homepage": "https://github.com/lmfacchini/poolstream#readme",
30
+ "dependencies": {
31
+ "@solana/web3.js": "^1.98.0",
32
+ "@stellar/stellar-sdk": "^13.1.0",
33
+ "axios": "^1.7.7",
34
+ "crypto": "^1.0.1",
35
+ "json-schema": "^0.4.0",
36
+ "tronweb": "^6.0.0",
37
+ "urijs": "^1.19.11",
38
+ "xrpl": "^4.0.0"
39
+ },
40
+ "devDependencies": {
41
+ "@babel/core": "^7.26.0",
42
+ "@babel/preset-env": "^7.26.0",
43
+ "@babel/preset-typescript": "^7.26.0",
44
+ "@types/jest": "^29.5.14",
45
+ "@types/json-schema": "^7.0.15",
46
+ "@types/urijs": "^1.19.25",
47
+ "babel-jest": "^29.7.0",
48
+ "jest": "^29.7.0",
49
+ "ts-jest": "^29.2.5",
50
+ "typescript": "^5.7.2",
51
+ "vite": "^5.4.11",
52
+ "vitest": "^2.1.8"
53
+ }
54
+ }
package/src/balance.ts ADDED
@@ -0,0 +1,6 @@
1
+ export interface Balance {
2
+ currency: string;
3
+ network: string;
4
+ availables: { [key: string]: string };
5
+ decimals: number;
6
+ }
@@ -0,0 +1,39 @@
1
+ import { Ripple } from "./network/ripple/ripple";
2
+ import { Solana } from "./network/solana/solana";
3
+ import { Stellar } from "./network/stellar/stellar";
4
+ import { Tron } from "./network/tron/tron";
5
+ import { SignebleTransaction } from "./transaction";
6
+ import { WalletAddress } from "./walletaddress";
7
+
8
+ export interface CurrencyNetworkModule {
9
+ signTransaction(transaction: SignebleTransaction): Promise<string>;
10
+ generateWalletAddress(): Promise<WalletAddress>;
11
+
12
+ readonly network: string;
13
+ }
14
+
15
+ export function getCoinNetworkModule(
16
+ network: string,
17
+ test: boolean = false,
18
+ contractAddress?: string
19
+ ): CurrencyNetworkModule {
20
+ network = network.toLowerCase();
21
+ const nw = networks[network];
22
+ if (nw) {
23
+ const instanceCurrencyModule = nw.currencies(contractAddress);
24
+ if (instanceCurrencyModule) {
25
+ return instanceCurrencyModule(test);
26
+ }
27
+ throw new Error(
28
+ `Contract Address [${contractAddress}]. Network [${network}] not available.`
29
+ );
30
+ }
31
+ throw new Error(`Network [${network}] not available.`);
32
+ }
33
+
34
+ export const networks: any = {
35
+ tron: Tron,
36
+ stellar: Stellar,
37
+ solana: Solana,
38
+ ripple: Ripple,
39
+ };
package/src/index.ts ADDED
@@ -0,0 +1,13 @@
1
+ export {
2
+ Transaction,
3
+ SignebleTransaction,
4
+ SubmittedTransaction,
5
+ TransactionItem,
6
+ } from "./transaction";
7
+ export { Network } from "./network";
8
+ export { CurrencyNetworkModule } from "./currency-network-module";
9
+ export { PoolStream, PoolStreamOptions } from "./poolstream";
10
+ export { WalletAddress } from "./walletaddress";
11
+ export { Balance } from "./balance";
12
+ export { Info } from "./info";
13
+ export { Ripple } from "./network/ripple/ripple";
package/src/info.ts ADDED
@@ -0,0 +1,5 @@
1
+ export interface Info {
2
+ currency: string;
3
+ network: string;
4
+ decimals: number;
5
+ }
@@ -0,0 +1,66 @@
1
+ import { Payment, Wallet } from "xrpl";
2
+ import { SignebleTransaction } from "../../transaction";
3
+
4
+ import { CurrencyNetworkModule } from "../../currency-network-module";
5
+ import { WalletAddress } from "../../walletaddress";
6
+
7
+ export abstract class Ripple implements CurrencyNetworkModule {
8
+ readonly network = "ripple";
9
+ constructor() {}
10
+
11
+ abstract signTransaction(transaction: SignebleTransaction): Promise<string>;
12
+
13
+ async generateWalletAddress(): Promise<WalletAddress> {
14
+ const wallet = Wallet.generate();
15
+ return {
16
+ address: wallet.classicAddress,
17
+ secret: wallet.seed ? wallet.seed : wallet.privateKey,
18
+ currency: "xrp",
19
+ network: this.network,
20
+ raw: wallet,
21
+ };
22
+ }
23
+ }
24
+
25
+ // export class GenericRipple extends Ripple {
26
+ // constructor(protected readonly assetAddress: string, currency: string) {
27
+ // super(currency);
28
+ // }
29
+
30
+ // async signTransaction(transaction: SignebleTransaction): Promise<string> {
31
+ // throw new Error("Method not implemented.");
32
+ // }
33
+ // }
34
+
35
+ export class XrpRipple extends Ripple {
36
+ constructor() {
37
+ super();
38
+ }
39
+ async signTransaction(
40
+ signebleTransaction: SignebleTransaction
41
+ ): Promise<string> {
42
+ const signTransactions = Promise.all(
43
+ signebleTransaction.items.map(async (item) => {
44
+ const seedWallet = Wallet.fromSeed(item.secret);
45
+
46
+ const Memos = item.extra ? item.extra.memos : undefined;
47
+ const Fee = signebleTransaction.fee
48
+ ? signebleTransaction.fee.toString()
49
+ : undefined;
50
+ const tx: Payment = {
51
+ TransactionType: "Payment",
52
+ Account: item.from,
53
+ Destination: item.to,
54
+ Amount: item.amount,
55
+ Memos,
56
+ Fee,
57
+ };
58
+
59
+ const signedTx = seedWallet.sign(tx);
60
+
61
+ return btoa(JSON.stringify(signedTx.tx_blob));
62
+ })
63
+ );
64
+ return btoa(JSON.stringify(signTransactions));
65
+ }
66
+ }
@@ -0,0 +1,73 @@
1
+ import { SignebleTransaction } from "../../transaction";
2
+
3
+ import { CurrencyNetworkModule } from "../../currency-network-module";
4
+ import { WalletAddress } from "../../walletaddress";
5
+
6
+ import {
7
+ clusterApiUrl,
8
+ Connection,
9
+ Keypair,
10
+ PublicKey,
11
+ SystemProgram,
12
+ Transaction,
13
+ } from "@solana/web3.js";
14
+
15
+ export abstract class Solana implements CurrencyNetworkModule {
16
+ readonly connection;
17
+ readonly network = "solana";
18
+ constructor(test: boolean = false) {
19
+ this.connection = new Connection(
20
+ clusterApiUrl(test ? "devnet" : "mainnet-beta")
21
+ );
22
+ }
23
+
24
+ abstract signTransaction(transaction: SignebleTransaction): Promise<string>;
25
+
26
+ async generateWalletAddress(): Promise<WalletAddress> {
27
+ const wallet = Keypair.generate();
28
+ return {
29
+ address: wallet.publicKey.toBase58(),
30
+ secret: Buffer.from(wallet.secretKey).toString("hex"),
31
+ currency: "sol",
32
+ network: this.network,
33
+ raw: wallet,
34
+ };
35
+ }
36
+ }
37
+
38
+ export class SolSolana extends Solana {
39
+ constructor(test: boolean = false) {
40
+ super(test);
41
+ }
42
+ async signTransaction(
43
+ signebleTransaction: SignebleTransaction
44
+ ): Promise<string> {
45
+ const signTransactions = Promise.all(
46
+ signebleTransaction.items.map(async (item) => {
47
+ const senderSecretKey = Uint8Array.from(
48
+ Buffer.from(item.secret, "hex")
49
+ );
50
+ const sender = Keypair.fromSecretKey(senderSecretKey);
51
+ const receiver = new PublicKey(item.to);
52
+ const { blockhash } = await this.connection.getLatestBlockhash();
53
+ const transaction = new Transaction().add(
54
+ SystemProgram.transfer({
55
+ fromPubkey: new PublicKey(item.from),
56
+ toPubkey: receiver,
57
+ lamports: parseInt(item.amount),
58
+ })
59
+ );
60
+ transaction.recentBlockhash = blockhash;
61
+
62
+ transaction.feePayer = sender.publicKey;
63
+
64
+ transaction.sign(sender);
65
+
66
+ return transaction.serialize().toString("base64");
67
+ })
68
+ );
69
+ return btoa(JSON.stringify(signTransactions));
70
+ }
71
+ }
72
+
73
+ const modules = { sol: (test: boolean = false) => new SolSolana() };
@@ -0,0 +1,149 @@
1
+ import {
2
+ Horizon,
3
+ Keypair,
4
+ Asset,
5
+ BASE_FEE,
6
+ Memo,
7
+ Networks,
8
+ Operation,
9
+ TransactionBuilder,
10
+ } from "@stellar/stellar-sdk";
11
+ import { SignebleTransaction } from "../../transaction";
12
+
13
+ import { CurrencyNetworkModule } from "../../currency-network-module";
14
+ import { WalletAddress } from "../../walletaddress";
15
+
16
+ import * as crypto from "crypto";
17
+
18
+ export abstract class Stellar implements CurrencyNetworkModule {
19
+ readonly server: Horizon.Server;
20
+ readonly network = "stellar";
21
+ constructor(test: boolean) {
22
+ this.server = new Horizon.Server(
23
+ test
24
+ ? "https://horizon-testnet.stellar.org"
25
+ : "https://horizon-futurenet.stellar.org"
26
+ );
27
+ }
28
+
29
+ abstract signTransaction(transaction: SignebleTransaction): Promise<string>;
30
+
31
+ async generateWalletAddress(): Promise<WalletAddress> {
32
+ const randomBytes = crypto.randomBytes(32);
33
+ const result = Keypair.fromRawEd25519Seed(Buffer.from(randomBytes));
34
+
35
+ return {
36
+ address: result.publicKey(),
37
+ secret: result.secret(),
38
+ currency: "xlm",
39
+ network: "stellar",
40
+ raw: result,
41
+ };
42
+ }
43
+ }
44
+
45
+ export class GenericStellar extends Stellar {
46
+ constructor(protected readonly assetAddress: string, test: boolean = false) {
47
+ super(test);
48
+ }
49
+ test = {
50
+ signebleTransaction: {
51
+ fee: 350000,
52
+ items: [
53
+ {
54
+ from: "TKTcGX3tVBk8Y5p5uhFTqcEXT4Tg7C9iTD",
55
+ privateKey:
56
+ "4d6e85f83e2e0e7fd78413e11662323f8bf8daa05089f520ec8dc2bb98996863",
57
+ to: "TRrEckDtWQWGpZHTT477VpAxaq1mFTbas5",
58
+ amount: "230000000000000000",
59
+ },
60
+ ],
61
+ },
62
+ };
63
+
64
+ async signTransaction(transaction: SignebleTransaction): Promise<string> {
65
+ if (transaction.items.length < 0) {
66
+ throw new Error("Incomplete transaction.");
67
+ }
68
+ const result = await Promise.all(
69
+ transaction.items.map(async (item) => {
70
+ const senderAccount = await this.server.loadAccount(item.from);
71
+
72
+ let transactionBuilder = new TransactionBuilder(senderAccount, {
73
+ fee: BASE_FEE,
74
+ networkPassphrase: Networks.FUTURENET,
75
+ }).addOperation(
76
+ Operation.payment({
77
+ destination: item.to,
78
+ asset: Asset.native(),
79
+ amount: item.amount,
80
+ })
81
+ );
82
+
83
+ if (item.extra) {
84
+ if (item.extra.memo) {
85
+ transactionBuilder = transactionBuilder.addMemo(
86
+ Memo.text(item.extra.memo)
87
+ );
88
+ }
89
+ if (item.extra.timeout) {
90
+ transactionBuilder = transactionBuilder.setTimeout(
91
+ item.extra.timeout
92
+ );
93
+ }
94
+ }
95
+
96
+ const senderKeypair = Keypair.fromSecret(item.secret);
97
+ const signedTransaction = transactionBuilder
98
+ .build()
99
+ .sign(senderKeypair);
100
+
101
+ return btoa(JSON.stringify(signedTransaction));
102
+ })
103
+ );
104
+ return btoa(JSON.stringify(result));
105
+ }
106
+ }
107
+
108
+ export class XlmStellar extends Stellar {
109
+ constructor(test: boolean = false) {
110
+ super(test);
111
+ }
112
+ async signTransaction(
113
+ signebleTransaction: SignebleTransaction
114
+ ): Promise<string> {
115
+ const signTransactions = Promise.all(
116
+ signebleTransaction.items.map(async (item) => {
117
+ const account = await this.server.loadAccount(item.from);
118
+ let transactionBuilder = new TransactionBuilder(account, {
119
+ fee: BASE_FEE,
120
+ networkPassphrase: Networks.FUTURENET,
121
+ }).addOperation(
122
+ Operation.payment({
123
+ destination: item.to,
124
+ asset: Asset.native(),
125
+ amount: item.amount,
126
+ })
127
+ );
128
+ if (item.extra) {
129
+ if (item.extra.memo) {
130
+ transactionBuilder = transactionBuilder.addMemo(
131
+ Memo.text(item.extra.memo)
132
+ );
133
+ }
134
+ if (item.extra.timeout) {
135
+ transactionBuilder = transactionBuilder.setTimeout(
136
+ item.extra.timeout
137
+ );
138
+ }
139
+ }
140
+
141
+ const signedTransaction = transactionBuilder
142
+ .build()
143
+ .sign(Keypair.fromSecret(item.secret));
144
+ return btoa(JSON.stringify(signedTransaction));
145
+ })
146
+ );
147
+ return btoa(JSON.stringify(signTransactions));
148
+ }
149
+ }
@@ -0,0 +1,96 @@
1
+ import { TronWeb, utils, Contract } from "tronweb";
2
+ import { SignebleTransaction } from "../../transaction";
3
+ import { CurrencyNetworkModule } from "../../currency-network-module";
4
+ import { WalletAddress } from "../../walletaddress";
5
+
6
+ export abstract class Tron implements CurrencyNetworkModule {
7
+ readonly tronWeb: TronWeb;
8
+
9
+ constructor(test: boolean) {
10
+ this.tronWeb = new TronWeb({
11
+ fullHost: test
12
+ ? "https://api.shasta.trongrid.io"
13
+ : "https://api.trongrid.io",
14
+ });
15
+ }
16
+
17
+ readonly network = "tron";
18
+
19
+ async generateWalletAddress(): Promise<WalletAddress> {
20
+ const result = utils.accounts.generateAccount();
21
+ return {
22
+ address: result.address.base58,
23
+ secret: result.privateKey,
24
+ currency: "trx",
25
+ network: "tron",
26
+ raw: result,
27
+ };
28
+ }
29
+
30
+ abstract signTransaction(transaction: SignebleTransaction): Promise<string>;
31
+ }
32
+
33
+ const functionSelector = "transfer(address, uint256)";
34
+ export class GenericTron extends Tron {
35
+ usdtContract: Contract | null = null;
36
+ constructor(public readonly contractAddress: string, test: boolean = false) {
37
+ super(test);
38
+ }
39
+
40
+ async signTransaction(transaction: SignebleTransaction): Promise<string> {
41
+ if (this.usdtContract === null) {
42
+ this.usdtContract = await this.tronWeb
43
+ .contract()
44
+ .at(this.contractAddress);
45
+ }
46
+ if (transaction.items.length < 0) {
47
+ throw new Error("Incomplete transaction.");
48
+ }
49
+ const result = await Promise.all(
50
+ transaction.items.map(async (item) => {
51
+ const from = item.from;
52
+ const parameter = [
53
+ { type: "address", value: item.to },
54
+ { type: "uint256", value: item.amount },
55
+ ];
56
+ const usdttransaction =
57
+ await this.tronWeb.transactionBuilder.triggerSmartContract(
58
+ this.contractAddress,
59
+ functionSelector,
60
+ { feeLimit: transaction.fee },
61
+ parameter,
62
+ from
63
+ );
64
+
65
+ const signedTransaction = utils.crypto.signTransaction(
66
+ item.secret,
67
+ usdttransaction.transaction
68
+ );
69
+ return btoa(JSON.stringify(signedTransaction));
70
+ })
71
+ );
72
+ return btoa(JSON.stringify(result));
73
+ }
74
+ }
75
+
76
+ export class TrxTron extends Tron {
77
+ constructor(test: boolean = false) {
78
+ super(test);
79
+ }
80
+
81
+ async signTransaction(
82
+ signebleTransaction: SignebleTransaction
83
+ ): Promise<string> {
84
+ const result = await Promise.all(
85
+ signebleTransaction.items.map(async (item) => {
86
+ const transaction = await this.tronWeb.transactionBuilder.sendTrx(
87
+ item.to,
88
+ parseInt(item.amount),
89
+ item.from
90
+ );
91
+ return this.tronWeb.trx.sign(transaction, item.secret);
92
+ })
93
+ );
94
+ return btoa(JSON.stringify(result));
95
+ }
96
+ }
package/src/network.ts ADDED
@@ -0,0 +1,4 @@
1
+ export interface Network {
2
+ name: string;
3
+ status: string;
4
+ }