# Dart SDK/CLI Updates

**URL:** <http://forum.zenon.org/t/dart-sdk-cli-updates/1166>\
**Category:** Development\
**Created:** [December 17, 2022, 4:52am UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166 "2022-12-17T04:52:42Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![vilkris](http://forum.zenon.org/user_avatar/forum.zenon.org/vilkris/32/146_2.png) [@vilkris](http://forum.zenon.org/u/vilkris)\
**Post date:** [January 5, 2023, 8:15pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/21 "2023-01-05T20:15:08Z")

</div>

> [@CryptoFish](#):
>
> In the case of the Atomic Swap scenario Bob needs to create an htlc with a hashlock that already exists. How would Bob be able to supply the hashlock if the htlc.create command generates the preimage?

In the Dart CLI the htlc.create command has an optional hashlock parameter. If it’s provided it’ll use that as the hashlock and won’t generate a preimage.

> [@CryptoFish](#):
>
> According to @georgezgeorgez the `KeyMaxSize` was introduced to prevent [Secret Size Attacks](https://gist.github.com/markblundeberg/7a932c98179de2190049f5823907c016). It puts a guarantee on max size of the unlock tx. For other networks there is tx size limit. We have to pay plasma per byte.  
> So the bounded size ensures that Bob knows he’ll be able to redeem his, if Alice redeems hers.

Thanks for the context, wasn’t aware of this. Looks like it’s best to add an optional KeyMaxSize param to the htlc.create command in the Dart CLI as well.

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 5, 2023, 8:55pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/22 "2023-01-05T20:55:21Z")

</div>

> [@vilkris](#):
>
> In the Dart CLI the htlc.create command has an optional hashlock parameter. If it’s provided it’ll use that as the hashlock and won’t generate a preimage.

Right, got it. I’ll update the C# CLI accordingly and remove the incompatibility issues.

---

<div class="post-metadata">

**Author:** ![sol](http://forum.zenon.org/user_avatar/forum.zenon.org/sol/32/1636_2.png) [@sol](http://forum.zenon.org/u/sol)\
**Post date:** [January 6, 2023, 6:24pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/23 "2023-01-06T18:24:00Z")

</div>

> [@vilkris](#):
>
> In the Dart CLI there’s a hardcoded variable `int hashLockLength = 32;` which is used to set `KeyMaxSize` when creating the HTLC. This is misleading as we’re setting the max length of the preimage, not the hashlock length.

I’ll update this, but I think we should also [check the length of the hashLock](https://github.com/Big-Inches-Club-House/go-zenon/blob/htlc/vm/embedded/implementation/htlc.go#L25).

The variables htlcPreimageMinLength (32) and htlcPreimageMaxLength (32) aren’t enforced in either znn\_sdk\_dart or go-zenon. I was able to create a 99-byte preimage and unlock an HTLC with it.

Any suggestions for lower/upper boundaries or should we enforce 32/32?

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 7, 2023, 9:57am UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/24 "2023-01-07T09:57:51Z")

</div>

I don’t see the problem with the [check the length of the hashLock](https://github.com/Big-Inches-Club-House/go-zenon/blob/htlc/vm/embedded/implementation/htlc.go#L25).

The `hashLock` length is determined by the output size of the `hashType`. Both hash types supported use a 256 bit output size. The `hashLock` is not valid if it does not equal 32 bytes in length.

> [@sol](#):
>
> Any suggestions for lower/upper boundaries or should we enforce 32/32?

How the preimage is generated is determined by the implementation of the calling application. I’m not sure what the benefits would be by enforcing its length.

The Dart CLI htlc.unlock command uses an extra hashType argument to do a preimage check. Wouldn’t it be easier to just use the `hashType` already available in the retrieved htlc object?

---

<div class="post-metadata">

**Author:** ![sol](http://forum.zenon.org/user_avatar/forum.zenon.org/sol/32/1636_2.png) [@sol](http://forum.zenon.org/u/sol)\
**Post date:** [January 7, 2023, 1:14pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/25 "2023-01-07T13:14:37Z")

</div>

> [@CryptoFish](#):
>
> I don’t see the problem with the [check the length of the hashLock](https://github.com/Big-Inches-Club-House/go-zenon/blob/htlc/vm/embedded/implementation/htlc.go#L25).

Right, no issues in go-zenon. I should have specified – I’d like to add matching client-side input validation in znn\_cli\_dart and syrius.

> [@CryptoFish](#):
>
> How the preimage is generated is determined by the implementation of the calling application.

We probably don’t want to allow weak preimages; I’d imagine some actors will try to bruteforce active swaps.  
I guess we don’t need to enforce the limit in the protocol (go-zenon) but it could help to do so in order to align all SDK/client applications with the same rule.

> [@CryptoFish](#):
>
> Wouldn’t it be easier to just use the `hashType` already available in the retrieved htlc object?

Yes, it would be easier but I found that is error-prone. If a user submits a pre-generated SHA-256 hashlock but forgets to enter the type in htlc.create, their type will default to 0. This optional flag in hltc.unlock allows them to recover the funds without having to wait for the swap to expire.

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 7, 2023, 2:25pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/26 "2023-01-07T14:25:21Z")

</div>

> [@sol](#):
>
> Right, no issues in go-zenon. I should have specified – I’d like to add matching client-side input validation in znn\_cli\_dart and syrius.

Doesn’t this already happen when creating the `hash` object from the Zenon Sdk which indirectly enforces the length of the hashLock. The `parse` and `fromBytes` methods perform a validation on the length.

> [@sol](#):
>
> We probably don’t want to allow weak preimages; I’d imagine some actors will try to bruteforce active swaps.  
> I guess we don’t need to enforce the limit in the protocol (go-zenon) but it could help to do so in order to align all SDK/client applications with the same rule.

Whether weak preimages are used depends on the client implementation. It makes sense to make recommendations on how to generate preimages. Also users of x client need to know that the preimages the client generates are considered secure. This should prevent weak preimages from being created, unless one creates them directly using the sdk.

Enforcing the limit in the client won’t stop someone from bruteforce active swaps. Again, one could easily use the sdk directly.

I guess if it makes sense to enforce the limits it has to be done in the protocol.

> [@sol](#):
>
> Yes, it would be easier but I found that is error-prone. If a user submits a pre-generated SHA-256 hashlock but forgets to enter the type in htlc.create, their type will default to 0. This optional flag in hltc.unlock allows them to recover the funds without having to wait for the swap to expire.

I see, good thinking.

---

<div class="post-metadata">

**Author:** ![sol](http://forum.zenon.org/user_avatar/forum.zenon.org/sol/32/1636_2.png) [@sol](http://forum.zenon.org/u/sol)\
**Post date:** [January 7, 2023, 2:46pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/27 "2023-01-07T14:46:55Z")

</div>

> [@CryptoFish](#):
>
> Doesn’t this already happen when creating the `hash` object from the Zenon Sdk which indirectly enforces the length of the hashLock. The `parse` and `fromBytes` methods perform a validation on the length.

I checked the code again; you’re probably right and doing a client-side length check seems redundant.

> [@CryptoFish](#):
>
> Also users of x client need to know that the preimages the client generates are considered secure.

Just want to be sure we’re all on the same page.  
The protocol and SDKs will not have any preimage length restrictions.  
Clients like znn-cli and syrius will impose min/max preimage lengths.

What should the min/max be? I was assuming 32/32 until yesterday.

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 7, 2023, 5:17pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/28 "2023-01-07T17:17:52Z")

</div>

> [@sol](#):
>
> Just want to be sure we’re all on the same page.

We’re on the same page 🙂

I personally don’t think the limits add much, but if we add them 32/32 seems like the most sensible limit.

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 8, 2023, 10:10am UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/29 "2023-01-08T10:10:42Z")

</div>

> [@sol](#):
>
> Yes, it would be easier but I found that is error-prone. If a user submits a pre-generated SHA-256 hashlock but forgets to enter the type in htlc.create, their type will default to 0. This optional flag in hltc.unlock allows them to recover the funds without having to wait for the swap to expire.

Let me get back on this one. The htlc unlock contract creates a SHA3-256 hashlock of the supplied preimage which will never match the SHA-256 hashlock. So I fail to see how someone could recover the funds without having to wait for the swap to expire.

```auto
// Alice creates a SHA-256 hash
znn-cli createHash 1

```

Preimage: c770b3160b83f6e3a7e45e961da8aaafbdc1557bb2f4dee3ae16d370e10fe16d  
Hash: 3b6e7bfafa188dff9f50b12be1143c751653453a27097cd297f2f283e7e804f3

```auto
// Alice creates a htlc with a pre-generated SHA-256 hashlock, but forgets to specify the hashtype
znn-cli htlc.create z1qpsjv3wzzuuzdudg7tf6uhvr6sk4ag8me42ua4 ZNN 100 3600 3b6e7bfafa188dff9f50b12be1143c751653453a27097cd297f2f283e7e804f3 -k Alice

```

Creating htlc with amount 100,00000000 tZNN  
Can be reclaimed in 01:00:00 by z1qqjnwjjpnue8xmmpanz6csze6tcmtzzdtfsww7  
Can be unlocked by z1qpsjv3wzzuuzdudg7tf6uhvr6sk4ag8me42ua4 with hashlock 3b6e7bfafa188dff9f50b12be1143c751653453a27097cd297f2f283e7e804f3 hashtype 0  
Done

```auto
// Bob checks the hash locked htlc's
znn-cli htlc.hashLocked -k Bob

```

Htlc id b1fdbd1888a689538587d273b18778bc26ae2dc8387669da8bad29c80ead4cb0 with amount 100,00000000 tZNN  
Can be reclaimed in 00:59:24 by z1qqjnwjjpnue8xmmpanz6csze6tcmtzzdtfsww7  
Can be unlocked by z1qpsjv3wzzuuzdudg7tf6uhvr6sk4ag8me42ua4 with hashlock 3b6e7bfafa188dff9f50b12be1143c751653453a27097cd297f2f283e7e804f3 hashtype 0

```auto
// Bob unlocks the htlc and specifies the correct hash type.
znn-cli htlc.unlock b1fdbd1888a689538587d273b18778bc26ae2dc8387669da8bad29c80ead4cb0 c770b3160b83f6e3a7e45e961da8aaafbdc1557bb2f4dee3ae16d370e10fe16d 1 -k Bob

```

Unlocking htlc id b1fdbd1888a689538587d273b18778bc26ae2dc8387669da8bad29c80ead4cb0 with amount 100,00000000 tZNN  
Done  
Use receiveAll to collect your htlc amount after 2 momentums

```auto
// Bob checks the hash locked htlc's and notices the htlc is not unlocked by the contract
znn-cli htlc.hashLocked -k Bob

```

Htlc id b1fdbd1888a689538587d273b18778bc26ae2dc8387669da8bad29c80ead4cb0 with amount 100,00000000 tZNN  
Can be reclaimed in 00:57:20 by z1qqjnwjjpnue8xmmpanz6csze6tcmtzzdtfsww7  
Can be unlocked by z1qpsjv3wzzuuzdudg7tf6uhvr6sk4ag8me42ua4 with hashlock 3b6e7bfafa188dff9f50b12be1143c751653453a27097cd297f2f283e7e804f3 hashtype 0

---

<div class="post-metadata">

**Author:** ![vilkris](http://forum.zenon.org/user_avatar/forum.zenon.org/vilkris/32/146_2.png) [@vilkris](http://forum.zenon.org/u/vilkris)\
**Post date:** [January 8, 2023, 12:02pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/30 "2023-01-08T12:02:09Z")

</div>

> [@sol](#):
>
> htlcPreimageMinLength (32) and htlcPreimageMaxLength (32)

Where are these variables?

> [@sol](#):
>
> Just want to be sure we’re all on the same page.  
> The protocol and SDKs will not have any preimage length restrictions.  
> Clients like znn-cli and syrius will impose min/max preimage lengths.
> 
> What should the min/max be? I was assuming 32/32 until yesterday.

The HTLC embedded contract stores the `KeyMaxSize` variable in a `uint8` so the max preimage length the protocol supports is 255 bytes. I think that should be enforced by clients. There’s no min preimage length and I’m not sure if there’s much benefit in enforcing one client-side.

---

<div class="post-metadata">

**Author:** ![sol](http://forum.zenon.org/user_avatar/forum.zenon.org/sol/32/1636_2.png) [@sol](http://forum.zenon.org/u/sol)\
**Post date:** [January 8, 2023, 4:23pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/31 "2023-01-08T16:23:18Z")

</div>

> [@CryptoFish](#):
>
> `// Bob checks the hash locked htlc's and notices the htlc is not unlocked by the contract`

Was this with Dart CLI or C# CLI?

> [@vilkris](#):
>
> Where are these variables?

In [go-zenon](https://github.com/Big-Inches-Club-House/go-zenon/blob/0cd866f3dba69064651187d0335998b59babfe24/vm/constants/embedded.go#L84) and [znn\_sdk\_dart](https://github.com/Sol-Sanctum/znn_sdk_dart/blob/spork_htlc/lib/src/embedded/constants.dart#L57).

> [@vilkris](#):
>
> I’m not sure if there’s much benefit in enforcing one client-side.

What’s your ideal default user experience for znn-cli and syrius?

Should znn-cli permit 0/255, default 32?  
And should syrius present similar options?

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 8, 2023, 5:58pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/32 "2023-01-08T17:58:33Z")

</div>

> [@sol](#):
>
> Was this with Dart CLI or C# CLI?

I tried both

Also, I get the following error when using the `verbose` flag.

```auto
Unhandled exception:
Unsupported operation: Please set "hierarchicalLoggingEnabled" to true if you want to change the level on a non-root logger.
#0 Logger.level= (package:logging/src/logger.dart:123)
#1 initZnn (file:///d:/repos/sol-sanctum/znn_cli_dart/init_znn.dart:137)
#2 main (file:///d:/repos/sol-sanctum/znn_cli_dart/cli_handler.dart:17)
#3 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:295)
#4 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192)

```

---

<div class="post-metadata">

**Author:** ![sol](http://forum.zenon.org/user_avatar/forum.zenon.org/sol/32/1636_2.png) [@sol](http://forum.zenon.org/u/sol)\
**Post date:** [January 8, 2023, 7:33pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/33 "2023-01-08T19:33:46Z")

</div>

I’ve pushed [some changes to my repo](https://github.com/zenon-network/znn_cli_dart/commit/62d16f3a5a368d4c3a9489f62bd16bb535a74cf8).  
The `verbose` parameter should work now, though I haven’t tested this thoroughly.

As for the scenario you pointed out, you’re right.[It won’t work](https://github.com/Big-Inches-Club-House/go-zenon/blob/0cd866f3dba69064651187d0335998b59babfe24/vm/embedded/implementation/htlc.go#L256).  
I’ve adjusted the htlc.unlock function accordingly.

znn-cli: default keyMaxSize = 32, with the possibility of calling `createHash` to generate a preimage of [1, 255] bytes.

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 9, 2023, 8:45am UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/34 "2023-01-09T08:45:56Z")

</div>

Great work, but there’s still a problem with the `htlc.create` function.

The `keyMaxSize` is always `32`, even when supplying a `hashlock` based on a pre-image with a larger size.

Thinking about the `keyMaxSize`. I think it’s best to always set it to `255`. The only reason it’s there is because of the [Secret Size Attacks](https://gist.github.com/markblundeberg/7a932c98179de2190049f5823907c016). Settings it to the max allowed pre-image size, prevents the attacks and allows the creation of pre-images between `1` and `255`, using a default of `32`.

The following line in the `createHash` function needs to be an or statement; otherwise the condition is never met.

```auto
if (keyMaxSize > htlcPreimageMaxLength && keyMaxSize < 1) {

```

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 9, 2023, 3:20pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/35 "2023-01-09T15:20:36Z")

</div>

Just updated the following work:

- added Spork support to the C# SDK
- added Spork support to the Java SDK
- removed all incompatibility issues from the C# CLI and matched the impl. of the Dart CLI
- updated the tutorial
- tested all Htlc and Spork commands

---

<div class="post-metadata">

**Author:** ![sol](http://forum.zenon.org/user_avatar/forum.zenon.org/sol/32/1636_2.png) [@sol](http://forum.zenon.org/u/sol)\
**Post date:** [January 9, 2023, 11:04pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/36 "2023-01-09T23:04:21Z")

</div>

> [@CryptoFish](#):
>
> The following line in the `createHash` function needs to be an or statement

That’s embarassing 🫠  
Should be fixed by these commits: [SDK](https://github.com/zenon-network/znn_sdk_dart/commit/bb7126bac65731f0acf4db6a3fa526b53462d3f0) and [CLI](https://github.com/zenon-network/znn_cli_dart/commit/05df33c384b1b7ec29b69b0961c1a8ab0ec2eb1b)  
I _think_ I’m done making changes now.

I think you brought down my devnet with your spork test. 😆  
It’s back up now.

---

<div class="post-metadata">

**Author:** ![CryptoFish](http://forum.zenon.org/user_avatar/forum.zenon.org/cryptofish/32/171_2.png) [@CryptoFish](http://forum.zenon.org/u/CryptoFish)\
**Post date:** [January 10, 2023, 8:28am UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/37 "2023-01-10T08:28:24Z")

</div>

Great, looks good to me 👍

> [@sol](#):
>
> I think you brought down my devnet with your spork test. 😆  
> It’s back up now.

Yeah, I thought that might be the case . That means the activation worked 😄

I will try to be more carefull next time 😅

---

<div class="post-metadata">

**Author:** ![vilkris](http://forum.zenon.org/user_avatar/forum.zenon.org/vilkris/32/146_2.png) [@vilkris](http://forum.zenon.org/u/vilkris)\
**Post date:** [January 10, 2023, 9:09pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/38 "2023-01-10T21:09:29Z")

</div>

> [@sol](#):
>
> What’s your ideal default user experience for znn-cli and syrius?
> 
> Should znn-cli permit 0/255, default 32?  
> And should syrius present similar options?

I guess the CLI can permit anything between 0-255 but for Syrius I’m not sure. For ZTS \<\> ZTS swaps the 32 byte preimage should be fine and not something the user needs to care about but for cross-chain swaps the length might have to be specified by the user.

---

<div class="post-metadata">

**Author:** ![vilkris](http://forum.zenon.org/user_avatar/forum.zenon.org/vilkris/32/146_2.png) [@vilkris](http://forum.zenon.org/u/vilkris)\
**Post date:** [January 11, 2023, 9:39pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/39 "2023-01-11T21:39:00Z")

</div>

> [@sol](#):
>
> In [go-zenon](https://github.com/Big-Inches-Club-House/go-zenon/blob/0cd866f3dba69064651187d0335998b59babfe24/vm/constants/embedded.go#L84) and [znn\_sdk\_dart](https://github.com/Sol-Sanctum/znn_sdk_dart/blob/spork_htlc/lib/src/embedded/constants.dart#L57).

Maybe I missed something but what is the purpose of these variables in go-zenon?

---

<div class="post-metadata">

**Author:** ![sol](http://forum.zenon.org/user_avatar/forum.zenon.org/sol/32/1636_2.png) [@sol](http://forum.zenon.org/u/sol)\
**Post date:** [January 11, 2023, 9:48pm UTC](http://forum.zenon.org/t/dart-sdk-cli-updates/1166/40 "2023-01-11T21:48:13Z")

</div>

I don’t think they’re being used in go-zenon.

[Previous page](http://forum.zenon.org/t/dart-sdk-cli-updates/1166.md?page=1)

[Next page](http://forum.zenon.org/t/dart-sdk-cli-updates/1166.md?page=3)
