@sundaeswap/wallet-lite 0.0.77 → 0.0.79
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/classes/WalletAssetMap.class.js +49 -3
- package/dist/cjs/classes/WalletAssetMap.class.js.map +1 -1
- package/dist/cjs/react-components/hooks/useWalletData.js +25 -8
- package/dist/cjs/react-components/hooks/useWalletData.js.map +1 -1
- package/dist/esm/classes/WalletAssetMap.class.js +41 -3
- package/dist/esm/classes/WalletAssetMap.class.js.map +1 -1
- package/dist/esm/react-components/hooks/useWalletData.js +28 -17
- package/dist/esm/react-components/hooks/useWalletData.js.map +1 -1
- package/dist/types/classes/WalletAssetMap.class.d.ts +21 -0
- package/dist/types/classes/WalletAssetMap.class.d.ts.map +1 -1
- package/dist/types/react-components/hooks/useWalletData.d.ts +3 -2
- package/dist/types/react-components/hooks/useWalletData.d.ts.map +1 -1
- package/dist/types/tsconfig.build.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/classes/WalletAssetMap.class.ts +60 -3
- package/src/classes/__tests__/WalletAssetMap.test.ts +162 -0
- package/src/react-components/hooks/useWalletData.ts +45 -34
package/package.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { AssetAmount, IAssetAmountMetadata } from "@sundaeswap/asset";
|
|
2
2
|
|
|
3
|
+
import { ADA_ASSET_ID } from "src/constants.js";
|
|
3
4
|
import { normalizeAssetIdWithDot } from "../utils/assets.js";
|
|
4
5
|
|
|
5
6
|
/**
|
|
@@ -15,7 +16,9 @@ export class WalletAssetMap<
|
|
|
15
16
|
* @returns {AssetAmount<AssetMetadata> | undefined}
|
|
16
17
|
*/
|
|
17
18
|
get(key: string): AssetAmount<AssetMetadata> | undefined {
|
|
18
|
-
return super.get(
|
|
19
|
+
return super.get(
|
|
20
|
+
this.__normalizeIfAdaAssetId(normalizeAssetIdWithDot(key)),
|
|
21
|
+
);
|
|
19
22
|
}
|
|
20
23
|
|
|
21
24
|
/**
|
|
@@ -26,7 +29,12 @@ export class WalletAssetMap<
|
|
|
26
29
|
* @returns {this}
|
|
27
30
|
*/
|
|
28
31
|
set(key: string, value: AssetAmount<AssetMetadata>): this {
|
|
29
|
-
return super.set(
|
|
32
|
+
return super.set(
|
|
33
|
+
this.__normalizeIfAdaAssetId(normalizeAssetIdWithDot(key)),
|
|
34
|
+
value.metadata
|
|
35
|
+
? value.withMetadata(this.__normalizeIfAdaMetadata(value.metadata))
|
|
36
|
+
: value,
|
|
37
|
+
);
|
|
30
38
|
}
|
|
31
39
|
|
|
32
40
|
/**
|
|
@@ -36,6 +44,55 @@ export class WalletAssetMap<
|
|
|
36
44
|
* @returns {AssetAmount<AssetMetadata> | undefined}
|
|
37
45
|
*/
|
|
38
46
|
has(key: string): boolean {
|
|
39
|
-
return super.has(
|
|
47
|
+
return super.has(
|
|
48
|
+
this.__normalizeIfAdaAssetId(normalizeAssetIdWithDot(key)),
|
|
49
|
+
);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Overlay to enforce asset ID normalization.
|
|
54
|
+
*
|
|
55
|
+
* @param {string} key Delete the asset ID as a HEX string (either with dot or without).
|
|
56
|
+
* @returns {boolean}
|
|
57
|
+
*/
|
|
58
|
+
delete(key: string): boolean {
|
|
59
|
+
return super.delete(
|
|
60
|
+
this.__normalizeIfAdaAssetId(normalizeAssetIdWithDot(key)),
|
|
61
|
+
);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Helper function to normalize asset ID if it is an ADA asset name.
|
|
66
|
+
*
|
|
67
|
+
* @param {string} assetId The asset ID as a HEX string (either with dot or without).
|
|
68
|
+
* @returns {string}
|
|
69
|
+
*/
|
|
70
|
+
private __normalizeIfAdaAssetId(assetId: string): string {
|
|
71
|
+
const ADA_ASSET_IDS = [
|
|
72
|
+
"",
|
|
73
|
+
".",
|
|
74
|
+
"ada.lovelace",
|
|
75
|
+
"cardano.ada",
|
|
76
|
+
"616461.6c6f76656c616365",
|
|
77
|
+
];
|
|
78
|
+
|
|
79
|
+
if (ADA_ASSET_IDS.includes(assetId)) {
|
|
80
|
+
return ADA_ASSET_ID;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return assetId;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Helper function to normalize asset's metadata if it is ADA.
|
|
88
|
+
*
|
|
89
|
+
* @param {AssetMetadata} metadata The asset's metadata.
|
|
90
|
+
* @returns {AssetMetadata}
|
|
91
|
+
*/
|
|
92
|
+
private __normalizeIfAdaMetadata(metadata: AssetMetadata): AssetMetadata {
|
|
93
|
+
return {
|
|
94
|
+
...metadata,
|
|
95
|
+
assetId: this.__normalizeIfAdaAssetId(metadata.assetId),
|
|
96
|
+
};
|
|
40
97
|
}
|
|
41
98
|
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { AssetAmount } from "@sundaeswap/asset";
|
|
2
|
+
import { beforeEach, describe, expect, it } from "bun:test";
|
|
3
|
+
import { ADA_ASSET_ID } from "src/constants";
|
|
4
|
+
import { WalletAssetMap } from "../WalletAssetMap.class";
|
|
5
|
+
|
|
6
|
+
let am: WalletAssetMap;
|
|
7
|
+
|
|
8
|
+
const ADA_ASSET_IDS = [
|
|
9
|
+
"",
|
|
10
|
+
".",
|
|
11
|
+
"ada.lovelace",
|
|
12
|
+
"cardano.ada",
|
|
13
|
+
"616461.6c6f76656c616365",
|
|
14
|
+
];
|
|
15
|
+
|
|
16
|
+
beforeEach(() => {
|
|
17
|
+
am = new WalletAssetMap();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
describe("WalletAssetMap", () => {
|
|
21
|
+
describe("set()", () => {
|
|
22
|
+
it("should conform the map key to dot notation", () => {
|
|
23
|
+
expect(am.size).toEqual(0);
|
|
24
|
+
|
|
25
|
+
am.set(
|
|
26
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15524245525259",
|
|
27
|
+
new AssetAmount(100n, {
|
|
28
|
+
assetId:
|
|
29
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15.524245525259",
|
|
30
|
+
decimals: 0,
|
|
31
|
+
}),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
expect(am.size).toEqual(1);
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it("should conform the asset metadata to dot notation", () => {
|
|
38
|
+
expect(am.size).toEqual(0);
|
|
39
|
+
|
|
40
|
+
am.set(
|
|
41
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15.524245525259",
|
|
42
|
+
new AssetAmount(100n, {
|
|
43
|
+
assetId:
|
|
44
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15524245525259",
|
|
45
|
+
decimals: 0,
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
expect(
|
|
50
|
+
am.get(
|
|
51
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15.524245525259",
|
|
52
|
+
)?.metadata.assetId,
|
|
53
|
+
).toEqual(
|
|
54
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15.524245525259",
|
|
55
|
+
);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("should normalize the key if using an ada assetId", () => {
|
|
59
|
+
ADA_ASSET_IDS.forEach((id) => {
|
|
60
|
+
expect(am.size).toEqual(0);
|
|
61
|
+
|
|
62
|
+
am.set(
|
|
63
|
+
id,
|
|
64
|
+
new AssetAmount(100n, {
|
|
65
|
+
assetId: id,
|
|
66
|
+
decimals: 0,
|
|
67
|
+
}),
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
expect(am.size).toEqual(1);
|
|
71
|
+
expect(am.get(ADA_ASSET_ID)).not.toBeUndefined();
|
|
72
|
+
expect(am.get(ADA_ASSET_ID)?.metadata.assetId).toEqual(ADA_ASSET_ID);
|
|
73
|
+
const deleted = am.delete(ADA_ASSET_ID);
|
|
74
|
+
expect(deleted).toBeTrue();
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe("get()", () => {
|
|
80
|
+
it("should normalize the key to dot notation", () => {
|
|
81
|
+
expect(am.size).toEqual(0);
|
|
82
|
+
|
|
83
|
+
am.set(
|
|
84
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15.524245525259",
|
|
85
|
+
new AssetAmount(100n, {
|
|
86
|
+
assetId:
|
|
87
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15.524245525259",
|
|
88
|
+
decimals: 0,
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(am.size).toEqual(1);
|
|
93
|
+
|
|
94
|
+
expect(
|
|
95
|
+
am.get(
|
|
96
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15524245525259",
|
|
97
|
+
),
|
|
98
|
+
).not.toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it("should normalize the key if using an ada assetId", () => {
|
|
102
|
+
ADA_ASSET_IDS.forEach((id) => {
|
|
103
|
+
expect(am.size).toEqual(0);
|
|
104
|
+
|
|
105
|
+
am.set(
|
|
106
|
+
"ada.lovelace",
|
|
107
|
+
new AssetAmount(100n, {
|
|
108
|
+
assetId: "ada.lovelace",
|
|
109
|
+
decimals: 0,
|
|
110
|
+
}),
|
|
111
|
+
);
|
|
112
|
+
|
|
113
|
+
expect(am.size).toEqual(1);
|
|
114
|
+
expect(am.get(id)).not.toBeUndefined();
|
|
115
|
+
expect(am.get(id)?.metadata.assetId).toEqual(ADA_ASSET_ID);
|
|
116
|
+
const deleted = am.delete(id);
|
|
117
|
+
expect(deleted).toBeTrue();
|
|
118
|
+
});
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe("has()", () => {
|
|
123
|
+
it("should normalize the key to dot notation", () => {
|
|
124
|
+
expect(am.size).toEqual(0);
|
|
125
|
+
|
|
126
|
+
am.set(
|
|
127
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15.524245525259",
|
|
128
|
+
new AssetAmount(100n, {
|
|
129
|
+
assetId:
|
|
130
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15.524245525259",
|
|
131
|
+
decimals: 0,
|
|
132
|
+
}),
|
|
133
|
+
);
|
|
134
|
+
|
|
135
|
+
expect(am.size).toEqual(1);
|
|
136
|
+
|
|
137
|
+
expect(
|
|
138
|
+
am.has(
|
|
139
|
+
"99b071ce8580d6a3a11b4902145adb8bfd0d2a03935af8cf66403e15524245525259",
|
|
140
|
+
),
|
|
141
|
+
).toBeTrue();
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("should normalize the key if using an ada assetId", () => {
|
|
145
|
+
ADA_ASSET_IDS.forEach((id) => {
|
|
146
|
+
expect(am.size).toEqual(0);
|
|
147
|
+
|
|
148
|
+
am.set(
|
|
149
|
+
"ada.lovelace",
|
|
150
|
+
new AssetAmount(100n, {
|
|
151
|
+
assetId: "ada.lovelace",
|
|
152
|
+
decimals: 0,
|
|
153
|
+
}),
|
|
154
|
+
);
|
|
155
|
+
|
|
156
|
+
expect(am.size).toEqual(1);
|
|
157
|
+
expect(am.has(id)).toBeTrue();
|
|
158
|
+
am.delete(id);
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { Cip30WalletApi } from "@cardano-sdk/dapp-connector";
|
|
2
1
|
import { IAssetAmountMetadata } from "@sundaeswap/asset";
|
|
3
|
-
|
|
4
2
|
import { useCallback, useMemo, useState } from "react";
|
|
3
|
+
|
|
4
|
+
import { DataSignError } from "@cardano-sdk/dapp-connector";
|
|
5
5
|
import { useWalletObserver } from "./useWalletObserver";
|
|
6
6
|
|
|
7
7
|
export interface ISignedData {
|
|
@@ -17,7 +17,6 @@ export interface ISignedData {
|
|
|
17
17
|
export interface ISignDataParams<T = object | string> {
|
|
18
18
|
payload: T;
|
|
19
19
|
signingAddress: string;
|
|
20
|
-
connectedWallet: Cip30WalletApi;
|
|
21
20
|
}
|
|
22
21
|
|
|
23
22
|
export const useWalletData = <
|
|
@@ -25,6 +24,8 @@ export const useWalletData = <
|
|
|
25
24
|
>() => {
|
|
26
25
|
const state = useWalletObserver<AssetMetadata>();
|
|
27
26
|
const [signedData, setSignedData] = useState<ISignedData>();
|
|
27
|
+
const [isSigningData, setIsSigningData] = useState(false);
|
|
28
|
+
const [error, setError] = useState<DataSignError>();
|
|
28
29
|
|
|
29
30
|
const signData = useCallback(
|
|
30
31
|
async ({ signingAddress, payload }: ISignDataParams) => {
|
|
@@ -42,39 +43,47 @@ export const useWalletData = <
|
|
|
42
43
|
import("@emurgo/cardano-message-signing-nodejs"),
|
|
43
44
|
]);
|
|
44
45
|
|
|
45
|
-
|
|
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
|
-
);
|
|
46
|
+
setIsSigningData(true);
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
48
|
+
try {
|
|
49
|
+
const response = await state.observer.api.signData(
|
|
50
|
+
Buffer.from(
|
|
51
|
+
Cardano.Address.fromBech32(signingAddress).toBytes(),
|
|
52
|
+
).toString("hex"),
|
|
53
|
+
Buffer.from(
|
|
54
|
+
typeof payload === "string"
|
|
55
|
+
? payload
|
|
56
|
+
: JSON.stringify(payload, null, 0),
|
|
57
|
+
"utf-8",
|
|
58
|
+
).toString("hex"),
|
|
59
|
+
);
|
|
64
60
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
).toString("hex"),
|
|
73
|
-
rawData: { cborKey: response.key, cborSignature: response.signature },
|
|
74
|
-
};
|
|
61
|
+
const coseSign1 = COSESign1.from_bytes(
|
|
62
|
+
new Uint8Array(Buffer.from(response.signature, "hex")),
|
|
63
|
+
);
|
|
64
|
+
const keySign = COSEKey.from_bytes(
|
|
65
|
+
new Uint8Array(Buffer.from(response.key, "hex")),
|
|
66
|
+
);
|
|
67
|
+
const labelInt = Int.new_negative(BigNum.from_str("2"));
|
|
75
68
|
|
|
76
|
-
|
|
77
|
-
|
|
69
|
+
const data: ISignedData = {
|
|
70
|
+
payload: Buffer.from(coseSign1.signed_data().to_bytes()).toString(
|
|
71
|
+
"hex",
|
|
72
|
+
),
|
|
73
|
+
signature: Ed25519Signature.fromBytes(coseSign1.signature()).hex(),
|
|
74
|
+
key: Buffer.from(
|
|
75
|
+
keySign.header(Label.new_int(labelInt))?.as_bytes() || [],
|
|
76
|
+
).toString("hex"),
|
|
77
|
+
rawData: { cborKey: response.key, cborSignature: response.signature },
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
setSignedData(data);
|
|
81
|
+
setIsSigningData(false);
|
|
82
|
+
return data;
|
|
83
|
+
} catch (e) {
|
|
84
|
+
setIsSigningData(false);
|
|
85
|
+
setError(e as DataSignError);
|
|
86
|
+
}
|
|
78
87
|
},
|
|
79
88
|
[state.observer.api],
|
|
80
89
|
);
|
|
@@ -83,7 +92,9 @@ export const useWalletData = <
|
|
|
83
92
|
() => ({
|
|
84
93
|
signData,
|
|
85
94
|
signedData,
|
|
95
|
+
isSigningData,
|
|
96
|
+
error,
|
|
86
97
|
}),
|
|
87
|
-
[signData, signedData],
|
|
98
|
+
[signData, signedData, isSigningData, error],
|
|
88
99
|
);
|
|
89
100
|
};
|