@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.js CHANGED
@@ -195,6 +195,12 @@ var Formatter = class {
195
195
  return Coin.isCoin(struct);
196
196
  }
197
197
  };
198
+ function addPrefix(s, prefix) {
199
+ if (s.startsWith(prefix)) {
200
+ return s;
201
+ }
202
+ return prefix + s;
203
+ }
198
204
 
199
205
  // src/utils/crypto.ts
200
206
  var SignatureVerifier = class _SignatureVerifier {
@@ -763,16 +769,13 @@ var BackendImpl = class _BackendImpl {
763
769
  }
764
770
  _token;
765
771
  async authSign(input) {
766
- const res = await axios.post(`${this.apiURL}/auth/login`, input);
767
- if (res.status !== 200 && res.status !== 201) {
768
- throw new Error(`invalid authSign return: ${res}`);
769
- }
772
+ const res = await this.post(`/auth/login`, input);
770
773
  this._token = res.data.accessToken;
771
774
  return this._token;
772
775
  }
773
776
  async verifyToken(jwt) {
774
777
  try {
775
- const res = await axios.get(`${this.apiURL}/auth`, { headers: this.headers(jwt) });
778
+ const res = await this.get(`/auth`, { headers: this.headers(jwt) });
776
779
  return res.status === 200;
777
780
  } catch (_) {
778
781
  return false;
@@ -788,13 +791,10 @@ var BackendImpl = class _BackendImpl {
788
791
  const query = {
789
792
  userAddressList: addresses
790
793
  };
791
- const res = await axios.get(`${this.apiURL}/user/public-keys`, {
794
+ const res = await this.get(`/user/public-keys`, {
792
795
  params: query,
793
796
  headers: this.headers()
794
797
  });
795
- if (res.status !== 200 && res.status !== 201) {
796
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
797
- }
798
798
  return res.data?.map(
799
799
  (publicKeyWithSchema) => publicKeyWithSchema ? PublicKeySerde3.de(publicKeyWithSchema) : void 0
800
800
  );
@@ -803,22 +803,16 @@ var BackendImpl = class _BackendImpl {
803
803
  const q = {
804
804
  msafeAddress
805
805
  };
806
- const res = await axios.get(`${this.apiURL}/msafe`, {
806
+ const res = await this.get(`/msafe`, {
807
807
  params: q,
808
808
  headers: this.headers()
809
809
  });
810
- if (res.status !== 200 && res.status !== 201) {
811
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
812
- }
813
810
  return _BackendImpl.toMSafeConfig(res.data);
814
811
  }
815
812
  async getUserInfo() {
816
- const userRes = await axios.get(`${this.apiURL}/user`, {
813
+ const userRes = await this.get(`/user`, {
817
814
  headers: this.headers()
818
815
  });
819
- if (userRes.status !== 200 && userRes.status !== 201) {
820
- throw new Error(`invalid getPublicKeyBatch return: ${userRes}`);
821
- }
822
816
  return userRes.data;
823
817
  }
824
818
  async getOwnedMSafeByStatus(input) {
@@ -829,13 +823,10 @@ var BackendImpl = class _BackendImpl {
829
823
  limit: input.pagination.limit.toString()
830
824
  } : {}
831
825
  };
832
- const res = await axios.get(`${this.apiURL}/msafe/owned`, {
826
+ const res = await this.get(`/msafe/owned`, {
833
827
  params: q,
834
828
  headers: this.headers()
835
829
  });
836
- if (res.status !== 200 && res.status !== 201) {
837
- throw new Error(`invalid getOwnedMSafeByStatus return: ${res}`);
838
- }
839
830
  return {
840
831
  data: res.data.data.map(_BackendImpl.toMSafeConfig),
841
832
  meta: res.data.meta
@@ -843,78 +834,54 @@ var BackendImpl = class _BackendImpl {
843
834
  }
844
835
  async updateMSafeStatus(input) {
845
836
  const p = input;
846
- const res = await axios.post(`${this.apiURL}/msafe/status`, p, { headers: this.headers() });
847
- if (res.status !== 200 && res.status !== 201) {
848
- throw new Error(`Invalid updateMSafeStatus return: ${res}`);
849
- }
837
+ await this.post(`/msafe/status`, p, { headers: this.headers() });
850
838
  }
851
839
  async getPendingTransactions(input) {
852
- const res = await axios.get(`${this.apiURL}/transaction/pending`, {
840
+ const res = await this.get(`/transaction/pending`, {
853
841
  params: input,
854
842
  headers: this.headers()
855
843
  });
856
- if (res.status !== 200 && res.status !== 201) {
857
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
858
- }
859
844
  return res.data;
860
845
  }
861
846
  async getHistoryTransactions(input) {
862
- const res = await axios.get(`${this.apiURL}/transaction/history`, {
847
+ const res = await this.get(`/transaction/history`, {
863
848
  params: input,
864
849
  headers: this.headers()
865
850
  });
866
- if (res.status !== 200 && res.status !== 201) {
867
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
868
- }
869
851
  return res.data;
870
852
  }
871
853
  async getFutureIntentions(input) {
872
- const res = await axios.get(`${this.apiURL}/transaction/intention`, {
854
+ const res = await this.get(`/transaction/intention`, {
873
855
  params: input,
874
856
  headers: this.headers()
875
857
  });
876
- if (res.status !== 200 && res.status !== 201) {
877
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
878
- }
879
858
  return res.data;
880
859
  }
881
860
  async getCurrentSequenceNumber(address) {
882
- const res = await axios.get(`${this.apiURL}/transaction/sn/current`, {
861
+ const res = await this.get(`/transaction/sn/current`, {
883
862
  params: {
884
863
  msafeAddress: address
885
864
  },
886
865
  headers: this.headers()
887
866
  });
888
- if (res.status !== 200 && res.status !== 201) {
889
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
890
- }
891
867
  return res.data;
892
868
  }
893
869
  async getNextSequenceNumber(address) {
894
- const res = await axios.get(`${this.apiURL}/transaction/sn/next`, {
870
+ const res = await this.get(`/transaction/sn/next`, {
895
871
  params: {
896
872
  msafeAddress: address
897
873
  },
898
874
  headers: this.headers()
899
875
  });
900
- if (res.status !== 200 && res.status !== 201) {
901
- throw new Error(`invalid getNextSequenceNumber return: ${res}`);
902
- }
903
876
  return res.data;
904
877
  }
905
878
  async createMSafeAccount(input) {
906
- const res = await axios.post(`${this.apiURL}/msafe/create`, input, {
879
+ await this.post(`/msafe/create`, input, {
907
880
  headers: this.headers()
908
881
  });
909
- if (res.status !== 200 && res.status !== 201) {
910
- throw new Error(`invalid createMSafeAccount return: ${res}`);
911
- }
912
882
  }
913
883
  async proposeIntention(input) {
914
- const res = await axios.post(`${this.apiURL}/transaction/intention`, input, { headers: this.headers() });
915
- if (res.status !== 200 && res.status !== 201) {
916
- throw new Error(`invalid proposeIntention return: ${res}`);
917
- }
884
+ await this.post(`/transaction/intention`, input, { headers: this.headers() });
918
885
  }
919
886
  // TODO later
920
887
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -922,52 +889,30 @@ var BackendImpl = class _BackendImpl {
922
889
  return void 0;
923
890
  }
924
891
  async rejectCurrentTx(input) {
925
- const res = await axios.post(`${this.apiURL}/transaction/pending/reject`, input, {
892
+ await this.post(`/transaction/pending/reject`, input, {
926
893
  headers: this.headers()
927
894
  });
928
- if (res.status !== 200 && res.status !== 201) {
929
- throw new Error(`invalid voteForTransaction return: ${res}`);
930
- }
931
895
  }
932
896
  async voteForTransaction(input) {
933
- const res = await axios.post(`${this.apiURL}/transaction/pending/vote`, input, {
897
+ await this.post(`/transaction/pending/vote`, input, {
934
898
  headers: this.headers()
935
899
  });
936
- if (res.status !== 200 && res.status !== 201) {
937
- throw new Error(`invalid voteForTransaction return: ${res}`);
938
- }
939
900
  }
940
901
  async buildNextIntentionAndAddToPending(input) {
941
- const res = await axios.post(`${this.apiURL}/transaction/pending/build`, input, { headers: this.headers() });
942
- if (res.status !== 200 && res.status !== 201) {
943
- throw new Error(`invalid buildNextIntentionAndAddToPending return: ${res}`);
944
- }
902
+ await this.post(`/transaction/pending/build`, input, { headers: this.headers() });
945
903
  }
946
904
  async skipNextFailedIntention(input) {
947
- const res = await axios.post(`${this.apiURL}/transaction/pending/skip`, input, { headers: this.headers() });
948
- if (res.status !== 200 && res.status !== 201) {
949
- throw new Error(`invalid skipNextFailedIntention return: ${res}`);
950
- }
905
+ await this.post(`/transaction/pending/skip`, input, { headers: this.headers() });
951
906
  }
952
907
  async getAddressBookEntries(pagination) {
953
- const res = await axios.get(`${this.apiURL}/address-book`, {
908
+ const res = await this.get(`/address-book`, {
954
909
  headers: this.headers(),
955
910
  params: pagination
956
911
  });
957
- if (res.status !== 200) {
958
- throw new Error(`Invalid address-book return: ${res}`);
959
- }
960
912
  return res.data;
961
913
  }
962
914
  async updateAddressBook(input) {
963
- const res = await axios.post(`${this.apiURL}/address-book`, input, { headers: this.headers() });
964
- if (res.status !== 200 && res.status !== 201) {
965
- throw new Error(`invalid updateAddressBook return: ${res}`);
966
- }
967
- }
968
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
969
- async processExecutedTransaction(_digest) {
970
- return void 0;
915
+ await this.post(`/address-book`, input, { headers: this.headers() });
971
916
  }
972
917
  headers(token) {
973
918
  return { Authorization: `Bearer ${token || this._token}` };
@@ -985,6 +930,57 @@ var BackendImpl = class _BackendImpl {
985
930
  }))
986
931
  };
987
932
  }
933
+ async get(url, config) {
934
+ const fullUrl = this.getFullUrl(url);
935
+ try {
936
+ return await axios.get(fullUrl, config);
937
+ } catch (e) {
938
+ throw BackendError.fromError(e) ?? e;
939
+ }
940
+ }
941
+ async post(url, data, config) {
942
+ const fullUrl = this.getFullUrl(url);
943
+ try {
944
+ return await axios.post(fullUrl, data, config);
945
+ } catch (e) {
946
+ throw BackendError.fromError(e) ?? e;
947
+ }
948
+ }
949
+ getFullUrl(url) {
950
+ return url.startsWith(this.apiURL) ? url : `${this.apiURL}${addPrefix(url, "/")}`;
951
+ }
952
+ };
953
+ var BackendError = class _BackendError extends Error {
954
+ constructor(e) {
955
+ super();
956
+ this.e = e;
957
+ Error.captureStackTrace(this, this.constructor);
958
+ }
959
+ name;
960
+ static fromError(e) {
961
+ if (axios.isAxiosError(e) && e?.response?.data && "message" in e.response.data) {
962
+ return new _BackendError(e);
963
+ }
964
+ return void 0;
965
+ }
966
+ get status() {
967
+ return this.e.response?.status ?? void 0;
968
+ }
969
+ get message() {
970
+ return `Request to ${this.endpoint} failed: ${this.status} ${this.respMessage() ?? "Unknown resp"}`;
971
+ }
972
+ respMessage() {
973
+ if (!this.e.response?.data) {
974
+ return void 0;
975
+ }
976
+ return (this.e.response?.data).message ?? void 0;
977
+ }
978
+ get endpoint() {
979
+ return this.e.config?.url ?? "";
980
+ }
981
+ toString() {
982
+ return this.message;
983
+ }
988
984
  };
989
985
 
990
986
  // src/globals/const.ts
@@ -1199,6 +1195,7 @@ export {
1199
1195
  SignatureVerifier,
1200
1196
  TESTNET_RPC_URL,
1201
1197
  Uint8ArrayToHex,
1198
+ addPrefix,
1202
1199
  getAllCoins,
1203
1200
  getMSafeConfig,
1204
1201
  getPublicKeyFromChain,