@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.js CHANGED
@@ -1,5 +1,10 @@
1
1
  // src/core/CreateHelper.ts
2
- import { MultiSigAccount, PublicKeySerde, SigningMessageHelper } from "@msafe/sui3-utils";
2
+ import {
3
+ isSameAddress,
4
+ MultiSigAccount,
5
+ PublicKeySerde,
6
+ SigningMessageHelper
7
+ } from "@msafe/sui3-utils";
3
8
  var CreateHelper = class {
4
9
  constructor(globals, pkHelper) {
5
10
  this.globals = globals;
@@ -17,6 +22,21 @@ var CreateHelper = class {
17
22
  async validateCreateInfo(createInfo) {
18
23
  return this.calculateMSafeAddress(createInfo);
19
24
  }
25
+ /**
26
+ * Add user manually added public key for an address. If user input the public key with
27
+ * extra signature scheme flag, the scheme value will be automatically fixed.
28
+ *
29
+ * @param address
30
+ * @param pkEncoded
31
+ * @throws error if the public key does not match
32
+ */
33
+ addPublicKey(address, pkEncoded) {
34
+ const pk = PublicKeySerde.de(pkEncoded);
35
+ if (!isSameAddress(address, pk.toSuiAddress())) {
36
+ throw new Error("Public key not match");
37
+ }
38
+ this.pkHelper.addPublicKey(address, pk);
39
+ }
20
40
  async submitMSafeCreation(creationInfo) {
21
41
  const msafeAddress = await this.validateCreateInfo(creationInfo);
22
42
  const signingMessage = SigningMessageHelper.createMSafeMessage(msafeAddress);
@@ -103,7 +123,7 @@ import {
103
123
  TransactionType,
104
124
  buildObjectTransferTxb,
105
125
  buildRejectTxb,
106
- isSameAddress
126
+ isSameAddress as isSameAddress2
107
127
  } from "@msafe/sui3-utils";
108
128
  import { TransactionBlock } from "@mysten/sui.js/transactions";
109
129
  import { normalizeStructTag as normalizeStructTag3 } from "@mysten/sui.js/utils";
@@ -195,6 +215,12 @@ var Formatter = class {
195
215
  return Coin.isCoin(struct);
196
216
  }
197
217
  };
218
+ function addPrefix(s, prefix) {
219
+ if (s.startsWith(prefix)) {
220
+ return s;
221
+ }
222
+ return prefix + s;
223
+ }
198
224
 
199
225
  // src/utils/crypto.ts
200
226
  var SignatureVerifier = class _SignatureVerifier {
@@ -655,7 +681,7 @@ var MSafeAccount = class _MSafeAccount {
655
681
  calculateWeight(addressList) {
656
682
  let gotWeight = 0;
657
683
  for (let i = 0; i < addressList.length; i++) {
658
- const found = this.info.owners.find((owner) => isSameAddress(owner.address, addressList[i]));
684
+ const found = this.info.owners.find((owner) => isSameAddress2(owner.address, addressList[i]));
659
685
  if (found) {
660
686
  gotWeight += found.weight;
661
687
  }
@@ -680,6 +706,8 @@ var MSafeAccount = class _MSafeAccount {
680
706
  };
681
707
 
682
708
  // src/core/PublicKeyHelper.ts
709
+ import { isSameAddress as isSameAddress3 } from "@msafe/sui3-utils";
710
+ import { normalizeSuiAddress as normalizeSuiAddress2 } from "@mysten/sui.js/utils";
683
711
  var PublicKeyHelper = class {
684
712
  constructor(globals) {
685
713
  this.globals = globals;
@@ -724,6 +752,12 @@ var PublicKeyHelper = class {
724
752
  }
725
753
  return results;
726
754
  }
755
+ addPublicKey(address, publicKey) {
756
+ if (!isSameAddress3(address, publicKey.toSuiAddress())) {
757
+ throw new Error("Invalid public key");
758
+ }
759
+ this.knownPublicKeys.set(normalizeSuiAddress2(address), publicKey);
760
+ }
727
761
  async _getPublicKey(address) {
728
762
  const pkBackend = await this.getPublicKeyFromBackend(address);
729
763
  if (pkBackend) {
@@ -763,16 +797,13 @@ var BackendImpl = class _BackendImpl {
763
797
  }
764
798
  _token;
765
799
  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
- }
800
+ const res = await this.post(`/auth/login`, input);
770
801
  this._token = res.data.accessToken;
771
802
  return this._token;
772
803
  }
773
804
  async verifyToken(jwt) {
774
805
  try {
775
- const res = await axios.get(`${this.apiURL}/auth`, { headers: this.headers(jwt) });
806
+ const res = await this.get(`/auth`, { headers: this.headers(jwt) });
776
807
  return res.status === 200;
777
808
  } catch (_) {
778
809
  return false;
@@ -788,13 +819,10 @@ var BackendImpl = class _BackendImpl {
788
819
  const query = {
789
820
  userAddressList: addresses
790
821
  };
791
- const res = await axios.get(`${this.apiURL}/user/public-keys`, {
822
+ const res = await this.get(`/user/public-keys`, {
792
823
  params: query,
793
824
  headers: this.headers()
794
825
  });
795
- if (res.status !== 200 && res.status !== 201) {
796
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
797
- }
798
826
  return res.data?.map(
799
827
  (publicKeyWithSchema) => publicKeyWithSchema ? PublicKeySerde3.de(publicKeyWithSchema) : void 0
800
828
  );
@@ -803,22 +831,16 @@ var BackendImpl = class _BackendImpl {
803
831
  const q = {
804
832
  msafeAddress
805
833
  };
806
- const res = await axios.get(`${this.apiURL}/msafe`, {
834
+ const res = await this.get(`/msafe`, {
807
835
  params: q,
808
836
  headers: this.headers()
809
837
  });
810
- if (res.status !== 200 && res.status !== 201) {
811
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
812
- }
813
838
  return _BackendImpl.toMSafeConfig(res.data);
814
839
  }
815
840
  async getUserInfo() {
816
- const userRes = await axios.get(`${this.apiURL}/user`, {
841
+ const userRes = await this.get(`/user`, {
817
842
  headers: this.headers()
818
843
  });
819
- if (userRes.status !== 200 && userRes.status !== 201) {
820
- throw new Error(`invalid getPublicKeyBatch return: ${userRes}`);
821
- }
822
844
  return userRes.data;
823
845
  }
824
846
  async getOwnedMSafeByStatus(input) {
@@ -829,13 +851,10 @@ var BackendImpl = class _BackendImpl {
829
851
  limit: input.pagination.limit.toString()
830
852
  } : {}
831
853
  };
832
- const res = await axios.get(`${this.apiURL}/msafe/owned`, {
854
+ const res = await this.get(`/msafe/owned`, {
833
855
  params: q,
834
856
  headers: this.headers()
835
857
  });
836
- if (res.status !== 200 && res.status !== 201) {
837
- throw new Error(`invalid getOwnedMSafeByStatus return: ${res}`);
838
- }
839
858
  return {
840
859
  data: res.data.data.map(_BackendImpl.toMSafeConfig),
841
860
  meta: res.data.meta
@@ -843,78 +862,54 @@ var BackendImpl = class _BackendImpl {
843
862
  }
844
863
  async updateMSafeStatus(input) {
845
864
  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
- }
865
+ await this.post(`/msafe/status`, p, { headers: this.headers() });
850
866
  }
851
867
  async getPendingTransactions(input) {
852
- const res = await axios.get(`${this.apiURL}/transaction/pending`, {
868
+ const res = await this.get(`/transaction/pending`, {
853
869
  params: input,
854
870
  headers: this.headers()
855
871
  });
856
- if (res.status !== 200 && res.status !== 201) {
857
- throw new Error(`invalid getPublicKeyBatch return: ${res}`);
858
- }
859
872
  return res.data;
860
873
  }
861
874
  async getHistoryTransactions(input) {
862
- const res = await axios.get(`${this.apiURL}/transaction/history`, {
875
+ const res = await this.get(`/transaction/history`, {
863
876
  params: input,
864
877
  headers: this.headers()
865
878
  });
866
- if (res.status !== 200 && res.status !== 201) {
867
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
868
- }
869
879
  return res.data;
870
880
  }
871
881
  async getFutureIntentions(input) {
872
- const res = await axios.get(`${this.apiURL}/transaction/intention`, {
882
+ const res = await this.get(`/transaction/intention`, {
873
883
  params: input,
874
884
  headers: this.headers()
875
885
  });
876
- if (res.status !== 200 && res.status !== 201) {
877
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
878
- }
879
886
  return res.data;
880
887
  }
881
888
  async getCurrentSequenceNumber(address) {
882
- const res = await axios.get(`${this.apiURL}/transaction/sn/current`, {
889
+ const res = await this.get(`/transaction/sn/current`, {
883
890
  params: {
884
891
  msafeAddress: address
885
892
  },
886
893
  headers: this.headers()
887
894
  });
888
- if (res.status !== 200 && res.status !== 201) {
889
- throw new Error(`invalid getCurrentSequenceNumber return: ${res}`);
890
- }
891
895
  return res.data;
892
896
  }
893
897
  async getNextSequenceNumber(address) {
894
- const res = await axios.get(`${this.apiURL}/transaction/sn/next`, {
898
+ const res = await this.get(`/transaction/sn/next`, {
895
899
  params: {
896
900
  msafeAddress: address
897
901
  },
898
902
  headers: this.headers()
899
903
  });
900
- if (res.status !== 200 && res.status !== 201) {
901
- throw new Error(`invalid getNextSequenceNumber return: ${res}`);
902
- }
903
904
  return res.data;
904
905
  }
905
906
  async createMSafeAccount(input) {
906
- const res = await axios.post(`${this.apiURL}/msafe/create`, input, {
907
+ await this.post(`/msafe/create`, input, {
907
908
  headers: this.headers()
908
909
  });
909
- if (res.status !== 200 && res.status !== 201) {
910
- throw new Error(`invalid createMSafeAccount return: ${res}`);
911
- }
912
910
  }
913
911
  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
- }
912
+ await this.post(`/transaction/intention`, input, { headers: this.headers() });
918
913
  }
919
914
  // TODO later
920
915
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -922,52 +917,30 @@ var BackendImpl = class _BackendImpl {
922
917
  return void 0;
923
918
  }
924
919
  async rejectCurrentTx(input) {
925
- const res = await axios.post(`${this.apiURL}/transaction/pending/reject`, input, {
920
+ await this.post(`/transaction/pending/reject`, input, {
926
921
  headers: this.headers()
927
922
  });
928
- if (res.status !== 200 && res.status !== 201) {
929
- throw new Error(`invalid voteForTransaction return: ${res}`);
930
- }
931
923
  }
932
924
  async voteForTransaction(input) {
933
- const res = await axios.post(`${this.apiURL}/transaction/pending/vote`, input, {
925
+ await this.post(`/transaction/pending/vote`, input, {
934
926
  headers: this.headers()
935
927
  });
936
- if (res.status !== 200 && res.status !== 201) {
937
- throw new Error(`invalid voteForTransaction return: ${res}`);
938
- }
939
928
  }
940
929
  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
- }
930
+ await this.post(`/transaction/pending/build`, input, { headers: this.headers() });
945
931
  }
946
932
  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
- }
933
+ await this.post(`/transaction/pending/skip`, input, { headers: this.headers() });
951
934
  }
952
935
  async getAddressBookEntries(pagination) {
953
- const res = await axios.get(`${this.apiURL}/address-book`, {
936
+ const res = await this.get(`/address-book`, {
954
937
  headers: this.headers(),
955
938
  params: pagination
956
939
  });
957
- if (res.status !== 200) {
958
- throw new Error(`Invalid address-book return: ${res}`);
959
- }
960
940
  return res.data;
961
941
  }
962
942
  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;
943
+ await this.post(`/address-book`, input, { headers: this.headers() });
971
944
  }
972
945
  headers(token) {
973
946
  return { Authorization: `Bearer ${token || this._token}` };
@@ -985,6 +958,57 @@ var BackendImpl = class _BackendImpl {
985
958
  }))
986
959
  };
987
960
  }
961
+ async get(url, config) {
962
+ const fullUrl = this.getFullUrl(url);
963
+ try {
964
+ return await axios.get(fullUrl, config);
965
+ } catch (e) {
966
+ throw BackendError.fromError(e) ?? e;
967
+ }
968
+ }
969
+ async post(url, data, config) {
970
+ const fullUrl = this.getFullUrl(url);
971
+ try {
972
+ return await axios.post(fullUrl, data, config);
973
+ } catch (e) {
974
+ throw BackendError.fromError(e) ?? e;
975
+ }
976
+ }
977
+ getFullUrl(url) {
978
+ return url.startsWith(this.apiURL) ? url : `${this.apiURL}${addPrefix(url, "/")}`;
979
+ }
980
+ };
981
+ var BackendError = class _BackendError extends Error {
982
+ constructor(e) {
983
+ super();
984
+ this.e = e;
985
+ Error.captureStackTrace(this, this.constructor);
986
+ }
987
+ name;
988
+ static fromError(e) {
989
+ if (axios.isAxiosError(e) && e?.response?.data && "message" in e.response.data) {
990
+ return new _BackendError(e);
991
+ }
992
+ return void 0;
993
+ }
994
+ get status() {
995
+ return this.e.response?.status ?? void 0;
996
+ }
997
+ get message() {
998
+ return `Request to ${this.endpoint} failed: ${this.status} ${this.respMessage() ?? "Unknown resp"}`;
999
+ }
1000
+ respMessage() {
1001
+ if (!this.e.response?.data) {
1002
+ return void 0;
1003
+ }
1004
+ return (this.e.response?.data).message ?? void 0;
1005
+ }
1006
+ get endpoint() {
1007
+ return this.e.config?.url ?? "";
1008
+ }
1009
+ toString() {
1010
+ return this.message;
1011
+ }
988
1012
  };
989
1013
 
990
1014
  // src/globals/const.ts
@@ -1199,6 +1223,7 @@ export {
1199
1223
  SignatureVerifier,
1200
1224
  TESTNET_RPC_URL,
1201
1225
  Uint8ArrayToHex,
1226
+ addPrefix,
1202
1227
  getAllCoins,
1203
1228
  getMSafeConfig,
1204
1229
  getPublicKeyFromChain,