@deserialize/multi-vm-wallet 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.
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/utils/IChainWallet.d.ts +16 -0
- package/dist/utils/IChainWallet.js +22 -0
- package/dist/utils/bip32.d.ts +11 -0
- package/dist/utils/bip32.js +99 -0
- package/dist/utils/evm/evm.d.ts +30 -0
- package/dist/utils/evm/evm.js +72 -0
- package/dist/utils/evm/index.d.ts +2 -0
- package/dist/utils/evm/index.js +18 -0
- package/dist/utils/evm/utils.d.ts +92 -0
- package/dist/utils/evm/utils.js +346 -0
- package/dist/utils/index.d.ts +6 -0
- package/dist/utils/index.js +22 -0
- package/dist/utils/svm/index.d.ts +1 -0
- package/dist/utils/svm/index.js +17 -0
- package/dist/utils/svm/svm.d.ts +24 -0
- package/dist/utils/svm/svm.js +71 -0
- package/dist/utils/svm/transactionSender.d.ts +8 -0
- package/dist/utils/svm/transactionSender.js +83 -0
- package/dist/utils/svm/utils.d.ts +26 -0
- package/dist/utils/svm/utils.js +161 -0
- package/dist/utils/types.d.ts +44 -0
- package/dist/utils/types.js +9 -0
- package/dist/utils/vm.d.ts +13 -0
- package/dist/utils/vm.js +49 -0
- package/package.json +42 -0
- package/tsconfig.json +115 -0
- package/utils/IChainWallet.ts +36 -0
- package/utils/bip32.ts +66 -0
- package/utils/evm/evm.ts +84 -0
- package/utils/evm/index.ts +2 -0
- package/utils/evm/utils.ts +504 -0
- package/utils/index.ts +6 -0
- package/utils/svm/index.js +17 -0
- package/utils/svm/index.ts +1 -0
- package/utils/svm/svm.js +71 -0
- package/utils/svm/svm.ts +75 -0
- package/utils/svm/transactionSender.js +83 -0
- package/utils/svm/transactionSender.ts +108 -0
- package/utils/svm/utils.js +161 -0
- package/utils/svm/utils.ts +203 -0
- package/utils/types.ts +53 -0
- package/utils/vm.ts +26 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Account } from "@solana/spl-token";
|
|
2
|
+
import { Connection, Keypair, PublicKey, TransactionInstruction, VersionedTransaction } from "@solana/web3.js";
|
|
3
|
+
import { TokenInfo } from "../types";
|
|
4
|
+
export declare const createV0Transaction: (connection: Connection, inX: TransactionInstruction[], signers: Keypair[], payerPubKey: PublicKey, blockHash?: string) => Promise<VersionedTransaction>;
|
|
5
|
+
export declare const createAtaAndIx: (token: PublicKey, ownerPublicKey: PublicKey, tokenProgramId: PublicKey, connection: Connection) => Promise<{
|
|
6
|
+
AtaTokenIx: any;
|
|
7
|
+
associatedToken: PublicKey;
|
|
8
|
+
}>;
|
|
9
|
+
export declare const getSureAssociatedTokenAddressAndAccount: (connection: Connection, token: PublicKey, owner: PublicKey) => Promise<{
|
|
10
|
+
ATA: PublicKey;
|
|
11
|
+
programId: PublicKey;
|
|
12
|
+
tokenAccount: Account;
|
|
13
|
+
}>;
|
|
14
|
+
export declare const getProgramIdOfToken: (owner: PublicKey, token: PublicKey, connection: Connection) => Promise<PublicKey>;
|
|
15
|
+
export declare const getSvmNativeBalance: (address: PublicKey, connection: Connection) => Promise<{
|
|
16
|
+
balance: import("bn.js");
|
|
17
|
+
formatted: number;
|
|
18
|
+
decimal: number;
|
|
19
|
+
}>;
|
|
20
|
+
export declare const getTokenBalance: (address: PublicKey, token: PublicKey, connection: Connection) => Promise<0 | import("@solana/web3.js").TokenAmount>;
|
|
21
|
+
export declare const getTokenAccountAccount: (token: PublicKey, address: PublicKey, connection: Connection) => Promise<Account | null>;
|
|
22
|
+
export declare const getTransferNativeInx: (from: PublicKey, to: PublicKey, amount: number) => Promise<TransactionInstruction>;
|
|
23
|
+
export declare const getTransferNativeTransaction: (from: Keypair, to: PublicKey, amount: number, connection: Connection) => Promise<VersionedTransaction>;
|
|
24
|
+
export declare const getTransferTokenInx: (from: PublicKey, to: PublicKey, token: TokenInfo, amount: number, connection: Connection) => Promise<TransactionInstruction[]>;
|
|
25
|
+
export declare const getTransferTokenTransaction: (from: Keypair, to: PublicKey, token: TokenInfo, amount: number, connection: Connection) => Promise<VersionedTransaction>;
|
|
26
|
+
export declare const signAndSendTransaction: (transaction: VersionedTransaction, connection: Connection, signers: Keypair[]) => Promise<string>;
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
//we will write all the svm utils function here
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.signAndSendTransaction = exports.getTransferTokenTransaction = exports.getTransferTokenInx = exports.getTransferNativeTransaction = exports.getTransferNativeInx = exports.getTokenAccountAccount = exports.getTokenBalance = exports.getSvmNativeBalance = exports.getProgramIdOfToken = exports.getSureAssociatedTokenAddressAndAccount = exports.createAtaAndIx = exports.createV0Transaction = void 0;
|
|
5
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
6
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
7
|
+
const transactionSender_1 = require("./transactionSender");
|
|
8
|
+
const bn_js_1 = require("bn.js");
|
|
9
|
+
const createV0Transaction = async (connection, inX, signers, payerPubKey, blockHash) => {
|
|
10
|
+
const blockhash = blockHash || (await connection.getLatestBlockhash()).blockhash;
|
|
11
|
+
const message = new web3_js_1.TransactionMessage({
|
|
12
|
+
payerKey: payerPubKey,
|
|
13
|
+
instructions: inX,
|
|
14
|
+
recentBlockhash: blockhash,
|
|
15
|
+
}).compileToV0Message();
|
|
16
|
+
const transaction = new web3_js_1.VersionedTransaction(message);
|
|
17
|
+
transaction.message.staticAccountKeys;
|
|
18
|
+
if (signers.length < 1) {
|
|
19
|
+
transaction.sign(signers);
|
|
20
|
+
}
|
|
21
|
+
return transaction;
|
|
22
|
+
};
|
|
23
|
+
exports.createV0Transaction = createV0Transaction;
|
|
24
|
+
const createAtaAndIx = async (token, ownerPublicKey, tokenProgramId, connection) => {
|
|
25
|
+
let AtaTokenIx;
|
|
26
|
+
const associatedToken = (0, spl_token_1.getAssociatedTokenAddressSync)(token, ownerPublicKey, false, tokenProgramId);
|
|
27
|
+
const accountExist = await connection.getAccountInfo(associatedToken);
|
|
28
|
+
if (!accountExist) {
|
|
29
|
+
AtaTokenIx = (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(ownerPublicKey, associatedToken, ownerPublicKey, token, tokenProgramId);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
AtaTokenIx,
|
|
33
|
+
associatedToken,
|
|
34
|
+
};
|
|
35
|
+
};
|
|
36
|
+
exports.createAtaAndIx = createAtaAndIx;
|
|
37
|
+
const getSureAssociatedTokenAddressAndAccount = async (connection, token, owner) => {
|
|
38
|
+
let ATA;
|
|
39
|
+
let programId;
|
|
40
|
+
let tokenAccount;
|
|
41
|
+
try {
|
|
42
|
+
programId = token.equals(spl_token_1.NATIVE_MINT)
|
|
43
|
+
? spl_token_1.TOKEN_PROGRAM_ID
|
|
44
|
+
: spl_token_1.TOKEN_2022_PROGRAM_ID;
|
|
45
|
+
ATA = (0, spl_token_1.getAssociatedTokenAddressSync)(token, owner, true, programId);
|
|
46
|
+
tokenAccount = await (0, spl_token_1.getAccount)(connection, ATA, "confirmed", programId);
|
|
47
|
+
return { ATA, programId, tokenAccount };
|
|
48
|
+
}
|
|
49
|
+
catch (error) {
|
|
50
|
+
programId = spl_token_1.TOKEN_PROGRAM_ID;
|
|
51
|
+
ATA = (0, spl_token_1.getAssociatedTokenAddressSync)(token, owner, true, programId);
|
|
52
|
+
tokenAccount = await (0, spl_token_1.getAccount)(connection, ATA, "confirmed", programId);
|
|
53
|
+
return { ATA, programId, tokenAccount };
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
exports.getSureAssociatedTokenAddressAndAccount = getSureAssociatedTokenAddressAndAccount;
|
|
57
|
+
const getProgramIdOfToken = async (owner, token, connection) => {
|
|
58
|
+
if (token.equals(spl_token_1.NATIVE_MINT)) {
|
|
59
|
+
return spl_token_1.TOKEN_PROGRAM_ID;
|
|
60
|
+
}
|
|
61
|
+
let ATA;
|
|
62
|
+
let programId = spl_token_1.TOKEN_PROGRAM_ID;
|
|
63
|
+
let tokenAccount;
|
|
64
|
+
try {
|
|
65
|
+
ATA = (0, spl_token_1.getAssociatedTokenAddressSync)(token, owner, true, programId);
|
|
66
|
+
tokenAccount = await (0, spl_token_1.getAccount)(connection, ATA, "confirmed", programId);
|
|
67
|
+
return spl_token_1.TOKEN_PROGRAM_ID;
|
|
68
|
+
}
|
|
69
|
+
catch (error) {
|
|
70
|
+
return spl_token_1.TOKEN_2022_PROGRAM_ID;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
exports.getProgramIdOfToken = getProgramIdOfToken;
|
|
74
|
+
//get native balance
|
|
75
|
+
const getSvmNativeBalance = async (address, connection) => {
|
|
76
|
+
const balance = await connection.getBalance(address);
|
|
77
|
+
return { balance: new bn_js_1.BN(balance), formatted: balance / web3_js_1.LAMPORTS_PER_SOL, decimal: 9 };
|
|
78
|
+
};
|
|
79
|
+
exports.getSvmNativeBalance = getSvmNativeBalance;
|
|
80
|
+
const getTokenBalance = async (address, token, connection) => {
|
|
81
|
+
try {
|
|
82
|
+
// Get the balance from the token account
|
|
83
|
+
const tokenAccount = await (0, exports.getTokenAccountAccount)(token, address, connection);
|
|
84
|
+
if (!tokenAccount) {
|
|
85
|
+
console.error("Token account not found");
|
|
86
|
+
return 0;
|
|
87
|
+
}
|
|
88
|
+
const tokenBalance = await connection.getTokenAccountBalance(tokenAccount.address);
|
|
89
|
+
if (!tokenBalance) {
|
|
90
|
+
console.error("Token balance not found");
|
|
91
|
+
return 0;
|
|
92
|
+
}
|
|
93
|
+
return tokenBalance.value;
|
|
94
|
+
}
|
|
95
|
+
catch (error) {
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
exports.getTokenBalance = getTokenBalance;
|
|
100
|
+
const getTokenAccountAccount = async (token, address, connection) => {
|
|
101
|
+
try {
|
|
102
|
+
// Get the associated token account address for the user and the token mint
|
|
103
|
+
const associatedTokenAccount = await (0, spl_token_1.getAssociatedTokenAddress)(token, // The token mint address
|
|
104
|
+
address // The user's public key
|
|
105
|
+
);
|
|
106
|
+
// Fetch the token account information
|
|
107
|
+
const tokenAccount = await (0, spl_token_1.getAccount)(connection, associatedTokenAccount);
|
|
108
|
+
return tokenAccount;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
console.error("Error getting token balance:");
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
exports.getTokenAccountAccount = getTokenAccountAccount;
|
|
116
|
+
const getTransferNativeInx = async (from, to, amount) => {
|
|
117
|
+
return web3_js_1.SystemProgram.transfer({
|
|
118
|
+
fromPubkey: from,
|
|
119
|
+
toPubkey: to,
|
|
120
|
+
lamports: amount * web3_js_1.LAMPORTS_PER_SOL, // Convert SOL to lamports
|
|
121
|
+
});
|
|
122
|
+
};
|
|
123
|
+
exports.getTransferNativeInx = getTransferNativeInx;
|
|
124
|
+
const getTransferNativeTransaction = async (from, to, amount, connection) => {
|
|
125
|
+
const instruction = await (0, exports.getTransferNativeInx)(from.publicKey, to, amount);
|
|
126
|
+
const transaction = await (0, exports.createV0Transaction)(connection, [instruction], [from], from.publicKey);
|
|
127
|
+
return transaction;
|
|
128
|
+
};
|
|
129
|
+
exports.getTransferNativeTransaction = getTransferNativeTransaction;
|
|
130
|
+
const getTransferTokenInx = async (from, to, token, amount, connection) => {
|
|
131
|
+
const inx = [];
|
|
132
|
+
const tokenToSend = new web3_js_1.PublicKey(token.address);
|
|
133
|
+
const { ATA: source, programId, tokenAccount } = await (0, exports.getSureAssociatedTokenAddressAndAccount)(connection, from, tokenToSend);
|
|
134
|
+
const { associatedToken: destination, AtaTokenIx } = await (0, exports.createAtaAndIx)(tokenToSend, to, programId, connection);
|
|
135
|
+
if (!tokenAccount) {
|
|
136
|
+
throw new Error("Token account not found");
|
|
137
|
+
}
|
|
138
|
+
if (AtaTokenIx) {
|
|
139
|
+
inx.push(AtaTokenIx);
|
|
140
|
+
}
|
|
141
|
+
const tInx = (0, spl_token_1.createTransferCheckedInstruction)(source, tokenToSend, destination, from, amount, token.decimals, undefined, programId);
|
|
142
|
+
inx.push(tInx);
|
|
143
|
+
return inx;
|
|
144
|
+
};
|
|
145
|
+
exports.getTransferTokenInx = getTransferTokenInx;
|
|
146
|
+
const getTransferTokenTransaction = async (from, to, token, amount, connection) => {
|
|
147
|
+
const instruction = await (0, exports.getTransferTokenInx)(from.publicKey, to, token, amount, connection);
|
|
148
|
+
const transaction = await (0, exports.createV0Transaction)(connection, instruction, [from], from.publicKey);
|
|
149
|
+
return transaction;
|
|
150
|
+
};
|
|
151
|
+
exports.getTransferTokenTransaction = getTransferTokenTransaction;
|
|
152
|
+
const signAndSendTransaction = async (transaction, connection, signers) => {
|
|
153
|
+
transaction.sign(signers);
|
|
154
|
+
const blockhash = await connection.getLatestBlockhash();
|
|
155
|
+
const res = await (0, transactionSender_1.transactionSenderAndConfirmationWaiter)({ connection, serializedTransaction: Buffer.from(transaction.serialize()), blockhashWithExpiryBlockHeight: { blockhash: blockhash.blockhash, lastValidBlockHeight: blockhash.lastValidBlockHeight } });
|
|
156
|
+
if (!res) {
|
|
157
|
+
throw new Error("Transaction failed to send or confirm");
|
|
158
|
+
}
|
|
159
|
+
return res.transaction.signatures[0];
|
|
160
|
+
};
|
|
161
|
+
exports.signAndSendTransaction = signAndSendTransaction;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import BN from "bn.js";
|
|
2
|
+
import { EVMVM } from "./evm";
|
|
3
|
+
import { SVMVM } from "./svm";
|
|
4
|
+
export interface ChainWalletConfig {
|
|
5
|
+
chainId: string | number;
|
|
6
|
+
name: string;
|
|
7
|
+
rpcUrl: string;
|
|
8
|
+
explorerUrl: string;
|
|
9
|
+
nativeToken: {
|
|
10
|
+
name: string;
|
|
11
|
+
symbol: string;
|
|
12
|
+
decimals: number;
|
|
13
|
+
};
|
|
14
|
+
confirmationNo?: number;
|
|
15
|
+
testnet?: boolean;
|
|
16
|
+
}
|
|
17
|
+
export interface TokenInfo {
|
|
18
|
+
address: string;
|
|
19
|
+
name: string;
|
|
20
|
+
symbol: string;
|
|
21
|
+
decimals: number;
|
|
22
|
+
}
|
|
23
|
+
export interface NFTInfo {
|
|
24
|
+
tokenId: string;
|
|
25
|
+
contractAddress: string;
|
|
26
|
+
name?: string;
|
|
27
|
+
description?: string;
|
|
28
|
+
image?: string;
|
|
29
|
+
}
|
|
30
|
+
export interface TransactionResult {
|
|
31
|
+
hash: string;
|
|
32
|
+
success: boolean;
|
|
33
|
+
error?: string;
|
|
34
|
+
}
|
|
35
|
+
export interface Balance {
|
|
36
|
+
balance: BN;
|
|
37
|
+
formatted: number;
|
|
38
|
+
decimal: number;
|
|
39
|
+
}
|
|
40
|
+
export declare const SUPPORTED_VM: {
|
|
41
|
+
readonly EVM: typeof EVMVM;
|
|
42
|
+
readonly SVM: typeof SVMVM;
|
|
43
|
+
};
|
|
44
|
+
export type vmTypes = keyof typeof SUPPORTED_VM;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { vmTypes } from "./types";
|
|
2
|
+
export declare abstract class VM<AddressType, PrivateKeyType, ConnectionType> {
|
|
3
|
+
protected mnemonic: string;
|
|
4
|
+
protected seed: string;
|
|
5
|
+
type: vmTypes;
|
|
6
|
+
constructor(mnemonic: string, vm: vmTypes);
|
|
7
|
+
mnemonicToSeed: (mnemonic: string) => string;
|
|
8
|
+
abstract derivationPath: string;
|
|
9
|
+
abstract generatePrivateKey(index: number, mnemonic?: string, derivationPath?: string): {
|
|
10
|
+
privateKey: PrivateKeyType;
|
|
11
|
+
index: number;
|
|
12
|
+
};
|
|
13
|
+
}
|
package/dist/utils/vm.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.VM = void 0;
|
|
37
|
+
const bip39 = __importStar(require("bip39"));
|
|
38
|
+
// Abstract Base Classes
|
|
39
|
+
class VM {
|
|
40
|
+
constructor(mnemonic, vm) {
|
|
41
|
+
this.mnemonicToSeed = (mnemonic) => {
|
|
42
|
+
return bip39.mnemonicToSeedSync(mnemonic).toString("hex");
|
|
43
|
+
};
|
|
44
|
+
this.mnemonic = mnemonic;
|
|
45
|
+
this.type = vm;
|
|
46
|
+
this.seed = this.mnemonicToSeed(this.mnemonic);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
exports.VM = VM;
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@deserialize/multi-vm-wallet",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"devDependencies": {
|
|
5
|
+
"@types/bn.js": "^5.2.0",
|
|
6
|
+
"@types/node": "^24.1.0",
|
|
7
|
+
"@types/promise-retry": "^1.1.6"
|
|
8
|
+
},
|
|
9
|
+
"dependencies": {
|
|
10
|
+
"@solana/spl-token": "^0.4.13",
|
|
11
|
+
"@solana/web3.js": "^1.98.4",
|
|
12
|
+
"bip32": "^4.0.0",
|
|
13
|
+
"bip39": "^3.1.0",
|
|
14
|
+
"bn.js": "^5.2.2",
|
|
15
|
+
"ed25519-hd-key": "^1.3.0",
|
|
16
|
+
"ethers": "^6.15.0",
|
|
17
|
+
"promise-retry": "^2.0.1",
|
|
18
|
+
"tiny-secp256k1": "^2.2.4"
|
|
19
|
+
},
|
|
20
|
+
"description": "Multi VM Wallet",
|
|
21
|
+
"main": "dist/index.js",
|
|
22
|
+
"types": "dist/index.d.ts",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
25
|
+
"build": "sudo rm -r dist ; tsc -p tsconfig.json",
|
|
26
|
+
"publish:sdk": "npm run build && npm publish --access=public"
|
|
27
|
+
},
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "git+https://github.com/Bravark/deserialize-swap-sdk.git"
|
|
31
|
+
},
|
|
32
|
+
"keywords": [
|
|
33
|
+
"wallet",
|
|
34
|
+
"metamask"
|
|
35
|
+
],
|
|
36
|
+
"author": "0xazach",
|
|
37
|
+
"license": "MIT",
|
|
38
|
+
"bugs": {
|
|
39
|
+
"url": "https://github.com/Bravark/deserialize-swap-sdk/issues"
|
|
40
|
+
},
|
|
41
|
+
"homepage": "https://github.com/Bravark/deserialize-swap-sdk#readme"
|
|
42
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
/* Visit https://aka.ms/tsconfig to read more about this file */
|
|
4
|
+
|
|
5
|
+
/* Projects */
|
|
6
|
+
// "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
|
|
7
|
+
"composite": true /* Enable constraints that allow a TypeScript project to be used with project references. */,
|
|
8
|
+
// "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
|
|
9
|
+
// "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
|
|
10
|
+
// "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
|
|
11
|
+
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
|
12
|
+
|
|
13
|
+
/* Language and Environment */
|
|
14
|
+
"target": "es2020" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
|
|
15
|
+
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
|
16
|
+
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
|
17
|
+
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
|
18
|
+
// "emitDecoratorMetadata": true, /* Emit design-type metadata for decorated declarations in source files. */
|
|
19
|
+
// "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
|
|
20
|
+
// "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
|
|
21
|
+
// "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
|
|
22
|
+
// "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
|
|
23
|
+
// "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
|
|
24
|
+
// "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
|
|
25
|
+
// "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
|
|
26
|
+
|
|
27
|
+
/* Modules */
|
|
28
|
+
"module": "commonjs" /* Specify what module code is generated. */,
|
|
29
|
+
// "rootDir": "./", /* Specify the root folder within your source files. */
|
|
30
|
+
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
|
|
31
|
+
"baseUrl": "./" /* Specify the base directory to resolve non-relative module names. */,
|
|
32
|
+
// "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
|
|
33
|
+
// "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
|
|
34
|
+
"typeRoots": [
|
|
35
|
+
"./node_modules/@types",
|
|
36
|
+
"./src/types"
|
|
37
|
+
] /* Specify multiple folders that act like './node_modules/@types'. */,
|
|
38
|
+
// "types": [], /* Specify type package names to be included without being referenced in a source file. */
|
|
39
|
+
// "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
|
|
40
|
+
// "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
|
|
41
|
+
// "allowImportingTsExtensions": true, /* Allow imports to include TypeScript file extensions. Requires '--moduleResolution bundler' and either '--noEmit' or '--emitDeclarationOnly' to be set. */
|
|
42
|
+
// "rewriteRelativeImportExtensions": true, /* Rewrite '.ts', '.tsx', '.mts', and '.cts' file extensions in relative import paths to their JavaScript equivalent in output files. */
|
|
43
|
+
// "resolvePackageJsonExports": true, /* Use the package.json 'exports' field when resolving package imports. */
|
|
44
|
+
// "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */
|
|
45
|
+
// "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */
|
|
46
|
+
// "noUncheckedSideEffectImports": true, /* Check side effect imports. */
|
|
47
|
+
"resolveJsonModule": true /* Enable importing .json files. */,
|
|
48
|
+
// "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */
|
|
49
|
+
// "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
|
|
50
|
+
|
|
51
|
+
/* JavaScript Support */
|
|
52
|
+
"allowJs": true /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */,
|
|
53
|
+
"checkJs": false /* Enable error reporting in type-checked JavaScript files. */,
|
|
54
|
+
// "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
|
|
55
|
+
|
|
56
|
+
/* Emit */
|
|
57
|
+
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
|
|
58
|
+
// "declarationMap": true, /* Create sourcemaps for d.ts files. */
|
|
59
|
+
// "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
|
|
60
|
+
// "sourceMap": true, /* Create source map files for emitted JavaScript files. */
|
|
61
|
+
// "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
|
|
62
|
+
// "noEmit": true, /* Disable emitting files from a compilation. */
|
|
63
|
+
// "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
|
|
64
|
+
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
|
|
65
|
+
// "removeComments": true, /* Disable emitting comments. */
|
|
66
|
+
// "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
|
|
67
|
+
// "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
|
|
68
|
+
// "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
|
|
69
|
+
// "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
|
|
70
|
+
// "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
|
|
71
|
+
// "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
|
|
72
|
+
// "newLine": "crlf", /* Set the newline character for emitting files. */
|
|
73
|
+
// "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
|
|
74
|
+
// "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
|
|
75
|
+
// "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
|
|
76
|
+
// "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
|
|
77
|
+
// "declarationDir": "./", /* Specify the output directory for generated declaration files. */
|
|
78
|
+
|
|
79
|
+
/* Interop Constraints */
|
|
80
|
+
// "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
|
|
81
|
+
// "verbatimModuleSyntax": true, /* Do not transform or elide any imports or exports not marked as type-only, ensuring they are written in the output file's format based on the 'module' setting. */
|
|
82
|
+
// "isolatedDeclarations": true, /* Require sufficient annotation on exports so other tools can trivially generate declaration files. */
|
|
83
|
+
// "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
|
|
84
|
+
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
|
|
85
|
+
"preserveSymlinks": true /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */,
|
|
86
|
+
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
|
|
87
|
+
|
|
88
|
+
/* Type Checking */
|
|
89
|
+
// "strict": true /* Enable all strict type-checking options. */,
|
|
90
|
+
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
|
|
91
|
+
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
|
|
92
|
+
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
|
|
93
|
+
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
|
|
94
|
+
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
|
|
95
|
+
// "strictBuiltinIteratorReturn": true, /* Built-in iterators are instantiated with a 'TReturn' type of 'undefined' instead of 'any'. */
|
|
96
|
+
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
|
|
97
|
+
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
|
|
98
|
+
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
|
|
99
|
+
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
|
|
100
|
+
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
|
|
101
|
+
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
|
|
102
|
+
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
|
|
103
|
+
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
|
|
104
|
+
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
|
|
105
|
+
// "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
|
|
106
|
+
// "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
|
|
107
|
+
// "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
|
|
108
|
+
// "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
|
|
109
|
+
|
|
110
|
+
/* Completeness */
|
|
111
|
+
// "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
|
|
112
|
+
"skipLibCheck": true /* Skip type checking all .d.ts files. */
|
|
113
|
+
},
|
|
114
|
+
"exclude": ["node_modules", "dist"]
|
|
115
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Balance, ChainWalletConfig, NFTInfo, TokenInfo, TransactionResult } from "./types";
|
|
2
|
+
|
|
3
|
+
export abstract class ChainWallet<AddressType, PrivateKeyType, ConnectionType> {
|
|
4
|
+
protected privateKey: PrivateKeyType;
|
|
5
|
+
config: ChainWalletConfig;
|
|
6
|
+
address: AddressType;
|
|
7
|
+
index: number | undefined
|
|
8
|
+
connection: ConnectionType | undefined
|
|
9
|
+
|
|
10
|
+
constructor(config: ChainWalletConfig, privateKey: PrivateKeyType, index?: number) {
|
|
11
|
+
this.config = config;
|
|
12
|
+
this.privateKey = privateKey;
|
|
13
|
+
this.address = this.generateAddress(privateKey);
|
|
14
|
+
this.index = index;
|
|
15
|
+
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
abstract generateAddress(privateKey: PrivateKeyType): AddressType;
|
|
19
|
+
abstract getNativeBalance(): Promise<Balance>;
|
|
20
|
+
abstract getTokenBalance(tokenAddress: AddressType): Promise<Balance>;
|
|
21
|
+
abstract transferNative(to: AddressType, amount: number): Promise<TransactionResult>;
|
|
22
|
+
abstract transferToken(tokenAddress: TokenInfo, to: AddressType, amount: number): Promise<TransactionResult>;
|
|
23
|
+
// abstract transferNFT(contractAddress: AddressType, tokenId: string, to: string): Promise<TransactionResult>;
|
|
24
|
+
// abstract getTokenInfo(tokenAddress: string): Promise<TokenInfo>;
|
|
25
|
+
// abstract getNFTInfo(contractAddress: string, tokenId: string): Promise<NFTInfo>;
|
|
26
|
+
// abstract getTransactionHistory(): Promise<any[]>;
|
|
27
|
+
|
|
28
|
+
getAddress(): AddressType {
|
|
29
|
+
return this.address;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
getChainWalletConfig(): ChainWalletConfig {
|
|
33
|
+
return this.config;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
}
|
package/utils/bip32.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import * as bip39 from "bip39";
|
|
2
|
+
import BIP32Factory from "bip32";
|
|
3
|
+
import * as ecc from "tiny-secp256k1";
|
|
4
|
+
import { BIP32Interface } from "bip32";
|
|
5
|
+
import * as ed25519 from "ed25519-hd-key";
|
|
6
|
+
import { Keypair } from "@solana/web3.js";
|
|
7
|
+
|
|
8
|
+
const bip32 = BIP32Factory(ecc);
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
// export function getPublicKeyFromSeed(seed: string, index: number) {
|
|
12
|
+
// const node = getSeedNode(seed);
|
|
13
|
+
// const { publicKey } = deriveChildPrivateKey(node, index);
|
|
14
|
+
// return publicKey;
|
|
15
|
+
// }
|
|
16
|
+
// export function getPrivateKeyFromSeed(seed: string, index: number) {
|
|
17
|
+
// const node = getSeedNode(seed);
|
|
18
|
+
// const keyPair = deriveChildPrivateKey(node, index);
|
|
19
|
+
// return keyPair.privateKey;
|
|
20
|
+
// }
|
|
21
|
+
|
|
22
|
+
export function GenerateNewMnemonic() {
|
|
23
|
+
const mnemonic = bip39.generateMnemonic();
|
|
24
|
+
return mnemonic;
|
|
25
|
+
}
|
|
26
|
+
export function ValidateMnemonic(mnemonic: string) {
|
|
27
|
+
const isValid = bip39.validateMnemonic(mnemonic);
|
|
28
|
+
if (!isValid) {
|
|
29
|
+
throw new Error("Invalid mnemonic");
|
|
30
|
+
}
|
|
31
|
+
return isValid;
|
|
32
|
+
}
|
|
33
|
+
export function GenerateSeed(_mnemonic?: string) {
|
|
34
|
+
const mnemonic = _mnemonic || bip39.generateMnemonic();
|
|
35
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
36
|
+
const seedString = seed.toString("hex");
|
|
37
|
+
return seedString;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function getSeedNode(seed: string) {
|
|
41
|
+
const bip32 = BIP32Factory(ecc);
|
|
42
|
+
const restoredSeedBuffer = Buffer.from(seed, "hex");
|
|
43
|
+
const node: BIP32Interface = bip32.fromSeed(restoredSeedBuffer);
|
|
44
|
+
return node;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
//EVM
|
|
49
|
+
export function EVMDeriveChildPrivateKey(seed: string, index: number, derivationPath: string) {
|
|
50
|
+
const node = getSeedNode(seed);
|
|
51
|
+
const child = node.derivePath(`${derivationPath}${index}'`);
|
|
52
|
+
const privateKey = child.privateKey!.toString("hex");
|
|
53
|
+
const publicKey = child.publicKey.toString("hex");
|
|
54
|
+
return { privateKey, publicKey };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
//SVM
|
|
60
|
+
export function SVMDeriveChildPrivateKey(seed: string, index: number, derivationPath: string) {
|
|
61
|
+
const path = `${derivationPath}${index}'`;
|
|
62
|
+
// Derive a seed from the given path
|
|
63
|
+
const derivedSeed = ed25519.derivePath(path, seed).key;
|
|
64
|
+
const derivedKeyPair = Keypair.fromSeed(derivedSeed);
|
|
65
|
+
return derivedKeyPair;
|
|
66
|
+
}
|