@vleap/warps-adapter-near 0.1.0-beta.3 → 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.mjs CHANGED
@@ -900,83 +900,65 @@ var WarpNearExplorer = class {
900
900
  };
901
901
 
902
902
  // src/WarpNearWallet.ts
903
- import * as bip39 from "@scure/bip39";
904
- import { wordlist } from "@scure/bip39/wordlists/english.js";
905
903
  import {
906
904
  getProviderConfig as getProviderConfig4,
907
- getWarpWalletMnemonicFromConfig,
908
- getWarpWalletPrivateKeyFromConfig
905
+ initializeWalletCache
909
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";
910
914
  import bs58 from "bs58";
911
- import { connect as connect4, KeyPair, keyStores as keyStores4 } from "near-api-js";
912
- var WarpNearWallet = class {
915
+ import { KeyPair } from "near-api-js";
916
+ var MnemonicWalletProvider = class {
913
917
  constructor(config, chain) {
914
918
  this.config = config;
915
919
  this.chain = chain;
916
- const providerConfig = getProviderConfig4(config, chain.name, config.env, chain.defaultApiUrl);
917
- this.nearConfig = {
918
- networkId: this.config.env === "mainnet" ? "mainnet" : this.config.env === "testnet" ? "testnet" : "testnet",
919
- nodeUrl: providerConfig.url,
920
- keyStore: new keyStores4.InMemoryKeyStore()
921
- };
920
+ this.keypair = null;
922
921
  }
923
- async signTransaction(tx) {
924
- if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
925
- const keyPair = this.getKeyPair();
926
- const accountId = this.getAddress();
927
- if (!accountId) throw new Error("No account ID available");
928
- await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
929
- const near = await connect4(this.nearConfig);
930
- const account = await near.account(accountId);
931
- if (tx.signature) {
932
- 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;
933
931
  }
934
- const signedTx = await account.signAndSendTransaction({
935
- receiverId: tx.receiverId,
936
- actions: tx.actions
937
- });
938
- return {
939
- ...tx,
940
- signature: signedTx.transaction.hash,
941
- transactionHash: signedTx.transaction.hash
942
- };
943
932
  }
944
- async signTransactions(txs) {
945
- if (txs.length === 0) return [];
946
- 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;
947
945
  }
948
946
  async signMessage(message) {
949
- const keyPair = this.getKeyPair();
947
+ const keypair = this.getKeyPair();
950
948
  const messageBytes = new TextEncoder().encode(message);
951
- const signature = keyPair.sign(messageBytes);
949
+ const signature = keypair.sign(messageBytes);
952
950
  return bs58.encode(signature.signature);
953
951
  }
954
- async sendTransaction(tx) {
955
- if (!tx || typeof tx !== "object") throw new Error("Invalid transaction object");
956
- const keyPair = this.getKeyPair();
957
- const accountId = this.getAddress();
958
- if (!accountId) throw new Error("No account ID available");
959
- await this.nearConfig.keyStore.setKey(this.nearConfig.networkId, accountId, keyPair);
960
- const near = await connect4(this.nearConfig);
961
- const account = await near.account(accountId);
962
- if (tx.transactionHash) {
963
- return tx.transactionHash;
964
- }
965
- const result = await account.signAndSendTransaction({
966
- receiverId: tx.receiverId,
967
- actions: tx.actions
968
- });
969
- return result.transaction.hash;
970
- }
971
- async sendTransactions(txs) {
972
- return Promise.all(txs.map(async (tx) => this.sendTransaction(tx)));
952
+ getKeyPairInstance() {
953
+ return this.getKeyPair();
973
954
  }
974
955
  create(mnemonic) {
975
956
  const seed = bip39.mnemonicToSeedSync(mnemonic);
976
957
  const keyPair = KeyPair.fromRandom("ed25519");
977
958
  const publicKey = keyPair.getPublicKey();
978
- const accountId = publicKey.toString().split(":")[1] || publicKey.toString();
959
+ const accountId = keyToImplicitAddress(publicKey.toString());
979
960
  return {
961
+ provider: "mnemonic",
980
962
  address: accountId,
981
963
  privateKey: keyPair.toString(),
982
964
  mnemonic
@@ -987,77 +969,213 @@ var WarpNearWallet = class {
987
969
  const seed = bip39.mnemonicToSeedSync(mnemonic);
988
970
  const keyPair = KeyPair.fromRandom("ed25519");
989
971
  const publicKey = keyPair.getPublicKey();
990
- const accountId = publicKey.toString().split(":")[1] || publicKey.toString();
972
+ const accountId = keyToImplicitAddress(publicKey.toString());
991
973
  return {
974
+ provider: "mnemonic",
992
975
  address: accountId,
993
976
  privateKey: keyPair.toString(),
994
977
  mnemonic
995
978
  };
996
979
  }
997
- 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;
998
1006
  try {
999
- const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
1000
- if (privateKey) {
1001
- try {
1002
- const keyPair = KeyPair.fromString(privateKey);
1003
- const publicKey = keyPair.getPublicKey();
1004
- return publicKey.toString().split(":")[1] || publicKey.toString();
1005
- } catch {
1006
- return null;
1007
- }
1008
- }
1009
- const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
1010
- if (mnemonic) {
1011
- const seed = bip39.mnemonicToSeedSync(mnemonic);
1012
- const keyPair = KeyPair.fromRandom("ed25519");
1013
- const publicKey = keyPair.getPublicKey();
1014
- return publicKey.toString().split(":")[1] || publicKey.toString();
1015
- }
1016
- const wallet = this.config.user?.wallets?.[this.chain.name];
1017
- if (wallet && typeof wallet === "object" && "address" in wallet) {
1018
- return wallet.address;
1019
- }
1020
- if (wallet && typeof wallet === "string") {
1021
- return wallet;
1022
- }
1023
- return null;
1007
+ const keypair = this.getKeyPair();
1008
+ const publicKey = keypair.getPublicKey();
1009
+ return keyToImplicitAddress2(publicKey.toString());
1024
1010
  } catch {
1025
1011
  return null;
1026
1012
  }
1027
1013
  }
1028
- getPublicKey() {
1014
+ async getPublicKey() {
1029
1015
  try {
1030
- const keyPair = this.getKeyPair();
1031
- const publicKey = keyPair.getPublicKey();
1016
+ const keypair = this.getKeyPair();
1017
+ const publicKey = keypair.getPublicKey();
1032
1018
  return publicKey.toString();
1033
1019
  } catch {
1034
1020
  return null;
1035
1021
  }
1036
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
+ }
1037
1061
  getKeyPair() {
1062
+ if (this.keypair) return this.keypair;
1038
1063
  const privateKey = getWarpWalletPrivateKeyFromConfig(this.config, this.chain.name);
1039
- if (privateKey) {
1040
- try {
1041
- try {
1042
- return KeyPair.fromString(privateKey);
1043
- } catch {
1044
- }
1045
- const keyPair = KeyPair.fromRandom("ed25519");
1046
- return keyPair;
1047
- } catch (error) {
1048
- if (error instanceof Error) {
1049
- throw new Error(`Invalid private key format: ${error.message}`);
1050
- }
1051
- 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}`);
1052
1070
  }
1071
+ throw new Error("Invalid private key format");
1053
1072
  }
1054
- const mnemonic = getWarpWalletMnemonicFromConfig(this.config, this.chain.name);
1055
- if (mnemonic) {
1056
- const seed = bip39.mnemonicToSeedSync(mnemonic);
1057
- const keyPair = KeyPair.fromRandom("ed25519");
1058
- return keyPair;
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
+ };
1059
1131
  }
1060
- throw new Error("No private key or mnemonic provided");
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;
1061
1179
  }
1062
1180
  };
1063
1181
 
@@ -1090,7 +1208,7 @@ var NativeTokenNear = {
1090
1208
  decimals: 24,
1091
1209
  logoUrl: "https://joai.ai/images/tokens/near-black.svg"
1092
1210
  };
1093
- var getNearAdapter = createNearAdapter("near", {
1211
+ var NearAdapter = createNearAdapter("near", {
1094
1212
  mainnet: {
1095
1213
  name: "near",
1096
1214
  displayName: "NEAR Mainnet",
@@ -1126,6 +1244,7 @@ export {
1126
1244
  ExplorerUrls,
1127
1245
  KnownTokens,
1128
1246
  NativeTokenNear,
1247
+ NearAdapter,
1129
1248
  NearExplorerMap,
1130
1249
  NearExplorerNames,
1131
1250
  NearExplorerUrls,
@@ -1139,7 +1258,6 @@ export {
1139
1258
  WarpNearSerializer,
1140
1259
  WarpNearWallet,
1141
1260
  findKnownTokenById,
1142
- getKnownTokensForChain,
1143
- getNearAdapter
1261
+ getKnownTokensForChain
1144
1262
  };
1145
1263
  //# sourceMappingURL=index.mjs.map