@msafe/sui3-sdk 0.0.22 → 0.0.23

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.cjs CHANGED
@@ -52,6 +52,7 @@ __export(src_exports, {
52
52
  SignatureVerifier: () => SignatureVerifier,
53
53
  TESTNET_RPC_URL: () => TESTNET_RPC_URL,
54
54
  Uint8ArrayToHex: () => Uint8ArrayToHex,
55
+ addPrefix: () => addPrefix,
55
56
  getAllCoins: () => getAllCoins,
56
57
  getMSafeConfig: () => getMSafeConfig,
57
58
  getPublicKeyFromChain: () => getPublicKeyFromChain,
@@ -244,6 +245,12 @@ var Formatter = class {
244
245
  return Coin.isCoin(struct);
245
246
  }
246
247
  };
248
+ function addPrefix(s, prefix) {
249
+ if (s.startsWith(prefix)) {
250
+ return s;
251
+ }
252
+ return prefix + s;
253
+ }
247
254
 
248
255
  // src/utils/crypto.ts
249
256
  var SignatureVerifier = class _SignatureVerifier {
@@ -809,16 +816,13 @@ var BackendImpl = class _BackendImpl {
809
816
  }
810
817
  _token;
811
818
  async authSign(input) {
812
- const res = await import_axios.default.post(`${this.apiURL}/auth/login`, input);
813
- if (res.status !== 200 && res.status !== 201) {
814
- throw new Error(`invalid authSign return: ${res}`);
815
- }
819
+ const res = await this.post(`/auth/login`, input);
816
820
  this._token = res.data.accessToken;
817
821
  return this._token;
818
822
  }
819
823
  async verifyToken(jwt) {
820
824
  try {
821
- const res = await import_axios.default.get(`${this.apiURL}/auth`, { headers: this.headers(jwt) });
825
+ const res = await this.get(`/auth`, { headers: this.headers(jwt) });
822
826
  return res.status === 200;
823
827
  } catch (_) {
824
828
  return false;
@@ -834,13 +838,10 @@ var BackendImpl = class _BackendImpl {
834
838
  const query = {
835
839
  userAddressList: addresses
836
840
  };
837
- const res = await import_axios.default.get(`${this.apiURL}/user/public-keys`, {
841
+ const res = await this.get(`/user/public-keys`, {
838
842
  params: query,
839
843
  headers: this.headers()
840
844
  });
841
- if (res.status !== 200 && res.status !== 201) {
842
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
843
- }
844
845
  return res.data?.map(
845
846
  (publicKeyWithSchema) => publicKeyWithSchema ? import_sui3_utils5.PublicKeySerde.de(publicKeyWithSchema) : void 0
846
847
  );
@@ -849,22 +850,16 @@ var BackendImpl = class _BackendImpl {
849
850
  const q = {
850
851
  msafeAddress
851
852
  };
852
- const res = await import_axios.default.get(`${this.apiURL}/msafe`, {
853
+ const res = await this.get(`/msafe`, {
853
854
  params: q,
854
855
  headers: this.headers()
855
856
  });
856
- if (res.status !== 200 && res.status !== 201) {
857
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
858
- }
859
857
  return _BackendImpl.toMSafeConfig(res.data);
860
858
  }
861
859
  async getUserInfo() {
862
- const userRes = await import_axios.default.get(`${this.apiURL}/user`, {
860
+ const userRes = await this.get(`/user`, {
863
861
  headers: this.headers()
864
862
  });
865
- if (userRes.status !== 200 && userRes.status !== 201) {
866
- throw new Error(`invalid getPublicKeyBatch return: ${userRes}`);
867
- }
868
863
  return userRes.data;
869
864
  }
870
865
  async getOwnedMSafeByStatus(input) {
@@ -875,13 +870,10 @@ var BackendImpl = class _BackendImpl {
875
870
  limit: input.pagination.limit.toString()
876
871
  } : {}
877
872
  };
878
- const res = await import_axios.default.get(`${this.apiURL}/msafe/owned`, {
873
+ const res = await this.get(`/msafe/owned`, {
879
874
  params: q,
880
875
  headers: this.headers()
881
876
  });
882
- if (res.status !== 200 && res.status !== 201) {
883
- throw new Error(`invalid getOwnedMSafeByStatus return: ${res}`);
884
- }
885
877
  return {
886
878
  data: res.data.data.map(_BackendImpl.toMSafeConfig),
887
879
  meta: res.data.meta
@@ -889,78 +881,54 @@ var BackendImpl = class _BackendImpl {
889
881
  }
890
882
  async updateMSafeStatus(input) {
891
883
  const p = input;
892
- const res = await import_axios.default.post(`${this.apiURL}/msafe/status`, p, { headers: this.headers() });
893
- if (res.status !== 200 && res.status !== 201) {
894
- throw new Error(`Invalid updateMSafeStatus return: ${res}`);
895
- }
884
+ await this.post(`/msafe/status`, p, { headers: this.headers() });
896
885
  }
897
886
  async getPendingTransactions(input) {
898
- const res = await import_axios.default.get(`${this.apiURL}/transaction/pending`, {
887
+ const res = await this.get(`/transaction/pending`, {
899
888
  params: input,
900
889
  headers: this.headers()
901
890
  });
902
- if (res.status !== 200 && res.status !== 201) {
903
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
904
- }
905
891
  return res.data;
906
892
  }
907
893
  async getHistoryTransactions(input) {
908
- const res = await import_axios.default.get(`${this.apiURL}/transaction/history`, {
894
+ const res = await this.get(`/transaction/history`, {
909
895
  params: input,
910
896
  headers: this.headers()
911
897
  });
912
- if (res.status !== 200 && res.status !== 201) {
913
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
914
- }
915
898
  return res.data;
916
899
  }
917
900
  async getFutureIntentions(input) {
918
- const res = await import_axios.default.get(`${this.apiURL}/transaction/intention`, {
901
+ const res = await this.get(`/transaction/intention`, {
919
902
  params: input,
920
903
  headers: this.headers()
921
904
  });
922
- if (res.status !== 200 && res.status !== 201) {
923
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
924
- }
925
905
  return res.data;
926
906
  }
927
907
  async getCurrentSequenceNumber(address) {
928
- const res = await import_axios.default.get(`${this.apiURL}/transaction/sn/current`, {
908
+ const res = await this.get(`/transaction/sn/current`, {
929
909
  params: {
930
910
  msafeAddress: address
931
911
  },
932
912
  headers: this.headers()
933
913
  });
934
- if (res.status !== 200 && res.status !== 201) {
935
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
936
- }
937
914
  return res.data;
938
915
  }
939
916
  async getNextSequenceNumber(address) {
940
- const res = await import_axios.default.get(`${this.apiURL}/transaction/sn/next`, {
917
+ const res = await this.get(`/transaction/sn/next`, {
941
918
  params: {
942
919
  msafeAddress: address
943
920
  },
944
921
  headers: this.headers()
945
922
  });
946
- if (res.status !== 200 && res.status !== 201) {
947
- throw new Error(`invalid getNextSequenceNumber return: ${res}`);
948
- }
949
923
  return res.data;
950
924
  }
951
925
  async createMSafeAccount(input) {
952
- const res = await import_axios.default.post(`${this.apiURL}/msafe/create`, input, {
926
+ await this.post(`/msafe/create`, input, {
953
927
  headers: this.headers()
954
928
  });
955
- if (res.status !== 200 && res.status !== 201) {
956
- throw new Error(`invalid createMSafeAccount return: ${res}`);
957
- }
958
929
  }
959
930
  async proposeIntention(input) {
960
- const res = await import_axios.default.post(`${this.apiURL}/transaction/intention`, input, { headers: this.headers() });
961
- if (res.status !== 200 && res.status !== 201) {
962
- throw new Error(`invalid proposeIntention return: ${res}`);
963
- }
931
+ await this.post(`/transaction/intention`, input, { headers: this.headers() });
964
932
  }
965
933
  // TODO later
966
934
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -968,52 +936,30 @@ var BackendImpl = class _BackendImpl {
968
936
  return void 0;
969
937
  }
970
938
  async rejectCurrentTx(input) {
971
- const res = await import_axios.default.post(`${this.apiURL}/transaction/pending/reject`, input, {
939
+ await this.post(`/transaction/pending/reject`, input, {
972
940
  headers: this.headers()
973
941
  });
974
- if (res.status !== 200 && res.status !== 201) {
975
- throw new Error(`invalid voteForTransaction return: ${res}`);
976
- }
977
942
  }
978
943
  async voteForTransaction(input) {
979
- const res = await import_axios.default.post(`${this.apiURL}/transaction/pending/vote`, input, {
944
+ await this.post(`/transaction/pending/vote`, input, {
980
945
  headers: this.headers()
981
946
  });
982
- if (res.status !== 200 && res.status !== 201) {
983
- throw new Error(`invalid voteForTransaction return: ${res}`);
984
- }
985
947
  }
986
948
  async buildNextIntentionAndAddToPending(input) {
987
- const res = await import_axios.default.post(`${this.apiURL}/transaction/pending/build`, input, { headers: this.headers() });
988
- if (res.status !== 200 && res.status !== 201) {
989
- throw new Error(`invalid buildNextIntentionAndAddToPending return: ${res}`);
990
- }
949
+ await this.post(`/transaction/pending/build`, input, { headers: this.headers() });
991
950
  }
992
951
  async skipNextFailedIntention(input) {
993
- const res = await import_axios.default.post(`${this.apiURL}/transaction/pending/skip`, input, { headers: this.headers() });
994
- if (res.status !== 200 && res.status !== 201) {
995
- throw new Error(`invalid skipNextFailedIntention return: ${res}`);
996
- }
952
+ await this.post(`/transaction/pending/skip`, input, { headers: this.headers() });
997
953
  }
998
954
  async getAddressBookEntries(pagination) {
999
- const res = await import_axios.default.get(`${this.apiURL}/address-book`, {
955
+ const res = await this.get(`/address-book`, {
1000
956
  headers: this.headers(),
1001
957
  params: pagination
1002
958
  });
1003
- if (res.status !== 200) {
1004
- throw new Error(`Invalid address-book return: ${res}`);
1005
- }
1006
959
  return res.data;
1007
960
  }
1008
961
  async updateAddressBook(input) {
1009
- const res = await import_axios.default.post(`${this.apiURL}/address-book`, input, { headers: this.headers() });
1010
- if (res.status !== 200 && res.status !== 201) {
1011
- throw new Error(`invalid updateAddressBook return: ${res}`);
1012
- }
1013
- }
1014
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
1015
- async processExecutedTransaction(_digest) {
1016
- return void 0;
962
+ await this.post(`/address-book`, input, { headers: this.headers() });
1017
963
  }
1018
964
  headers(token) {
1019
965
  return { Authorization: `Bearer ${token || this._token}` };
@@ -1031,6 +977,57 @@ var BackendImpl = class _BackendImpl {
1031
977
  }))
1032
978
  };
1033
979
  }
980
+ async get(url, config) {
981
+ const fullUrl = this.getFullUrl(url);
982
+ try {
983
+ return await import_axios.default.get(fullUrl, config);
984
+ } catch (e) {
985
+ throw BackendError.fromError(e) ?? e;
986
+ }
987
+ }
988
+ async post(url, data, config) {
989
+ const fullUrl = this.getFullUrl(url);
990
+ try {
991
+ return await import_axios.default.post(fullUrl, data, config);
992
+ } catch (e) {
993
+ throw BackendError.fromError(e) ?? e;
994
+ }
995
+ }
996
+ getFullUrl(url) {
997
+ return url.startsWith(this.apiURL) ? url : `${this.apiURL}${addPrefix(url, "/")}`;
998
+ }
999
+ };
1000
+ var BackendError = class _BackendError extends Error {
1001
+ constructor(e) {
1002
+ super();
1003
+ this.e = e;
1004
+ Error.captureStackTrace(this, this.constructor);
1005
+ }
1006
+ name;
1007
+ static fromError(e) {
1008
+ if (import_axios.default.isAxiosError(e) && e?.response?.data && "message" in e.response.data) {
1009
+ return new _BackendError(e);
1010
+ }
1011
+ return void 0;
1012
+ }
1013
+ get status() {
1014
+ return this.e.response?.status ?? void 0;
1015
+ }
1016
+ get message() {
1017
+ return `Request to ${this.endpoint} failed: ${this.status} ${this.respMessage() ?? "Unknown resp"}`;
1018
+ }
1019
+ respMessage() {
1020
+ if (!this.e.response?.data) {
1021
+ return void 0;
1022
+ }
1023
+ return (this.e.response?.data).message ?? void 0;
1024
+ }
1025
+ get endpoint() {
1026
+ return this.e.config?.url ?? "";
1027
+ }
1028
+ toString() {
1029
+ return this.message;
1030
+ }
1034
1031
  };
1035
1032
 
1036
1033
  // src/globals/const.ts
@@ -1246,6 +1243,7 @@ var MSafeClient = class _MSafeClient {
1246
1243
  SignatureVerifier,
1247
1244
  TESTNET_RPC_URL,
1248
1245
  Uint8ArrayToHex,
1246
+ addPrefix,
1249
1247
  getAllCoins,
1250
1248
  getMSafeConfig,
1251
1249
  getPublicKeyFromChain,