@vleap/warps-adapter-near 0.1.0-beta.4 → 0.1.0-beta.6

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.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
- getWarpWalletMnemonicFromConfig,
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 { connect as connect4, KeyPair, keyStores as keyStores4 } from "near-api-js";
913
- var WarpNearWallet = class {
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
- const providerConfig = getProviderConfig4(config, chain.name, config.env, chain.defaultApiUrl);
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 signTransaction(tx) {
925
- if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
926
- const keyPair = this.getKeyPair();
927
- const accountId = this.getAddress();
928
- if (!accountId) throw new Error("No account ID available");
929
- await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
930
- const near = await connect4(this.nearConfig);
931
- const account = await near.account(accountId);
932
- if (tx.signature) {
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 signTransactions(txs) {
946
- if (txs.length === 0) return [];
947
- return Promise.all(txs.map(async (tx) => this.signTransaction(tx)));
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 keyPair = this.getKeyPair();
947
+ const keypair = this.getKeyPair();
951
948
  const messageBytes = new TextEncoder().encode(message);
952
- const signature = keyPair.sign(messageBytes);
949
+ const signature = keypair.sign(messageBytes);
953
950
  return bs58.encode(signature.signature);
954
951
  }
955
- async sendTransaction(tx) {
956
- if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
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,250 @@ 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
- getAddress() {
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 wallet = this.config.user?.wallets?.[this.chain.name];
1001
- if (wallet && typeof wallet === "object" && "address" in wallet) {
1002
- return wallet.address;
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 keyPair = this.getKeyPair();
1032
- const publicKey = keyPair.getPublicKey();
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
- try {
1042
- try {
1043
- return KeyPair.fromString(privateKey);
1044
- } catch {
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
- const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
1056
- if (mnemonic) {
1057
- const seed = bip39.mnemonicToSeedSync(mnemonic);
1058
- const keyPair = KeyPair.fromRandom("ed25519");
1059
- return keyPair;
1073
+ }
1074
+ };
1075
+
1076
+ // src/providers/ReadOnlyWalletProvider.ts
1077
+ import { getWarpWalletAddressFromConfig as getWarpWalletAddressFromConfig5 } from "@vleap/warps";
1078
+ var ReadOnlyWalletProvider = class {
1079
+ constructor(config, chain) {
1080
+ this.config = config;
1081
+ this.chain = chain;
1082
+ }
1083
+ async getAddress() {
1084
+ return getWarpWalletAddressFromConfig5(this.config, this.chain.name);
1085
+ }
1086
+ async getPublicKey() {
1087
+ return null;
1088
+ }
1089
+ async signTransaction(tx) {
1090
+ const address = await this.getAddress();
1091
+ throw new Error(`Wallet can not be used for signing: ${address}`);
1092
+ }
1093
+ async signMessage(message) {
1094
+ const address = await this.getAddress();
1095
+ throw new Error(`Wallet can not be used for signing: ${address}`);
1096
+ }
1097
+ create(mnemonic) {
1098
+ const address = getWarpWalletAddressFromConfig5(this.config, this.chain.name);
1099
+ throw new Error(`Wallet can not be used for signing: ${address}`);
1100
+ }
1101
+ generate() {
1102
+ const address = getWarpWalletAddressFromConfig5(this.config, this.chain.name);
1103
+ throw new Error(`Wallet can not be used for signing: ${address}`);
1104
+ }
1105
+ };
1106
+
1107
+ // src/WarpNearWallet.ts
1108
+ var WarpNearWallet = class {
1109
+ constructor(config, chain) {
1110
+ this.config = config;
1111
+ this.chain = chain;
1112
+ this.cachedAddress = null;
1113
+ this.cachedPublicKey = null;
1114
+ const providerConfig = getProviderConfig4(config, chain.name, config.env, chain.defaultApiUrl);
1115
+ this.nearConfig = {
1116
+ networkId: this.config.env === "mainnet" ? "mainnet" : this.config.env === "testnet" ? "testnet" : "testnet",
1117
+ nodeUrl: providerConfig.url,
1118
+ keyStore: new keyStores4.InMemoryKeyStore()
1119
+ };
1120
+ this.walletProvider = this.createProvider();
1121
+ this.initializeCache();
1122
+ }
1123
+ createProvider() {
1124
+ const wallet = this.config.user?.wallets?.[this.chain.name];
1125
+ if (!wallet) return null;
1126
+ if (typeof wallet === "string") return new ReadOnlyWalletProvider(this.config, this.chain);
1127
+ const customWalletProviders = this.config.walletProviders?.[this.chain.name];
1128
+ const providerFactory = customWalletProviders?.[wallet.provider];
1129
+ if (providerFactory) return providerFactory(this.config, this.chain);
1130
+ if (wallet.provider === "privateKey") return new PrivateKeyWalletProvider(this.config, this.chain);
1131
+ if (wallet.provider === "mnemonic") return new MnemonicWalletProvider(this.config, this.chain);
1132
+ throw new Error(`Unsupported wallet provider for ${this.chain.name}: ${wallet.provider}`);
1133
+ }
1134
+ initializeCache() {
1135
+ initializeWalletCache(this.walletProvider).then((cache) => {
1136
+ this.cachedAddress = cache.address;
1137
+ this.cachedPublicKey = cache.publicKey;
1138
+ });
1139
+ }
1140
+ async signTransaction(tx) {
1141
+ if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
1142
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1143
+ if (this.walletProvider instanceof ReadOnlyWalletProvider) throw new Error(`Wallet (${this.chain.name}) is read-only`);
1144
+ const accountId = this.getAddress();
1145
+ if (!accountId) throw new Error("No account ID available");
1146
+ if (this.walletProvider instanceof PrivateKeyWalletProvider || this.walletProvider instanceof MnemonicWalletProvider) {
1147
+ const keyPair = this.walletProvider.getKeyPairInstance();
1148
+ await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
1149
+ const near = await connect4(this.nearConfig);
1150
+ const account = await near.account(accountId);
1151
+ if (tx.signature) {
1152
+ return tx;
1153
+ }
1154
+ const signedTx = await account.signAndSendTransaction({
1155
+ receiverId: tx.receiverId,
1156
+ actions: tx.actions
1157
+ });
1158
+ return {
1159
+ ...tx,
1160
+ signature: signedTx.transaction.hash,
1161
+ transactionHash: signedTx.transaction.hash
1162
+ };
1163
+ }
1164
+ throw new Error("Wallet provider does not support signing transactions");
1165
+ }
1166
+ async signTransactions(txs) {
1167
+ if (txs.length === 0) return [];
1168
+ const signedTxs = [];
1169
+ for (const tx of txs) {
1170
+ signedTxs.push(await this.signTransaction(tx));
1060
1171
  }
1061
- throw new Error("No private key or mnemonic provided");
1172
+ return signedTxs;
1173
+ }
1174
+ async signMessage(message) {
1175
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1176
+ if (this.walletProvider instanceof ReadOnlyWalletProvider) throw new Error(`Wallet (${this.chain.name}) is read-only`);
1177
+ return await this.walletProvider.signMessage(message);
1178
+ }
1179
+ async sendTransaction(tx) {
1180
+ if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
1181
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1182
+ const accountId = this.getAddress();
1183
+ if (!accountId) throw new Error("No account ID available");
1184
+ if (tx.transactionHash) {
1185
+ return tx.transactionHash;
1186
+ }
1187
+ if (this.walletProvider instanceof PrivateKeyWalletProvider || this.walletProvider instanceof MnemonicWalletProvider) {
1188
+ const keyPair = this.walletProvider.getKeyPairInstance();
1189
+ await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
1190
+ const near = await connect4(this.nearConfig);
1191
+ const account = await near.account(accountId);
1192
+ const result = await account.signAndSendTransaction({
1193
+ receiverId: tx.receiverId,
1194
+ actions: tx.actions
1195
+ });
1196
+ return result.transaction.hash;
1197
+ }
1198
+ throw new Error("Wallet provider does not support sending transactions");
1199
+ }
1200
+ async sendTransactions(txs) {
1201
+ return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
1202
+ }
1203
+ create(mnemonic) {
1204
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1205
+ if (this.walletProvider instanceof ReadOnlyWalletProvider) throw new Error(`Wallet (${this.chain.name}) is read-only`);
1206
+ return this.walletProvider.create(mnemonic);
1207
+ }
1208
+ generate() {
1209
+ if (!this.walletProvider) throw new Error("No wallet provider available");
1210
+ if (this.walletProvider instanceof ReadOnlyWalletProvider) throw new Error(`Wallet (${this.chain.name}) is read-only`);
1211
+ return this.walletProvider.generate();
1212
+ }
1213
+ getAddress() {
1214
+ return this.cachedAddress;
1215
+ }
1216
+ getPublicKey() {
1217
+ return this.cachedPublicKey;
1062
1218
  }
1063
1219
  };
1064
1220
 
@@ -1091,7 +1247,7 @@ var NativeTokenNear = {
1091
1247
  decimals: 24,
1092
1248
  logoUrl: "https://joai.ai/images/tokens/near-black.svg"
1093
1249
  };
1094
- var getNearAdapter = createNearAdapter("near", {
1250
+ var NearAdapter = createNearAdapter("near", {
1095
1251
  mainnet: {
1096
1252
  name: "near",
1097
1253
  displayName: "NEAR Mainnet",
@@ -1127,6 +1283,7 @@ export {
1127
1283
  ExplorerUrls,
1128
1284
  KnownTokens,
1129
1285
  NativeTokenNear,
1286
+ NearAdapter,
1130
1287
  NearExplorerMap,
1131
1288
  NearExplorerNames,
1132
1289
  NearExplorerUrls,
@@ -1140,7 +1297,6 @@ export {
1140
1297
  WarpNearSerializer,
1141
1298
  WarpNearWallet,
1142
1299
  findKnownTokenById,
1143
- getKnownTokensForChain,
1144
- getNearAdapter
1300
+ getKnownTokensForChain
1145
1301
  };
1146
1302
  //# sourceMappingURL=index.mjs.map