@sundaeswap/wallet-lite 0.0.76 → 0.0.77
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/cjs/react-components/WalletObserverProvider/hooks/useWalletObserverState.js +47 -47
- package/dist/cjs/react-components/WalletObserverProvider/hooks/useWalletObserverState.js.map +1 -1
- package/dist/cjs/react-components/hooks/index.js +11 -0
- package/dist/cjs/react-components/hooks/index.js.map +1 -1
- package/dist/cjs/react-components/hooks/useWalletData.js +95 -0
- package/dist/cjs/react-components/hooks/useWalletData.js.map +1 -0
- package/dist/esm/react-components/WalletObserverProvider/hooks/useWalletObserverState.js +9 -9
- package/dist/esm/react-components/WalletObserverProvider/hooks/useWalletObserverState.js.map +1 -1
- package/dist/esm/react-components/hooks/index.js +1 -0
- package/dist/esm/react-components/hooks/index.js.map +1 -1
- package/dist/esm/react-components/hooks/useWalletData.js +45 -0
- package/dist/esm/react-components/hooks/useWalletData.js.map +1 -0
- package/dist/types/react-components/WalletObserverProvider/hooks/useWalletObserverState.d.ts.map +1 -1
- package/dist/types/react-components/hooks/index.d.ts +1 -0
- package/dist/types/react-components/hooks/index.d.ts.map +1 -1
- package/dist/types/react-components/hooks/useWalletData.d.ts +21 -0
- package/dist/types/react-components/hooks/useWalletData.d.ts.map +1 -0
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +3 -2
- package/src/react-components/WalletObserverProvider/hooks/useWalletObserverState.ts +14 -14
- package/src/react-components/hooks/index.ts +1 -0
- package/src/react-components/hooks/useWalletData.ts +89 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sundaeswap/wallet-lite",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.77",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -49,7 +49,8 @@
|
|
|
49
49
|
"lodash": "^4.17.21",
|
|
50
50
|
"@cardano-sdk/core": "^0.45.0",
|
|
51
51
|
"@cardano-sdk/dapp-connector": "^0.13.3",
|
|
52
|
-
"@cardano-sdk/util": "^0.15.5"
|
|
52
|
+
"@cardano-sdk/util": "^0.15.5",
|
|
53
|
+
"@emurgo/cardano-message-signing-nodejs": "^1.1.0"
|
|
53
54
|
},
|
|
54
55
|
"devDependencies": {
|
|
55
56
|
"@babel/core": "^7.24.7",
|
|
@@ -60,6 +60,20 @@ export const useWalletObserverState = <
|
|
|
60
60
|
setWillAutoConnect(false);
|
|
61
61
|
}, [observer]);
|
|
62
62
|
|
|
63
|
+
const connectWallet = useCallback(
|
|
64
|
+
async (wallet: string) => {
|
|
65
|
+
if (observer.hasActiveConnection() && wallet !== observer.activeWallet) {
|
|
66
|
+
setSwitching(() => true);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
await observer.connectWallet(wallet);
|
|
70
|
+
await syncWallet();
|
|
71
|
+
setSwitching(() => false);
|
|
72
|
+
return observer.api;
|
|
73
|
+
},
|
|
74
|
+
[observer, setSwitching],
|
|
75
|
+
);
|
|
76
|
+
|
|
63
77
|
const syncWallet = useCallback(async () => {
|
|
64
78
|
if (observer.isSyncing() || !observer.hasActiveConnection()) {
|
|
65
79
|
return;
|
|
@@ -165,20 +179,6 @@ export const useWalletObserverState = <
|
|
|
165
179
|
}
|
|
166
180
|
}, [observer, disconnect]);
|
|
167
181
|
|
|
168
|
-
const connectWallet = useCallback(
|
|
169
|
-
async (wallet: string) => {
|
|
170
|
-
if (observer.hasActiveConnection() && wallet !== observer.activeWallet) {
|
|
171
|
-
setSwitching(() => true);
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
await observer.connectWallet(wallet);
|
|
175
|
-
await syncWallet();
|
|
176
|
-
setSwitching(() => false);
|
|
177
|
-
return observer.api;
|
|
178
|
-
},
|
|
179
|
-
[observer, setSwitching],
|
|
180
|
-
);
|
|
181
|
-
|
|
182
182
|
/**
|
|
183
183
|
* Ensure the wallet syncs on connect and disconnect.
|
|
184
184
|
*/
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Cip30WalletApi } from "@cardano-sdk/dapp-connector";
|
|
2
|
+
import { IAssetAmountMetadata } from "@sundaeswap/asset";
|
|
3
|
+
|
|
4
|
+
import { useCallback, useMemo, useState } from "react";
|
|
5
|
+
import { useWalletObserver } from "./useWalletObserver";
|
|
6
|
+
|
|
7
|
+
export interface ISignedData {
|
|
8
|
+
payload: string;
|
|
9
|
+
signature: string;
|
|
10
|
+
key: string;
|
|
11
|
+
rawData: {
|
|
12
|
+
cborKey: string;
|
|
13
|
+
cborSignature: string;
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ISignDataParams<T = object | string> {
|
|
18
|
+
payload: T;
|
|
19
|
+
signingAddress: string;
|
|
20
|
+
connectedWallet: Cip30WalletApi;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export const useWalletData = <
|
|
24
|
+
AssetMetadata extends IAssetAmountMetadata = IAssetAmountMetadata,
|
|
25
|
+
>() => {
|
|
26
|
+
const state = useWalletObserver<AssetMetadata>();
|
|
27
|
+
const [signedData, setSignedData] = useState<ISignedData>();
|
|
28
|
+
|
|
29
|
+
const signData = useCallback(
|
|
30
|
+
async ({ signingAddress, payload }: ISignDataParams) => {
|
|
31
|
+
if (!state.observer.api) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
const [
|
|
36
|
+
{ Cardano },
|
|
37
|
+
{ Ed25519Signature },
|
|
38
|
+
{ COSESign1, COSEKey, Int, BigNum, Label },
|
|
39
|
+
] = await Promise.all([
|
|
40
|
+
import("@cardano-sdk/core"),
|
|
41
|
+
import("@cardano-sdk/crypto"),
|
|
42
|
+
import("@emurgo/cardano-message-signing-nodejs"),
|
|
43
|
+
]);
|
|
44
|
+
|
|
45
|
+
const response = await state.observer.api.signData(
|
|
46
|
+
Buffer.from(
|
|
47
|
+
Cardano.Address.fromBech32(signingAddress).toBytes(),
|
|
48
|
+
).toString("hex"),
|
|
49
|
+
Buffer.from(
|
|
50
|
+
typeof payload === "string"
|
|
51
|
+
? payload
|
|
52
|
+
: JSON.stringify(payload, null, 0),
|
|
53
|
+
"utf-8",
|
|
54
|
+
).toString("hex"),
|
|
55
|
+
);
|
|
56
|
+
|
|
57
|
+
const coseSign1 = COSESign1.from_bytes(
|
|
58
|
+
new Uint8Array(Buffer.from(response.signature, "hex")),
|
|
59
|
+
);
|
|
60
|
+
const keySign = COSEKey.from_bytes(
|
|
61
|
+
new Uint8Array(Buffer.from(response.key, "hex")),
|
|
62
|
+
);
|
|
63
|
+
const labelInt = Int.new_negative(BigNum.from_str("2"));
|
|
64
|
+
|
|
65
|
+
const data: ISignedData = {
|
|
66
|
+
payload: Buffer.from(coseSign1.signed_data().to_bytes()).toString(
|
|
67
|
+
"hex",
|
|
68
|
+
),
|
|
69
|
+
signature: Ed25519Signature.fromBytes(coseSign1.signature()).hex(),
|
|
70
|
+
key: Buffer.from(
|
|
71
|
+
keySign.header(Label.new_int(labelInt))?.as_bytes() || [],
|
|
72
|
+
).toString("hex"),
|
|
73
|
+
rawData: { cborKey: response.key, cborSignature: response.signature },
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
setSignedData(data);
|
|
77
|
+
return data;
|
|
78
|
+
},
|
|
79
|
+
[state.observer.api],
|
|
80
|
+
);
|
|
81
|
+
|
|
82
|
+
return useMemo(
|
|
83
|
+
() => ({
|
|
84
|
+
signData,
|
|
85
|
+
signedData,
|
|
86
|
+
}),
|
|
87
|
+
[signData, signedData],
|
|
88
|
+
);
|
|
89
|
+
};
|