@vleap/warps-adapter-near 0.1.0-beta.4 → 0.1.0-beta.5
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/index.d.cts +10 -14
- package/dist/index.d.ts +10 -14
- package/dist/index.js +222 -104
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +224 -107
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -900,77 +900,57 @@ var WarpNearExplorer = class {
|
|
|
900
900
|
};
|
|
901
901
|
|
|
902
902
|
// src/WarpNearWallet.ts
|
|
903
|
-
import { keyToImplicitAddress } from "@near-js/crypto";
|
|
904
|
-
import * as bip39 from "@scure/bip39";
|
|
905
|
-
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
906
903
|
import {
|
|
907
904
|
getProviderConfig as getProviderConfig4,
|
|
908
|
-
|
|
909
|
-
getWarpWalletPrivateKeyFromConfig
|
|
905
|
+
initializeWalletCache
|
|
910
906
|
} from "@vleap/warps";
|
|
907
|
+
import { connect as connect4, keyStores as keyStores4 } from "near-api-js";
|
|
908
|
+
|
|
909
|
+
// src/providers/MnemonicWalletProvider.ts
|
|
910
|
+
import { keyToImplicitAddress } from "@near-js/crypto";
|
|
911
|
+
import * as bip39 from "@scure/bip39";
|
|
912
|
+
import { wordlist } from "@scure/bip39/wordlists/english.js";
|
|
913
|
+
import { getWarpWalletAddressFromConfig as getWarpWalletAddressFromConfig3, getWarpWalletMnemonicFromConfig } from "@vleap/warps";
|
|
911
914
|
import bs58 from "bs58";
|
|
912
|
-
import {
|
|
913
|
-
var
|
|
915
|
+
import { KeyPair } from "near-api-js";
|
|
916
|
+
var MnemonicWalletProvider = class {
|
|
914
917
|
constructor(config, chain) {
|
|
915
918
|
this.config = config;
|
|
916
919
|
this.chain = chain;
|
|
917
|
-
|
|
918
|
-
this.nearConfig = {
|
|
919
|
-
networkId: this.config.env === "mainnet" ? "mainnet" : this.config.env === "testnet" ? "testnet" : "testnet",
|
|
920
|
-
nodeUrl: providerConfig.url,
|
|
921
|
-
keyStore: new keyStores4.InMemoryKeyStore()
|
|
922
|
-
};
|
|
920
|
+
this.keypair = null;
|
|
923
921
|
}
|
|
924
|
-
async
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
return tx;
|
|
922
|
+
async getAddress() {
|
|
923
|
+
const address = getWarpWalletAddressFromConfig3(this.config, this.chain.name);
|
|
924
|
+
if (address) return address;
|
|
925
|
+
try {
|
|
926
|
+
const keypair = this.getKeyPair();
|
|
927
|
+
const publicKey = keypair.getPublicKey();
|
|
928
|
+
return keyToImplicitAddress(publicKey.toString());
|
|
929
|
+
} catch {
|
|
930
|
+
return null;
|
|
934
931
|
}
|
|
935
|
-
const signedTx = await account.signAndSendTransaction({
|
|
936
|
-
receiverId: tx.receiverId,
|
|
937
|
-
actions: tx.actions
|
|
938
|
-
});
|
|
939
|
-
return {
|
|
940
|
-
...tx,
|
|
941
|
-
signature: signedTx.transaction.hash,
|
|
942
|
-
transactionHash: signedTx.transaction.hash
|
|
943
|
-
};
|
|
944
932
|
}
|
|
945
|
-
async
|
|
946
|
-
|
|
947
|
-
|
|
933
|
+
async getPublicKey() {
|
|
934
|
+
try {
|
|
935
|
+
const keypair = this.getKeyPair();
|
|
936
|
+
const publicKey = keypair.getPublicKey();
|
|
937
|
+
return publicKey.toString();
|
|
938
|
+
} catch {
|
|
939
|
+
return null;
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
async signTransaction(tx) {
|
|
943
|
+
const keypair = this.getKeyPair();
|
|
944
|
+
return tx;
|
|
948
945
|
}
|
|
949
946
|
async signMessage(message) {
|
|
950
|
-
const
|
|
947
|
+
const keypair = this.getKeyPair();
|
|
951
948
|
const messageBytes = new TextEncoder().encode(message);
|
|
952
|
-
const signature =
|
|
949
|
+
const signature = keypair.sign(messageBytes);
|
|
953
950
|
return bs58.encode(signature.signature);
|
|
954
951
|
}
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
const keyPair = this.getKeyPair();
|
|
958
|
-
const accountId = this.getAddress();
|
|
959
|
-
if (!accountId) throw new Error("No account ID available");
|
|
960
|
-
await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
|
|
961
|
-
const near = await connect4(this.nearConfig);
|
|
962
|
-
const account = await near.account(accountId);
|
|
963
|
-
if (tx.transactionHash) {
|
|
964
|
-
return tx.transactionHash;
|
|
965
|
-
}
|
|
966
|
-
const result = await account.signAndSendTransaction({
|
|
967
|
-
receiverId: tx.receiverId,
|
|
968
|
-
actions: tx.actions
|
|
969
|
-
});
|
|
970
|
-
return result.transaction.hash;
|
|
971
|
-
}
|
|
972
|
-
async sendTransactions(txs) {
|
|
973
|
-
return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
|
|
952
|
+
getKeyPairInstance() {
|
|
953
|
+
return this.getKeyPair();
|
|
974
954
|
}
|
|
975
955
|
create(mnemonic) {
|
|
976
956
|
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
@@ -978,6 +958,7 @@ var WarpNearWallet = class {
|
|
|
978
958
|
const publicKey = keyPair.getPublicKey();
|
|
979
959
|
const accountId = keyToImplicitAddress(publicKey.toString());
|
|
980
960
|
return {
|
|
961
|
+
provider: "mnemonic",
|
|
981
962
|
address: accountId,
|
|
982
963
|
privateKey: keyPair.toString(),
|
|
983
964
|
mnemonic
|
|
@@ -990,75 +971,211 @@ var WarpNearWallet = class {
|
|
|
990
971
|
const publicKey = keyPair.getPublicKey();
|
|
991
972
|
const accountId = keyToImplicitAddress(publicKey.toString());
|
|
992
973
|
return {
|
|
974
|
+
provider: "mnemonic",
|
|
993
975
|
address: accountId,
|
|
994
976
|
privateKey: keyPair.toString(),
|
|
995
977
|
mnemonic
|
|
996
978
|
};
|
|
997
979
|
}
|
|
998
|
-
|
|
980
|
+
getKeyPair() {
|
|
981
|
+
if (this.keypair) return this.keypair;
|
|
982
|
+
const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
|
|
983
|
+
if (!mnemonic) throw new Error("No mnemonic provided");
|
|
984
|
+
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
985
|
+
this.keypair = KeyPair.fromRandom("ed25519");
|
|
986
|
+
return this.keypair;
|
|
987
|
+
}
|
|
988
|
+
};
|
|
989
|
+
|
|
990
|
+
// src/providers/PrivateKeyWalletProvider.ts
|
|
991
|
+
import { keyToImplicitAddress as keyToImplicitAddress2 } from "@near-js/crypto";
|
|
992
|
+
import * as bip392 from "@scure/bip39";
|
|
993
|
+
import { wordlist as wordlist2 } from "@scure/bip39/wordlists/english.js";
|
|
994
|
+
import { getWarpWalletAddressFromConfig as getWarpWalletAddressFromConfig4, getWarpWalletPrivateKeyFromConfig } from "@vleap/warps";
|
|
995
|
+
import bs582 from "bs58";
|
|
996
|
+
import { KeyPair as KeyPair2 } from "near-api-js";
|
|
997
|
+
var PrivateKeyWalletProvider = class {
|
|
998
|
+
constructor(config, chain) {
|
|
999
|
+
this.config = config;
|
|
1000
|
+
this.chain = chain;
|
|
1001
|
+
this.keypair = null;
|
|
1002
|
+
}
|
|
1003
|
+
async getAddress() {
|
|
1004
|
+
const address = getWarpWalletAddressFromConfig4(this.config, this.chain.name);
|
|
1005
|
+
if (address) return address;
|
|
999
1006
|
try {
|
|
1000
|
-
const
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
}
|
|
1004
|
-
if (wallet && typeof wallet === "string") {
|
|
1005
|
-
return wallet;
|
|
1006
|
-
}
|
|
1007
|
-
const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
|
|
1008
|
-
if (privateKey) {
|
|
1009
|
-
try {
|
|
1010
|
-
const keyPair = KeyPair.fromString(privateKey);
|
|
1011
|
-
const publicKey = keyPair.getPublicKey();
|
|
1012
|
-
return keyToImplicitAddress(publicKey.toString());
|
|
1013
|
-
} catch {
|
|
1014
|
-
return null;
|
|
1015
|
-
}
|
|
1016
|
-
}
|
|
1017
|
-
const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
|
|
1018
|
-
if (mnemonic) {
|
|
1019
|
-
const seed = bip39.mnemonicToSeedSync(mnemonic);
|
|
1020
|
-
const keyPair = KeyPair.fromRandom("ed25519");
|
|
1021
|
-
const publicKey = keyPair.getPublicKey();
|
|
1022
|
-
return keyToImplicitAddress(publicKey.toString());
|
|
1023
|
-
}
|
|
1024
|
-
return null;
|
|
1007
|
+
const keypair = this.getKeyPair();
|
|
1008
|
+
const publicKey = keypair.getPublicKey();
|
|
1009
|
+
return keyToImplicitAddress2(publicKey.toString());
|
|
1025
1010
|
} catch {
|
|
1026
1011
|
return null;
|
|
1027
1012
|
}
|
|
1028
1013
|
}
|
|
1029
|
-
getPublicKey() {
|
|
1014
|
+
async getPublicKey() {
|
|
1030
1015
|
try {
|
|
1031
|
-
const
|
|
1032
|
-
const publicKey =
|
|
1016
|
+
const keypair = this.getKeyPair();
|
|
1017
|
+
const publicKey = keypair.getPublicKey();
|
|
1033
1018
|
return publicKey.toString();
|
|
1034
1019
|
} catch {
|
|
1035
1020
|
return null;
|
|
1036
1021
|
}
|
|
1037
1022
|
}
|
|
1023
|
+
async signTransaction(tx) {
|
|
1024
|
+
const keypair = this.getKeyPair();
|
|
1025
|
+
return tx;
|
|
1026
|
+
}
|
|
1027
|
+
async signMessage(message) {
|
|
1028
|
+
const keypair = this.getKeyPair();
|
|
1029
|
+
const messageBytes = new TextEncoder().encode(message);
|
|
1030
|
+
const signature = keypair.sign(messageBytes);
|
|
1031
|
+
return bs582.encode(signature.signature);
|
|
1032
|
+
}
|
|
1033
|
+
getKeyPairInstance() {
|
|
1034
|
+
return this.getKeyPair();
|
|
1035
|
+
}
|
|
1036
|
+
create(mnemonic) {
|
|
1037
|
+
const seed = bip392.mnemonicToSeedSync(mnemonic);
|
|
1038
|
+
const keyPair = KeyPair2.fromRandom("ed25519");
|
|
1039
|
+
const publicKey = keyPair.getPublicKey();
|
|
1040
|
+
const accountId = keyToImplicitAddress2(publicKey.toString());
|
|
1041
|
+
return {
|
|
1042
|
+
provider: "privateKey",
|
|
1043
|
+
address: accountId,
|
|
1044
|
+
privateKey: keyPair.toString(),
|
|
1045
|
+
mnemonic
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
generate() {
|
|
1049
|
+
const mnemonic = bip392.generateMnemonic(wordlist2);
|
|
1050
|
+
const seed = bip392.mnemonicToSeedSync(mnemonic);
|
|
1051
|
+
const keyPair = KeyPair2.fromRandom("ed25519");
|
|
1052
|
+
const publicKey = keyPair.getPublicKey();
|
|
1053
|
+
const accountId = keyToImplicitAddress2(publicKey.toString());
|
|
1054
|
+
return {
|
|
1055
|
+
provider: "privateKey",
|
|
1056
|
+
address: accountId,
|
|
1057
|
+
privateKey: keyPair.toString(),
|
|
1058
|
+
mnemonic
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1038
1061
|
getKeyPair() {
|
|
1062
|
+
if (this.keypair) return this.keypair;
|
|
1039
1063
|
const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
|
|
1040
|
-
if (privateKey)
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
}
|
|
1046
|
-
const keyPair = KeyPair.fromRandom("ed25519");
|
|
1047
|
-
return keyPair;
|
|
1048
|
-
} catch (error) {
|
|
1049
|
-
if (error instanceof Error) {
|
|
1050
|
-
throw new Error(`Invalid private key format: ${error.message}`);
|
|
1051
|
-
}
|
|
1052
|
-
throw new Error("Invalid private key format");
|
|
1064
|
+
if (!privateKey) throw new Error("No private key provided");
|
|
1065
|
+
try {
|
|
1066
|
+
return KeyPair2.fromString(privateKey);
|
|
1067
|
+
} catch (error) {
|
|
1068
|
+
if (error instanceof Error) {
|
|
1069
|
+
throw new Error(`Invalid private key format: ${error.message}`);
|
|
1053
1070
|
}
|
|
1071
|
+
throw new Error("Invalid private key format");
|
|
1054
1072
|
}
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1073
|
+
}
|
|
1074
|
+
};
|
|
1075
|
+
|
|
1076
|
+
// src/WarpNearWallet.ts
|
|
1077
|
+
var WarpNearWallet = class {
|
|
1078
|
+
constructor(config, chain) {
|
|
1079
|
+
this.config = config;
|
|
1080
|
+
this.chain = chain;
|
|
1081
|
+
this.cachedAddress = null;
|
|
1082
|
+
this.cachedPublicKey = null;
|
|
1083
|
+
const providerConfig = getProviderConfig4(config, chain.name, config.env, chain.defaultApiUrl);
|
|
1084
|
+
this.nearConfig = {
|
|
1085
|
+
networkId: this.config.env === "mainnet" ? "mainnet" : this.config.env === "testnet" ? "testnet" : "testnet",
|
|
1086
|
+
nodeUrl: providerConfig.url,
|
|
1087
|
+
keyStore: new keyStores4.InMemoryKeyStore()
|
|
1088
|
+
};
|
|
1089
|
+
this.walletProvider = this.createProvider();
|
|
1090
|
+
this.initializeCache();
|
|
1091
|
+
}
|
|
1092
|
+
createProvider() {
|
|
1093
|
+
const wallet = this.config.user?.wallets?.[this.chain.name];
|
|
1094
|
+
if (!wallet) return null;
|
|
1095
|
+
if (typeof wallet === "string") throw new Error(`Wallet can not be used for signing: ${wallet}`);
|
|
1096
|
+
const customWalletProviders = this.config.walletProviders?.[this.chain.name];
|
|
1097
|
+
const providerFactory = customWalletProviders?.[wallet.provider];
|
|
1098
|
+
if (providerFactory) return providerFactory(this.config, this.chain);
|
|
1099
|
+
if (wallet.provider === "privateKey") return new PrivateKeyWalletProvider(this.config, this.chain);
|
|
1100
|
+
if (wallet.provider === "mnemonic") return new MnemonicWalletProvider(this.config, this.chain);
|
|
1101
|
+
throw new Error(`Unsupported wallet provider for ${this.chain.name}: ${wallet.provider}`);
|
|
1102
|
+
}
|
|
1103
|
+
initializeCache() {
|
|
1104
|
+
initializeWalletCache(this.walletProvider).then((cache) => {
|
|
1105
|
+
this.cachedAddress = cache.address;
|
|
1106
|
+
this.cachedPublicKey = cache.publicKey;
|
|
1107
|
+
});
|
|
1108
|
+
}
|
|
1109
|
+
async signTransaction(tx) {
|
|
1110
|
+
if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
|
|
1111
|
+
if (!this.walletProvider) throw new Error("No wallet provider available");
|
|
1112
|
+
const accountId = this.getAddress();
|
|
1113
|
+
if (!accountId) throw new Error("No account ID available");
|
|
1114
|
+
if (this.walletProvider instanceof PrivateKeyWalletProvider || this.walletProvider instanceof MnemonicWalletProvider) {
|
|
1115
|
+
const keyPair = this.walletProvider.getKeyPairInstance();
|
|
1116
|
+
await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
|
|
1117
|
+
const near = await connect4(this.nearConfig);
|
|
1118
|
+
const account = await near.account(accountId);
|
|
1119
|
+
if (tx.signature) {
|
|
1120
|
+
return tx;
|
|
1121
|
+
}
|
|
1122
|
+
const signedTx = await account.signAndSendTransaction({
|
|
1123
|
+
receiverId: tx.receiverId,
|
|
1124
|
+
actions: tx.actions
|
|
1125
|
+
});
|
|
1126
|
+
return {
|
|
1127
|
+
...tx,
|
|
1128
|
+
signature: signedTx.transaction.hash,
|
|
1129
|
+
transactionHash: signedTx.transaction.hash
|
|
1130
|
+
};
|
|
1060
1131
|
}
|
|
1061
|
-
throw new Error("
|
|
1132
|
+
throw new Error("Wallet provider does not support signing transactions");
|
|
1133
|
+
}
|
|
1134
|
+
async signTransactions(txs) {
|
|
1135
|
+
if (txs.length === 0) return [];
|
|
1136
|
+
return Promise.all(txs.map(async (tx) => this.signTransaction(tx)));
|
|
1137
|
+
}
|
|
1138
|
+
async signMessage(message) {
|
|
1139
|
+
if (!this.walletProvider) throw new Error("No wallet provider available");
|
|
1140
|
+
return await this.walletProvider.signMessage(message);
|
|
1141
|
+
}
|
|
1142
|
+
async sendTransaction(tx) {
|
|
1143
|
+
if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
|
|
1144
|
+
if (!this.walletProvider) throw new Error("No wallet provider available");
|
|
1145
|
+
const accountId = this.getAddress();
|
|
1146
|
+
if (!accountId) throw new Error("No account ID available");
|
|
1147
|
+
if (tx.transactionHash) {
|
|
1148
|
+
return tx.transactionHash;
|
|
1149
|
+
}
|
|
1150
|
+
if (this.walletProvider instanceof PrivateKeyWalletProvider || this.walletProvider instanceof MnemonicWalletProvider) {
|
|
1151
|
+
const keyPair = this.walletProvider.getKeyPairInstance();
|
|
1152
|
+
await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
|
|
1153
|
+
const near = await connect4(this.nearConfig);
|
|
1154
|
+
const account = await near.account(accountId);
|
|
1155
|
+
const result = await account.signAndSendTransaction({
|
|
1156
|
+
receiverId: tx.receiverId,
|
|
1157
|
+
actions: tx.actions
|
|
1158
|
+
});
|
|
1159
|
+
return result.transaction.hash;
|
|
1160
|
+
}
|
|
1161
|
+
throw new Error("Wallet provider does not support sending transactions");
|
|
1162
|
+
}
|
|
1163
|
+
async sendTransactions(txs) {
|
|
1164
|
+
return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
|
|
1165
|
+
}
|
|
1166
|
+
create(mnemonic) {
|
|
1167
|
+
if (!this.walletProvider) throw new Error("No wallet provider available");
|
|
1168
|
+
return this.walletProvider.create(mnemonic);
|
|
1169
|
+
}
|
|
1170
|
+
generate() {
|
|
1171
|
+
if (!this.walletProvider) throw new Error("No wallet provider available");
|
|
1172
|
+
return this.walletProvider.generate();
|
|
1173
|
+
}
|
|
1174
|
+
getAddress() {
|
|
1175
|
+
return this.cachedAddress;
|
|
1176
|
+
}
|
|
1177
|
+
getPublicKey() {
|
|
1178
|
+
return this.cachedPublicKey;
|
|
1062
1179
|
}
|
|
1063
1180
|
};
|
|
1064
1181
|
|
|
@@ -1091,7 +1208,7 @@ var NativeTokenNear = {
|
|
|
1091
1208
|
decimals: 24,
|
|
1092
1209
|
logoUrl: "https://joai.ai/images/tokens/near-black.svg"
|
|
1093
1210
|
};
|
|
1094
|
-
var
|
|
1211
|
+
var NearAdapter = createNearAdapter("near", {
|
|
1095
1212
|
mainnet: {
|
|
1096
1213
|
name: "near",
|
|
1097
1214
|
displayName: "NEAR Mainnet",
|
|
@@ -1127,6 +1244,7 @@ export {
|
|
|
1127
1244
|
ExplorerUrls,
|
|
1128
1245
|
KnownTokens,
|
|
1129
1246
|
NativeTokenNear,
|
|
1247
|
+
NearAdapter,
|
|
1130
1248
|
NearExplorerMap,
|
|
1131
1249
|
NearExplorerNames,
|
|
1132
1250
|
NearExplorerUrls,
|
|
@@ -1140,7 +1258,6 @@ export {
|
|
|
1140
1258
|
WarpNearSerializer,
|
|
1141
1259
|
WarpNearWallet,
|
|
1142
1260
|
findKnownTokenById,
|
|
1143
|
-
getKnownTokensForChain
|
|
1144
|
-
getNearAdapter
|
|
1261
|
+
getKnownTokensForChain
|
|
1145
1262
|
};
|
|
1146
1263
|
//# sourceMappingURL=index.mjs.map
|