Repository: starknet-io/starknet.js (TypeScript)

Issue:
`encodeType` of typed data does not encode preset types correctly
**Describe the bug**
`encodeType` of type data does not encode preset types correctly. 

You can verify this from the SNIP:
- [X is an u256](https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-12.md#when-x-is-a-nft-id)
- [X is a TokenAmount](https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-12.md#when-x-is-a-token-amount)
- [X is a Nft ID](https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-12.md#when-x-is-a-nft-id)

Where for every preset type, it says:
> It will be encoded as the following object.

And per the [X is an object](https://github.com/starknet-io/SNIPs/blob/main/SNIPS/snip-12.md#when-x-is-an-object), every object should encoded as follows:
> `escape(name) || "(" || escape(param1_name) || ":" || escape(param1_type) || "," || ... || escape(paramN_name) || ":"|| escape(paramN_type) || ")"`
> If the object references other objects/enum which can also reference other objects/enums, the set of referenced objects/enums is collected, sorted by name, and appended to the encoding.

The spec also refers to [this repo](https://github.com/argentlabs/starknet-off-chain-signature) for examples, where preset type `u256` is [included along with its properties](https://github.com/argentlabs/starknet-off-chain-signature/blob/19ad573c0def1dc2f0a08d3d63fabdbccab1d6f6/src/struct_with_u256.cairo#L12).

**To Reproduce**
Run this test:
https://github.com/starknet-io/starknet.js/blob/fdcc7fbf5957c14df765fd78508db6539b6124e3/__tests__/utils/typedData.test.ts#L50

**Expected behavior**
All preset types are encoded as objects:
```
"Example"("n0":"TokenAmount","n1":"NftId")"NftId"("collection_address":"ContractAddress","token_id":"u256")"TokenAmount"("token_address":"ContractAddress","amount":"u256")"u256"("low":"u128","high":"u128")
```
**Actual behavior**
All preset types are encoded as basic types:
```
"Example"("n0":"TokenAmount","n1":"NftId")
```

**Desktop (please complete the following information):**

- Starknet.js version cb2059039e3b42501cdfefec7802da83eb73645b


Discussion:
Note that in the current code, following the SNIP notation, **encode_type** is treated as a simple type but **encoding** is applied as for a custom type/object.

@sgc-code Could you confirm that **encode_type** should be applied the same as for a custom type? I recall being conflicted about this while working on #920 since either approach is essentially a constant but it seems I didn't remember to explicitly ask about it.
:tada: This issue has been resolved in version 6.4.0 :tada:

The release is available on:
- [npm package (@next dist-tag)](https://www.npmjs.com/package/starknet/v/6.4.0)
- [GitHub release](https://github.com/starknet-io/starknet.js/releases/tag/v6.4.0)

Your **[semantic-release](https://github.com/semantic-release/semantic-release)** bot :package::rocket:
:tada: This issue has been resolved in version 7.0.0 :tada:

The release is available on:
- [npm package (@next dist-tag)](https://www.npmjs.com/package/starknet/v/7.0.0)
- [GitHub release](https://github.com/starknet-io/starknet.js/releases/tag/v7.0.0)

Your **[semantic-release](https://github.com/semantic-release/semantic-release)** bot :package::rocket:



Patch:
diff --git a/src/utils/typedData.ts b/src/utils/typedData.ts
index 3dfb99a5f..d1193b29c 100644
--- a/src/utils/typedData.ts
+++ b/src/utils/typedData.ts
@@ -188,14 +188,24 @@ export function encodeType(
   type: string,
   revision: Revision = Revision.Legacy
 ): string {
-  const [primary, ...dependencies] = getDependencies(types, type, undefined, undefined, revision);
+  const allTypes =
+    revision === Revision.Active
+      ? { ...types, ...revisionConfiguration[revision].presetTypes }
+      : types;
+  const [primary, ...dependencies] = getDependencies(
+    allTypes,
+    type,
+    undefined,
+    undefined,
+    revision
+  );
   const newTypes = !primary ? [] : [primary, ...dependencies.sort()];
 
   const esc = revisionConfiguration[revision].escapeTypeString;
 
   return newTypes
     .map((dependency) => {
-      const dependencyElements = types[dependency].map((t) => {
+      const dependencyElements = allTypes[dependency].map((t) => {
         const targetType =
           t.type === 'enum' && revision === Revision.Active
             ? (t as StarkNetEnumType).contains


Test patch:
diff --git a/__tests__/utils/typedData.test.ts b/__tests__/utils/typedData.test.ts
index 9ede00d77..c09f0ba65 100644
--- a/__tests__/utils/typedData.test.ts
+++ b/__tests__/utils/typedData.test.ts
@@ -47,7 +47,7 @@ describe('typedData', () => {
     );
     encoded = encodeType(examplePresetTypes.types, 'Example', TypedDataRevision.Active);
     expect(encoded).toMatchInlineSnapshot(
-      `"\\"Example\\"(\\"n0\\":\\"TokenAmount\\",\\"n1\\":\\"NftId\\")"`
+      `"\\"Example\\"(\\"n0\\":\\"TokenAmount\\",\\"n1\\":\\"NftId\\")\\"NftId\\"(\\"collection_address\\":\\"ContractAddress\\",\\"token_id\\":\\"u256\\")\\"TokenAmount\\"(\\"token_address\\":\\"ContractAddress\\",\\"amount\\":\\"u256\\")\\"u256\\"(\\"low\\":\\"u128\\",\\"high\\":\\"u128\\")"`
     );
     encoded = encodeType(exampleEnum.types, 'Example', TypedDataRevision.Active);
     expect(encoded).toMatchInlineSnapshot(
@@ -87,7 +87,7 @@ describe('typedData', () => {
     );
     typeHash = getTypeHash(examplePresetTypes.types, 'Example', TypedDataRevision.Active);
     expect(typeHash).toMatchInlineSnapshot(
-      `"0x155de33c6a0cc7f2b8926afc7a71fc2ac31ffc26726aee5da0570c5d517a763"`
+      `"0x1a25a8bb84b761090b1fadaebe762c4b679b0d8883d2bedda695ea340839a55"`
     );
     typeHash = getTypeHash(exampleEnum.types, 'Example', TypedDataRevision.Active);
     expect(typeHash).toMatchInlineSnapshot(
@@ -279,7 +279,7 @@ describe('typedData', () => {
 
     messageHash = getMessageHash(examplePresetTypes, exampleAddress);
     expect(messageHash).toMatchInlineSnapshot(
-      `"0x26e7b8cedfa63cdbed14e7e51b60ee53ac82bdf26724eb1e3f0710cb8987522"`
+      `"0x185b339d5c566a883561a88fb36da301051e2c0225deb325c91bb7aa2f3473a"`
     );
 
     messageHash = getMessageHash(exampleEnum, exampleAddress);