@msafe/sui3-sdk 0.0.22 → 0.0.24

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,
@@ -78,6 +79,21 @@ var CreateHelper = class {
78
79
  async validateCreateInfo(createInfo) {
79
80
  return this.calculateMSafeAddress(createInfo);
80
81
  }
82
+ /**
83
+ * Add user manually added public key for an address. If user input the public key with
84
+ * extra signature scheme flag, the scheme value will be automatically fixed.
85
+ *
86
+ * @param address
87
+ * @param pkEncoded
88
+ * @throws error if the public key does not match
89
+ */
90
+ addPublicKey(address, pkEncoded) {
91
+ const pk = import_sui3_utils.PublicKeySerde.de(pkEncoded);
92
+ if (!(0, import_sui3_utils.isSameAddress)(address, pk.toSuiAddress())) {
93
+ throw new Error("Public key not match");
94
+ }
95
+ this.pkHelper.addPublicKey(address, pk);
96
+ }
81
97
  async submitMSafeCreation(creationInfo) {
82
98
  const msafeAddress = await this.validateCreateInfo(creationInfo);
83
99
  const signingMessage = import_sui3_utils.SigningMessageHelper.createMSafeMessage(msafeAddress);
@@ -121,7 +137,7 @@ var CreateHelper = class {
121
137
  };
122
138
 
123
139
  // src/core/MSafeClient.ts
124
- var import_sui3_utils6 = require("@msafe/sui3-utils");
140
+ var import_sui3_utils7 = require("@msafe/sui3-utils");
125
141
 
126
142
  // src/core/AddressBookSDK.ts
127
143
  var import_sui3_utils2 = require("@msafe/sui3-utils");
@@ -244,6 +260,12 @@ var Formatter = class {
244
260
  return Coin.isCoin(struct);
245
261
  }
246
262
  };
263
+ function addPrefix(s, prefix) {
264
+ if (s.startsWith(prefix)) {
265
+ return s;
266
+ }
267
+ return prefix + s;
268
+ }
247
269
 
248
270
  // src/utils/crypto.ts
249
271
  var SignatureVerifier = class _SignatureVerifier {
@@ -729,6 +751,8 @@ var MSafeAccount = class _MSafeAccount {
729
751
  };
730
752
 
731
753
  // src/core/PublicKeyHelper.ts
754
+ var import_sui3_utils5 = require("@msafe/sui3-utils");
755
+ var import_utils5 = require("@mysten/sui.js/utils");
732
756
  var PublicKeyHelper = class {
733
757
  constructor(globals) {
734
758
  this.globals = globals;
@@ -773,6 +797,12 @@ var PublicKeyHelper = class {
773
797
  }
774
798
  return results;
775
799
  }
800
+ addPublicKey(address, publicKey) {
801
+ if (!(0, import_sui3_utils5.isSameAddress)(address, publicKey.toSuiAddress())) {
802
+ throw new Error("Invalid public key");
803
+ }
804
+ this.knownPublicKeys.set((0, import_utils5.normalizeSuiAddress)(address), publicKey);
805
+ }
776
806
  async _getPublicKey(address) {
777
807
  const pkBackend = await this.getPublicKeyFromBackend(address);
778
808
  if (pkBackend) {
@@ -801,7 +831,7 @@ var PublicKeyHelper = class {
801
831
  var import_client = require("@mysten/sui.js/client");
802
832
 
803
833
  // src/backend/BackendImpl.ts
804
- var import_sui3_utils5 = require("@msafe/sui3-utils");
834
+ var import_sui3_utils6 = require("@msafe/sui3-utils");
805
835
  var import_axios = __toESM(require("axios"), 1);
806
836
  var BackendImpl = class _BackendImpl {
807
837
  constructor(apiURL) {
@@ -809,16 +839,13 @@ var BackendImpl = class _BackendImpl {
809
839
  }
810
840
  _token;
811
841
  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
- }
842
+ const res = await this.post(`/auth/login`, input);
816
843
  this._token = res.data.accessToken;
817
844
  return this._token;
818
845
  }
819
846
  async verifyToken(jwt) {
820
847
  try {
821
- const res = await import_axios.default.get(`${this.apiURL}/auth`, { headers: this.headers(jwt) });
848
+ const res = await this.get(`/auth`, { headers: this.headers(jwt) });
822
849
  return res.status === 200;
823
850
  } catch (_) {
824
851
  return false;
@@ -834,54 +861,42 @@ var BackendImpl = class _BackendImpl {
834
861
  const query = {
835
862
  userAddressList: addresses
836
863
  };
837
- const res = await import_axios.default.get(`${this.apiURL}/user/public-keys`, {
864
+ const res = await this.get(`/user/public-keys`, {
838
865
  params: query,
839
866
  headers: this.headers()
840
867
  });
841
- if (res.status !== 200 && res.status !== 201) {
842
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
843
- }
844
868
  return res.data?.map(
845
- (publicKeyWithSchema) => publicKeyWithSchema ? import_sui3_utils5.PublicKeySerde.de(publicKeyWithSchema) : void 0
869
+ (publicKeyWithSchema) => publicKeyWithSchema ? import_sui3_utils6.PublicKeySerde.de(publicKeyWithSchema) : void 0
846
870
  );
847
871
  }
848
872
  async getMSafeAccountInfo(msafeAddress) {
849
873
  const q = {
850
874
  msafeAddress
851
875
  };
852
- const res = await import_axios.default.get(`${this.apiURL}/msafe`, {
876
+ const res = await this.get(`/msafe`, {
853
877
  params: q,
854
878
  headers: this.headers()
855
879
  });
856
- if (res.status !== 200 && res.status !== 201) {
857
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
858
- }
859
880
  return _BackendImpl.toMSafeConfig(res.data);
860
881
  }
861
882
  async getUserInfo() {
862
- const userRes = await import_axios.default.get(`${this.apiURL}/user`, {
883
+ const userRes = await this.get(`/user`, {
863
884
  headers: this.headers()
864
885
  });
865
- if (userRes.status !== 200 && userRes.status !== 201) {
866
- throw new Error(`invalid getPublicKeyBatch return: ${userRes}`);
867
- }
868
886
  return userRes.data;
869
887
  }
870
888
  async getOwnedMSafeByStatus(input) {
871
889
  const q = {
872
- status: input.status ?? import_sui3_utils5.UserMSafeStatus.active,
890
+ status: input.status ?? import_sui3_utils6.UserMSafeStatus.active,
873
891
  ...input.pagination ? {
874
892
  page: input.pagination.page.toString(),
875
893
  limit: input.pagination.limit.toString()
876
894
  } : {}
877
895
  };
878
- const res = await import_axios.default.get(`${this.apiURL}/msafe/owned`, {
896
+ const res = await this.get(`/msafe/owned`, {
879
897
  params: q,
880
898
  headers: this.headers()
881
899
  });
882
- if (res.status !== 200 && res.status !== 201) {
883
- throw new Error(`invalid getOwnedMSafeByStatus return: ${res}`);
884
- }
885
900
  return {
886
901
  data: res.data.data.map(_BackendImpl.toMSafeConfig),
887
902
  meta: res.data.meta
@@ -889,78 +904,54 @@ var BackendImpl = class _BackendImpl {
889
904
  }
890
905
  async updateMSafeStatus(input) {
891
906
  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
- }
907
+ await this.post(`/msafe/status`, p, { headers: this.headers() });
896
908
  }
897
909
  async getPendingTransactions(input) {
898
- const res = await import_axios.default.get(`${this.apiURL}/transaction/pending`, {
910
+ const res = await this.get(`/transaction/pending`, {
899
911
  params: input,
900
912
  headers: this.headers()
901
913
  });
902
- if (res.status !== 200 && res.status !== 201) {
903
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
904
- }
905
914
  return res.data;
906
915
  }
907
916
  async getHistoryTransactions(input) {
908
- const res = await import_axios.default.get(`${this.apiURL}/transaction/history`, {
917
+ const res = await this.get(`/transaction/history`, {
909
918
  params: input,
910
919
  headers: this.headers()
911
920
  });
912
- if (res.status !== 200 && res.status !== 201) {
913
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
914
- }
915
921
  return res.data;
916
922
  }
917
923
  async getFutureIntentions(input) {
918
- const res = await import_axios.default.get(`${this.apiURL}/transaction/intention`, {
924
+ const res = await this.get(`/transaction/intention`, {
919
925
  params: input,
920
926
  headers: this.headers()
921
927
  });
922
- if (res.status !== 200 && res.status !== 201) {
923
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
924
- }
925
928
  return res.data;
926
929
  }
927
930
  async getCurrentSequenceNumber(address) {
928
- const res = await import_axios.default.get(`${this.apiURL}/transaction/sn/current`, {
931
+ const res = await this.get(`/transaction/sn/current`, {
929
932
  params: {
930
933
  msafeAddress: address
931
934
  },
932
935
  headers: this.headers()
933
936
  });
934
- if (res.status !== 200 && res.status !== 201) {
935
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
936
- }
937
937
  return res.data;
938
938
  }
939
939
  async getNextSequenceNumber(address) {
940
- const res = await import_axios.default.get(`${this.apiURL}/transaction/sn/next`, {
940
+ const res = await this.get(`/transaction/sn/next`, {
941
941
  params: {
942
942
  msafeAddress: address
943
943
  },
944
944
  headers: this.headers()
945
945
  });
946
- if (res.status !== 200 && res.status !== 201) {
947
- throw new Error(`invalid getNextSequenceNumber return: ${res}`);
948
- }
949
946
  return res.data;
950
947
  }
951
948
  async createMSafeAccount(input) {
952
- const res = await import_axios.default.post(`${this.apiURL}/msafe/create`, input, {
949
+ await this.post(`/msafe/create`, input, {
953
950
  headers: this.headers()
954
951
  });
955
- if (res.status !== 200 && res.status !== 201) {
956
- throw new Error(`invalid createMSafeAccount return: ${res}`);
957
- }
958
952
  }
959
953
  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
- }
954
+ await this.post(`/transaction/intention`, input, { headers: this.headers() });
964
955
  }
965
956
  // TODO later
966
957
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -968,52 +959,30 @@ var BackendImpl = class _BackendImpl {
968
959
  return void 0;
969
960
  }
970
961
  async rejectCurrentTx(input) {
971
- const res = await import_axios.default.post(`${this.apiURL}/transaction/pending/reject`, input, {
962
+ await this.post(`/transaction/pending/reject`, input, {
972
963
  headers: this.headers()
973
964
  });
974
- if (res.status !== 200 && res.status !== 201) {
975
- throw new Error(`invalid voteForTransaction return: ${res}`);
976
- }
977
965
  }
978
966
  async voteForTransaction(input) {
979
- const res = await import_axios.default.post(`${this.apiURL}/transaction/pending/vote`, input, {
967
+ await this.post(`/transaction/pending/vote`, input, {
980
968
  headers: this.headers()
981
969
  });
982
- if (res.status !== 200 && res.status !== 201) {
983
- throw new Error(`invalid voteForTransaction return: ${res}`);
984
- }
985
970
  }
986
971
  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
- }
972
+ await this.post(`/transaction/pending/build`, input, { headers: this.headers() });
991
973
  }
992
974
  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
- }
975
+ await this.post(`/transaction/pending/skip`, input, { headers: this.headers() });
997
976
  }
998
977
  async getAddressBookEntries(pagination) {
999
- const res = await import_axios.default.get(`${this.apiURL}/address-book`, {
978
+ const res = await this.get(`/address-book`, {
1000
979
  headers: this.headers(),
1001
980
  params: pagination
1002
981
  });
1003
- if (res.status !== 200) {
1004
- throw new Error(`Invalid address-book return: ${res}`);
1005
- }
1006
982
  return res.data;
1007
983
  }
1008
984
  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;
985
+ await this.post(`/address-book`, input, { headers: this.headers() });
1017
986
  }
1018
987
  headers(token) {
1019
988
  return { Authorization: `Bearer ${token || this._token}` };
@@ -1024,13 +993,64 @@ var BackendImpl = class _BackendImpl {
1024
993
  owners: resp.owners.map((owner) => ({
1025
994
  address: owner.address,
1026
995
  weight: owner.weight,
1027
- publicKey: import_sui3_utils5.PublicKeySerde.de({
996
+ publicKey: import_sui3_utils6.PublicKeySerde.de({
1028
997
  publicKeyEncoded: owner.publicKeyEncoded,
1029
998
  schema: owner.schema
1030
999
  })
1031
1000
  }))
1032
1001
  };
1033
1002
  }
1003
+ async get(url, config) {
1004
+ const fullUrl = this.getFullUrl(url);
1005
+ try {
1006
+ return await import_axios.default.get(fullUrl, config);
1007
+ } catch (e) {
1008
+ throw BackendError.fromError(e) ?? e;
1009
+ }
1010
+ }
1011
+ async post(url, data, config) {
1012
+ const fullUrl = this.getFullUrl(url);
1013
+ try {
1014
+ return await import_axios.default.post(fullUrl, data, config);
1015
+ } catch (e) {
1016
+ throw BackendError.fromError(e) ?? e;
1017
+ }
1018
+ }
1019
+ getFullUrl(url) {
1020
+ return url.startsWith(this.apiURL) ? url : `${this.apiURL}${addPrefix(url, "/")}`;
1021
+ }
1022
+ };
1023
+ var BackendError = class _BackendError extends Error {
1024
+ constructor(e) {
1025
+ super();
1026
+ this.e = e;
1027
+ Error.captureStackTrace(this, this.constructor);
1028
+ }
1029
+ name;
1030
+ static fromError(e) {
1031
+ if (import_axios.default.isAxiosError(e) && e?.response?.data && "message" in e.response.data) {
1032
+ return new _BackendError(e);
1033
+ }
1034
+ return void 0;
1035
+ }
1036
+ get status() {
1037
+ return this.e.response?.status ?? void 0;
1038
+ }
1039
+ get message() {
1040
+ return `Request to ${this.endpoint} failed: ${this.status} ${this.respMessage() ?? "Unknown resp"}`;
1041
+ }
1042
+ respMessage() {
1043
+ if (!this.e.response?.data) {
1044
+ return void 0;
1045
+ }
1046
+ return (this.e.response?.data).message ?? void 0;
1047
+ }
1048
+ get endpoint() {
1049
+ return this.e.config?.url ?? "";
1050
+ }
1051
+ toString() {
1052
+ return this.message;
1053
+ }
1034
1054
  };
1035
1055
 
1036
1056
  // src/globals/const.ts
@@ -1162,7 +1182,7 @@ var MSafeClient = class _MSafeClient {
1162
1182
  return jwt;
1163
1183
  }
1164
1184
  async authSign(wallet) {
1165
- const messageStr = import_sui3_utils6.SigningMessageHelper.loginMessageWithTimestamp((/* @__PURE__ */ new Date()).toUTCString());
1185
+ const messageStr = import_sui3_utils7.SigningMessageHelper.loginMessageWithTimestamp((/* @__PURE__ */ new Date()).toUTCString());
1166
1186
  const sig = await wallet.signPersonalMessage({
1167
1187
  messageStr
1168
1188
  });
@@ -1177,7 +1197,7 @@ var MSafeClient = class _MSafeClient {
1177
1197
  return this.globals.backend.getUserInfo();
1178
1198
  }
1179
1199
  async ownedMSafe(pagination) {
1180
- return this.globals.backend.getOwnedMSafeByStatus({ status: import_sui3_utils6.UserMSafeStatus.active, pagination });
1200
+ return this.globals.backend.getOwnedMSafeByStatus({ status: import_sui3_utils7.UserMSafeStatus.active, pagination });
1181
1201
  }
1182
1202
  async createAccount(info) {
1183
1203
  return this.creationHelper.submitMSafeCreation(info);
@@ -1246,6 +1266,7 @@ var MSafeClient = class _MSafeClient {
1246
1266
  SignatureVerifier,
1247
1267
  TESTNET_RPC_URL,
1248
1268
  Uint8ArrayToHex,
1269
+ addPrefix,
1249
1270
  getAllCoins,
1250
1271
  getMSafeConfig,
1251
1272
  getPublicKeyFromChain,