@msafe/sui3-sdk 0.0.5 → 0.0.6-pre-fdcc3ff.0
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 +258 -969
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +24 -89
- package/dist/index.d.ts +24 -89
- package/dist/index.js +251 -958
- package/dist/index.js.map +1 -1
- package/package.json +3 -1
- package/src/backend/BackendImpl.ts +196 -0
- package/src/backend/PseudoBackend.ts +25 -14
- package/src/backend/interface.ts +10 -10
- package/src/backend/types.ts +0 -21
- package/src/core/CreateHelper.ts +22 -12
- package/src/core/MSafeAccount.ts +9 -5
- package/src/core/MSafeClient.ts +2 -6
- package/src/globals/MSafeGlobals.ts +2 -1
- package/src/globals/const.ts +8 -1
- package/src/types/msafe.ts +0 -20
- package/src/utils/index.ts +0 -1
- package/src/utils/multi-sig.ts +0 -113
package/src/utils/multi-sig.ts
DELETED
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { PublicKey, SerializedSignature } from '@mysten/sui.js/cryptography';
|
|
2
|
-
import { Ed25519PublicKey } from '@mysten/sui.js/keypairs/ed25519';
|
|
3
|
-
import { MultiSigPublicKey } from '@mysten/sui.js/multisig';
|
|
4
|
-
|
|
5
|
-
import { MSafeAccountInfo } from '@/types/msafe';
|
|
6
|
-
import { stringToBuffer } from '@/utils/buffer';
|
|
7
|
-
|
|
8
|
-
export interface PublicKeyWithWeight {
|
|
9
|
-
publicKey: PublicKey;
|
|
10
|
-
weight: number;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
export interface MultiSigConfig {
|
|
14
|
-
threshold: number;
|
|
15
|
-
ownerWithWeight: PublicKeyWithWeight[];
|
|
16
|
-
creationNonce: number | undefined;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export const NONCE_PK_PREFIX = 'maven';
|
|
20
|
-
export const NONCE_PREFIX_MAX_SIZE = 16;
|
|
21
|
-
export const NONCE_PK_WEIGHT = 1;
|
|
22
|
-
|
|
23
|
-
export const MAX_WEIGHT = 255;
|
|
24
|
-
export const MIN_WEIGHT = 1;
|
|
25
|
-
export const MAX_OWNER_WITH_NONCE = 9;
|
|
26
|
-
export const MAX_OWNER_WITHOUT_NONCE = 10;
|
|
27
|
-
export const MIN_THRESHOLD = 1;
|
|
28
|
-
|
|
29
|
-
export class RawMultiSig {
|
|
30
|
-
rawMsPK: MultiSigPublicKey;
|
|
31
|
-
|
|
32
|
-
constructor(public readonly config: MultiSigConfig) {
|
|
33
|
-
this.rawMsPK = getMultiSigPublicKey(config);
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
static fromMSafeAccountInfo(info: MSafeAccountInfo) {
|
|
37
|
-
const parsed: MultiSigConfig = {
|
|
38
|
-
threshold: info.threshold,
|
|
39
|
-
ownerWithWeight: info.ownersWithWeightPK,
|
|
40
|
-
creationNonce: info.creationNonce,
|
|
41
|
-
};
|
|
42
|
-
return new RawMultiSig(parsed);
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
get suiAddress(): string {
|
|
46
|
-
return this.rawMsPK.toSuiAddress();
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
get publicKeys(): PublicKeyWithWeight[] {
|
|
50
|
-
return this.rawMsPK.getPublicKeys();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
get threshold(): number {
|
|
54
|
-
return this.config.threshold;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
combinePartialSignatures(signatures: SerializedSignature[]): SerializedSignature {
|
|
58
|
-
return this.rawMsPK.combinePartialSignatures(signatures);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
async verifyPersonalMessage(messageStr: string, multiSigSignature: SerializedSignature): Promise<boolean> {
|
|
62
|
-
const message = stringToBuffer(messageStr);
|
|
63
|
-
return this.rawMsPK.verifyPersonalMessage(message, multiSigSignature);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
export function getMultiSigPublicKey(config: MultiSigConfig) {
|
|
68
|
-
const { ownerWithWeight, threshold, creationNonce } = config;
|
|
69
|
-
const pks = ownerWithWeight.map((pk) => ({
|
|
70
|
-
publicKey: pk.publicKey,
|
|
71
|
-
weight: pk.weight,
|
|
72
|
-
}));
|
|
73
|
-
if (creationNonce !== undefined) {
|
|
74
|
-
pks.push({
|
|
75
|
-
publicKey: makeNoncePublicKey(creationNonce),
|
|
76
|
-
weight: NONCE_PK_WEIGHT,
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
return MultiSigPublicKey.fromPublicKeys({ threshold, publicKeys: pks });
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
export function makeNoncePublicKey(nonce: number) {
|
|
83
|
-
const buffer = new ArrayBuffer(Ed25519PublicKey.SIZE);
|
|
84
|
-
const textEncoder = new TextEncoder();
|
|
85
|
-
textEncoder.encodeInto(NONCE_PK_PREFIX, new Uint8Array(buffer, 0, NONCE_PREFIX_MAX_SIZE));
|
|
86
|
-
const nonceView = new DataView(buffer, NONCE_PREFIX_MAX_SIZE, 4);
|
|
87
|
-
nonceView.setUint32(0, nonce, true);
|
|
88
|
-
return new Ed25519PublicKey(new Uint8Array(buffer));
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
export function validateMultiSigConfig(config: MultiSigConfig) {
|
|
92
|
-
config.ownerWithWeight.forEach((pk) => {
|
|
93
|
-
const { weight } = pk;
|
|
94
|
-
if (weight < MIN_WEIGHT || weight > MAX_WEIGHT) {
|
|
95
|
-
throw new Error(`Invalid multi-sig weight: ${weight} (1-${MAX_WEIGHT})`);
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
const totalWeight = config.ownerWithWeight.reduce((s, pk) => s + pk.weight, 0);
|
|
99
|
-
if (config.threshold > totalWeight) {
|
|
100
|
-
throw new Error('Threshold is larger than total weight');
|
|
101
|
-
}
|
|
102
|
-
if (config.threshold < MIN_THRESHOLD) {
|
|
103
|
-
throw new Error('Threshold is smaller than 1');
|
|
104
|
-
}
|
|
105
|
-
const maxOwner = config.creationNonce === undefined ? MAX_OWNER_WITHOUT_NONCE : MAX_OWNER_WITH_NONCE;
|
|
106
|
-
if (config.ownerWithWeight.length > maxOwner) {
|
|
107
|
-
throw new Error('Owner number bigger than upper cap');
|
|
108
|
-
}
|
|
109
|
-
const addressSet = new Set(config.ownerWithWeight.map((pk) => pk.publicKey.toSuiAddress()));
|
|
110
|
-
if (addressSet.size !== config.ownerWithWeight.length) {
|
|
111
|
-
throw new Error('Duplicate address detected');
|
|
112
|
-
}
|
|
113
|
-
}
|