@msafe/sui3-sdk 0.0.8 → 0.0.10
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 +94 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +103 -57
- package/dist/index.d.ts +103 -57
- package/dist/index.js +92 -11
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
- package/src/backend/BackendImpl.ts +33 -5
- package/src/backend/PseudoBackend.ts +22 -1
- package/src/backend/interface.ts +6 -1
- package/src/core/AddressBookSDK.ts +18 -0
- package/src/core/MSafeAccount.ts +2 -3
- package/src/core/MSafeClient.ts +23 -9
- package/src/core/MessageHelper.ts +28 -0
- package/src/core/index.ts +1 -0
- package/src/types/address-book.ts +32 -0
- package/src/{backend/types.ts → types/backend.ts} +1 -1
- package/src/types/index.ts +3 -0
- package/src/types/pagination.ts +15 -0
package/src/core/MSafeClient.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import { MSafeAccountInfo } from '@msafe/sui3-utils';
|
|
2
2
|
|
|
3
|
-
import {
|
|
3
|
+
import { AddressBookSDK } from '@/core/AddressBookSDK';
|
|
4
4
|
import { CreateHelper } from '@/core/CreateHelper';
|
|
5
5
|
import { MessageHelper } from '@/core/MessageHelper';
|
|
6
6
|
import { MSafeAccount } from '@/core/MSafeAccount';
|
|
7
7
|
import { PublicKeyHelper } from '@/core/PublicKeyHelper';
|
|
8
8
|
import { MSafeConfigOptions, MSafeEnv } from '@/globals/const';
|
|
9
9
|
import { MSafeGlobals } from '@/globals/MSafeGlobals';
|
|
10
|
+
import { JWTToken } from '@/types/backend';
|
|
10
11
|
import { CreateMSafeAccountInfo } from '@/types/creation';
|
|
11
12
|
import { IWallet } from '@/types/wallet';
|
|
12
13
|
|
|
@@ -23,24 +24,33 @@ export class MSafeClient {
|
|
|
23
24
|
}
|
|
24
25
|
|
|
25
26
|
async connectWallet(input: { wallet: IWallet; jwtToken?: JWTToken }): Promise<JWTToken> {
|
|
27
|
+
if (input.jwtToken) {
|
|
28
|
+
const verified = await this.backend.verifyToken(input.jwtToken);
|
|
29
|
+
if (verified) {
|
|
30
|
+
this.globals.wallet = input.wallet;
|
|
31
|
+
this.backend.setJWTToken(input.jwtToken);
|
|
32
|
+
return input.jwtToken;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
const jwt = await this.authSign(input.wallet);
|
|
26
36
|
this.globals.wallet = input.wallet;
|
|
37
|
+
this.backend.setJWTToken(jwt);
|
|
38
|
+
return jwt;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
private async authSign(wallet: IWallet): Promise<JWTToken> {
|
|
27
42
|
const messageStr = MessageHelper.welcomeMessage(new Date().toUTCString());
|
|
28
|
-
const sig = await
|
|
43
|
+
const sig = await wallet.signPersonalMessage({
|
|
29
44
|
messageStr,
|
|
30
45
|
});
|
|
31
46
|
return this.backend.authSign({
|
|
32
|
-
address: await
|
|
47
|
+
address: await wallet.address(),
|
|
33
48
|
message: messageStr,
|
|
34
49
|
signature: sig.signature,
|
|
35
|
-
walletType:
|
|
50
|
+
walletType: wallet.walletType,
|
|
36
51
|
});
|
|
37
52
|
}
|
|
38
53
|
|
|
39
|
-
async setWallet(input: { wallet: IWallet; jwtToken: JWTToken }) {
|
|
40
|
-
this.globals.wallet = input.wallet;
|
|
41
|
-
this.backend.setJWTToken(input.jwtToken);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
54
|
async userInfo() {
|
|
45
55
|
return this.globals.backend.getUserInfo(await this.walletAddress());
|
|
46
56
|
}
|
|
@@ -86,6 +96,10 @@ export class MSafeClient {
|
|
|
86
96
|
return this.globals.wallet;
|
|
87
97
|
}
|
|
88
98
|
|
|
99
|
+
get AddressBook(): AddressBookSDK {
|
|
100
|
+
return new AddressBookSDK(this.globals);
|
|
101
|
+
}
|
|
102
|
+
|
|
89
103
|
private async walletAddress() {
|
|
90
104
|
return this.wallet.address();
|
|
91
105
|
}
|
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
import { normalizeSuiAddress } from '@mysten/sui.js/utils';
|
|
2
|
+
import { MD5 } from 'crypto-js';
|
|
3
|
+
|
|
1
4
|
import { IntentionHelper, TxIntention } from '@/transactions/intention';
|
|
5
|
+
import { UpdateAddressBookEntry } from '@/types/address-book';
|
|
6
|
+
import { stringToBuffer } from '@/utils';
|
|
2
7
|
|
|
3
8
|
export class MessageHelper {
|
|
4
9
|
// Message used for MSafe creation
|
|
@@ -50,6 +55,29 @@ export class MessageHelper {
|
|
|
50
55
|
intention: IntentionHelper.de(intentionData),
|
|
51
56
|
};
|
|
52
57
|
}
|
|
58
|
+
|
|
59
|
+
static updateAddressBookMessage(updates: UpdateAddressBookEntry[]) {
|
|
60
|
+
const normalized = updates.map((update) => ({
|
|
61
|
+
...update,
|
|
62
|
+
address: normalizeSuiAddress(update.address),
|
|
63
|
+
}));
|
|
64
|
+
function sortObjectKeys(obj: Record<string, any>): Record<string, any> {
|
|
65
|
+
return Object.keys(obj)
|
|
66
|
+
.sort()
|
|
67
|
+
.reduce((result, key) => ({ ...result, [key]: obj[key] }), {} as Record<string, any>);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Sort keys of each object in the updates array
|
|
71
|
+
const sortedUpdates = normalized.map((update) => sortObjectKeys(update));
|
|
72
|
+
const raw = JSON.stringify(sortedUpdates, null, 2);
|
|
73
|
+
const encoded = stringToBuffer(raw);
|
|
74
|
+
if (encoded.length <= 1024) {
|
|
75
|
+
// BCS signing can handle this amount of data, directly return
|
|
76
|
+
return raw;
|
|
77
|
+
}
|
|
78
|
+
const md5 = MD5(raw);
|
|
79
|
+
return `Bulk address book update: ${md5}`;
|
|
80
|
+
}
|
|
53
81
|
}
|
|
54
82
|
|
|
55
83
|
export interface ProposeIntentionData {
|
package/src/core/index.ts
CHANGED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { PagedResult } from '@/types/pagination';
|
|
2
|
+
|
|
3
|
+
export enum OpAddressBookType {
|
|
4
|
+
Delete = 'delete',
|
|
5
|
+
Upsert = 'upsert',
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export type UpdateAddressBookEntry = DeleteAddressBookEntry | UpsertAddressBookEntry;
|
|
9
|
+
|
|
10
|
+
export interface DeleteAddressBookEntry {
|
|
11
|
+
opType: OpAddressBookType.Delete;
|
|
12
|
+
address: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface UpsertAddressBookEntry {
|
|
16
|
+
opType: OpAddressBookType.Upsert;
|
|
17
|
+
address: string;
|
|
18
|
+
addressName: string;
|
|
19
|
+
remark?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export type GetAddressBookResult = PagedResult<GetAddressBookResultEntry>;
|
|
23
|
+
|
|
24
|
+
export interface GetAddressBookResultEntry {
|
|
25
|
+
address: string;
|
|
26
|
+
|
|
27
|
+
addressName: string;
|
|
28
|
+
|
|
29
|
+
remark?: string;
|
|
30
|
+
|
|
31
|
+
updatedAt: Date;
|
|
32
|
+
}
|
package/src/types/index.ts
CHANGED