# Keccak256 Hash creation

**URL:** <https://forum.algorand.co/t/keccak256-hash-creation/7300>\
**Category:** Uncategorized\
**Created:** [June 30, 2022, 8:03am UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300 "2022-06-30T08:03:52Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [June 30, 2022, 8:03am UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/1 "2022-06-30T08:03:52Z")

</div>

**I want to create a keccak256 for on 4 different arguments that my function will receive:**

`myFunction((token: TealType.uint64, amount: TealType.uint64, payee: TealType.bytes, salt: TealType.bytes)`

**- First I want to generate a hash based on this message** `KECCAK256(BYTES("WithdrawSigned(token,payee,amount,salt)")),`

**PYTEAL CODE:**

```auto
       withdrawSignedMethod.store(Bytes("WithdrawSigned(token,payee,amount,salt)")),
            withdraw_signed_method.store(Keccak256(withdrawSignedMethod.load())),

```

`withdraw_signed_method.load()` **is a hash generated from** `Bytes("WithdrawSigned(token,payee,amount,salt)")`

- NOW, I want to create hash from the withdraw\_signed\_method hash and all other function parameters

**PYTEAL CODE:**

```auto
            initialS.store(withdraw_signed_method.load(), token, payee, amount, salt),
            signedMessage.store(Keccak256(initialS.load())),

```

But it doesn’t work and gives this error message:

```auto
  File "full-bridge.py", line 100, in withdraw_token
    initialS.store(withdraw_signed_method.load(), token, payee, amount, salt),
TypeError: store() takes 2 positional arguments but 6 were given

```

Could you please suggest a way using which I would be able to generate hash for all of my input parameters, I’m stuck on this problem for past 1 week?

---

<div class="post-metadata">

**Author:** ![Ben](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.algorand.co/ben/32/1979_2.png) [@Ben](https://forum.algorand.co/u/Ben)\
**Post date:** [June 30, 2022, 10:53am UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/2 "2022-06-30T10:53:37Z")

</div>

Hi salman,

The store method takes a single argument, the value you wish to store in scratch space. Are you trying to hash the concatenated values of all the arguments? If so you can convert the arguments you passed there (if relevant) to bytes with `Concat` _then_ hash the resulting bytes with Keccack256.

Ben

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [June 30, 2022, 3:16pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/3 "2022-06-30T15:16:16Z")

</div>

Can you share a possible syntax for that solution you are suggesting?

I tried the below mentioned solution but I feel it would concat the keys but I want to concat the values of the particular parameters.

```auto
            initialS.store(Concat(withdraw_signed_method.load(), Itob(token), payee, Itob(amount), salt)),
            signedMessage.store(Keccak256(initialS.load())),

```

Such as the input parameters:

```auto
token = 183838
payee = "X9JDHAS03...." 
amount = 100
salt = "SOME-HASH"

```

Now how can I concat the values of these input parameters and then pass it to Keccak256?

---

<div class="post-metadata">

**Author:** ![Ben](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.algorand.co/ben/32/1979_2.png) [@Ben](https://forum.algorand.co/u/Ben)\
**Post date:** [July 1, 2022, 12:10pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/4 "2022-07-01T12:10:29Z")

</div>

That looks approximately correct based on the description of what you want to do.

You can try logging the value with the `Log` method and inspect the result or call dryrun or use tealdbg to get a better sense of the output

> **[Debugging smart contracts - Algorand Developer Portal](https://developer.algorand.org/docs/get-details/dapps/smart-contracts/debugging/)**
>
> Algorand Developer Docs, SDKs, REST APIs, CLI tools, ecosystem projects, metrics dashboard and sample code, how-tos, and news from the Algorand developer community

Ben

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 1, 2022, 12:31pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/5 "2022-07-01T12:31:22Z")

</div>

Also how can I apply EcdsaRecover(App.globalGet(WithdrawSigned), signature),

EcdsaRecover(r, s) isn’t working with pyteal?

[https://pyteal.readthedocs.io/en/stable/crypto.html#:~:text=EcdsaRecover](https://pyteal.readthedocs.io/en/stable/crypto.html#:~:text=EcdsaRecover)

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 1, 2022, 12:43pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/6 "2022-07-01T12:43:04Z")

</div>

How can I install tealdbg?

---

<div class="post-metadata">

**Author:** ![Ben](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.algorand.co/ben/32/1979_2.png) [@Ben](https://forum.algorand.co/u/Ben)\
**Post date:** [July 1, 2022, 12:45pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/7 "2022-07-01T12:45:01Z")

</div>

This is described in the docs linked above

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 1, 2022, 2:10pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/8 "2022-07-01T14:10:44Z")

</div>

Also, respond to my question about ecdsaRecover(r,s) its not working for me.

---

<div class="post-metadata">

**Author:** ![fabrice](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fabrice](https://forum.algorand.co/u/fabrice)\
**Post date:** [July 1, 2022, 2:25pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/9 "2022-07-01T14:25:05Z")

</div>

> [@salman.haider](#):
>
> EcdsaRecover(r, s) isn’t working with pyteal?

You need to give four arguments to `EcdsaRecover`.

`EcdsaRecover(EcdsaCurve.Secp256k1, d, id, r, s)` matches Solidity `ecdsa_recover(hash=d, v=id, r, s)` in [Units and Globally Available Variables — Solidity 0.8.15 documentation](https://docs.soliditylang.org/en/v0.8.15/units-and-global-variables.html#mathematical-and-cryptographic-functions)

Note also that you need Python3 3.10 and an up to date version of Pyteal to use `EcdsaRecover`.  
Default versions of Python on Ubuntu 20.04 and macOS 12 are not sufficient (nor is the default version of Python for Homebrew).

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 1, 2022, 2:42pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/10 "2022-07-01T14:42:03Z")

</div>

I tried to execute tealdbg

./sandbox tealdbg debug bridge.teal

However, it cannot locate the bridge.teal file which I have placed in sandbox folder

---

<div class="post-metadata">

**Author:** ![fabrice](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fabrice](https://forum.algorand.co/u/fabrice)\
**Post date:** [July 1, 2022, 2:44pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/11 "2022-07-01T14:44:57Z")

</div>

You need to copy the `bridge.teal` file into sandbox using `./sandbox copyTo bridge.teal`  
Sandbox uses docker containers and so files need to be copied there.

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 1, 2022, 2:47pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/12 "2022-07-01T14:47:27Z")

</div>

> [@fabrice](#):
>
> `EcdsaRecover(EcdsaCurve.Secp256k1, d, id, r, s)`

In my case I’m only aware of the ecdsa.recover(message, signature) function that solidity have and I just wanted to replicate that. However, even if I have to use this function with four arguments, then what would be the first two arguments? Since I know, (r,s) → (message, signarture)

---

<div class="post-metadata">

**Author:** ![fabrice](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fabrice](https://forum.algorand.co/u/fabrice)\
**Post date:** [July 1, 2022, 11:00pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/13 "2022-07-01T23:00:34Z")

</div>

I’ve written an example there:

> **[algorand-devrel-demo-avm1.1/demos/eth\_ecdsa\_recover at eth\_ecdsa\_recover ·...](https://github.com/fabrice102/algorand-devrel-demo-avm1.1/tree/eth_ecdsa_recover/demos/eth_ecdsa_recover)**
>
> eth\_ecdsa\_recover/demos/eth\_ecdsa\_recover

See the [README](https://github.com/fabrice102/algorand-devrel-demo-avm1.1/tree/eth_ecdsa_recover) on how to use the code.

Importantly notice that you need to use Python 3.10 which is not the default on many OSes and which require manual installation.

Disclaimer: Do not use without full audit. The code was not audited.

Now for the details, `ecdsa.recovery` is a function of OpenZeppelin not a built-in solidity function:

> <https://github.com/OpenZeppelin/openzeppelin-contracts/blob/afb20119b33072da041c97ea717d3ce4417b5e01/contracts/utils/cryptography/ECDSA.sol#L57>

You need to simulate it on Algorand as Algorand essentially only support the pure `ecdsa_recover` from Solidity (with the default recovery id 0/1/2/3 instead of 27/28/29/30).

In particular, `r,s` is _not_ `(message,signature)`.

Instead:

- `hash=d` is the hash of the message
- `signature` is a concatenation of `r,s,v=id`.

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 5, 2022, 10:20am UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/15 "2022-07-05T10:20:33Z")

</div>

Can you please provide me the following that would work with algorand:

1. Test message **m**
2. **Signature** of that message
3. **Signer** ethereum

The one provided in demo.py doesn’t work, I was able to run the example however, the signer is not recovered using the m, signature provided in demo.py file (b"openzeppeline", signature)

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 5, 2022, 11:45am UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/16 "2022-07-05T11:45:57Z")

</div>

Also, please suggest me the following:

- How to pass a different smartContract/wallet address to a my withdrawToken function so When I call the withdraw function it should tx tokens to a specified or given wallet address/smartcontract.  
I tried sending the address as an argument to the contract but it doesn’t read it correctly. Could you please suggest a good way to do it?

- Second, please provide the chain ID of algorand’s testnet and mainnet.

---

<div class="post-metadata">

**Author:** ![fabrice](https://avatars.discourse-cdn.com/v4/letter/f/5fc32e/32.png) [@fabrice](https://forum.algorand.co/u/fabrice)\
**Post date:** [July 5, 2022, 3:14pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/17 "2022-07-05T15:14:28Z")

</div>

> [@salman.haider](#):
>
> Can you please provide me the following that would work with algorand:
> 
> 1. Test message **m**
> 2. **Signature** of that message
> 3. **Signer** ethereum
> 
> The one provided in demo.py doesn’t work, I was able to run the example however, the signer is not recovered using the m, signature provided in demo.py file (b"openzeppeline", signature)

[https://github.com/fabrice102/algorand-devrel-demo-avm1.1/blob/365acf147777337003ff535ec279a4255277cddb/demos/eth\_ecdsa\_recover/demo.py#L114](https://github.com/fabrice102/algorand-devrel-demo-avm1.1/blob/365acf147777337003ff535ec279a4255277cddb/demos/eth_ecdsa_recover/demo.py#L114) is an example of message / signature / signer that works.  
When you run the code, it works.

Note that the sender is an Ethereum address, not an Algorand address because Algorand addresses

> [@salman.haider](#):
>
> How to pass a different smartContract/wallet address to a my withdrawToken function so When I call the withdraw function it should tx tokens to a specified or given wallet address/smartcontract.  
> I tried sending the address as an argument to the contract but it doesn’t read it correctly. Could you please suggest a good way to do it?

What error do you see?  
Be careful that in smart contracts, addresses must be written as 32-byte string, not as a base32 string with checksum (which is how you see addresses everywhere). See [Overview - Algorand Developer Portal](https://developer.algorand.org/docs/get-details/accounts/#keys-and-addresses) for details.

Also, if you plan to use inner transactions, the receiving address must be in the account list. See [Overview - Algorand Developer Portal](https://developer.algorand.org/docs/get-details/dapps/smart-contracts/apps/#inner-transactions).

> [@salman.haider](#):
>
> - Second, please provide the chain ID of algorand’s testnet and mainnet.

There is no official chainID as Algorand is not an EVM chain.  
On WalletConnect, we use:

> <https://github.com/algorandfoundation/ARCs/blob/1c3e544cae5a186d4e59f5d43cdfbb346af68da3/ARCs/arc-0025.md#chain-ids>

(but there is no guarantee they’re unique unfortunately)

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 6, 2022, 9:10am UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/18 "2022-07-06T09:10:50Z")

</div>

> [@fabrice](#):
>
> [algorand-devrel-demo-avm1.1/demo.py at 365acf147777337003ff535ec279a4255277cddb · fabrice102/algorand-devrel-demo-avm1.1 · GitHub](https://github.com/fabrice102/algorand-devrel-demo-avm1.1/blob/365acf147777337003ff535ec279a4255277cddb/demos/eth_ecdsa_recover/demo.py#L114) is an example of message / signature / signer that works.  
> When you run the code, it works.
> 
> Note that the sender is an Ethereum address, not an Algorand address because Algorand addresses

The code that you have written (demo.py) is in python, however, I’m using the Javascript sdk for interacting with the ECDSA contract. Here is how I’m interacting with the contract for signing the signature. Moreover, once the message is received in the contract, we apply keccak256 on the message to convert it to hash.

Let me know if you think there are few changes required?

```auto
let index = 98492040;
let token_address = 81317600;
account = [];
foreignApp = [];
foreignAssets = [];
foreignAssets.push(token_address);

// withdraw_token(_token,_amount, _receiver, salt, signature),
let action = "withdraw";
let amount = 100;
let message = "OpenZeppelin";
let signature = "5d99b6f7f6d1f73d1a26497f2b1c89b24c0993913f86e9a2d02cd69887d9c94f3c880358579d811b21dd1b7fd9bb01c1d81d10e69f0384e675c32b39643be8921b";
let signByte = ByteBuffer.fromHex(signature, undefined, undefined);
let signBuffer = signByte.buffer;

let appArgs = [];
appArgs.push(new Uint8Array(Buffer.from(action)))
appArgs.push(algosdk.encodeUint64(token_address));
appArgs.push(algosdk.encodeUint64(amount));
appArgs.push(new Uint8Array(Buffer.from(message)));
appArgs.push(new Uint8Array(Buffer.from(signBuffer)));

```

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 6, 2022, 12:11pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/19 "2022-07-06T12:11:38Z")

</div>

How can we define struct and enum - in Algorand?

such as :

```auto
    enum TokenData {
      tokenVal, 
      tokenBalance,
    }
    struct testInfo {
        address sender;
        TokenData data; // defining enum into another structure
    }

    struct testConfig {
        address testToken;
        testInfo info; // defining struct into another struct
    }

```

// defining mapping based on the structs

```auto
    mapping(address => testInfo) public testInfo;
    mapping(address => testConfig) public testConfig;

```

How can we achieve the above mentioned in Algorand? I don’t see anything in Algorand?

---

<div class="post-metadata">

**Author:** ![salman.haider](https://avatars.discourse-cdn.com/v4/letter/s/f08c70/32.png) [@salman.haider](https://forum.algorand.co/u/salman.haider)\
**Post date:** [July 6, 2022, 12:20pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/20 "2022-07-06T12:20:37Z")

</div>

Also how can we replicate the Shift operators of Solidity in Algorand?

1 . \>\>  
2 . \<\<  
3 . &

Please provide relevant syntax in algorand

 ![image](https://us1.discourse-cdn.com/flex016/uploads/algorand/original/2X/0/080030a401ed0bd08021b05789644476fd96ea1f.png)

---

<div class="post-metadata">

**Author:** ![Ben](https://sea2.discourse-cdn.com/flex016/user_avatar/forum.algorand.co/ben/32/1979_2.png) [@Ben](https://forum.algorand.co/u/Ben)\
**Post date:** [July 6, 2022, 2:01pm UTC](https://forum.algorand.co/t/keccak256-hash-creation/7300/21 "2022-07-06T14:01:45Z")

</div>

The bitwise operations in pyteal are detailed here: [PyTeal Package — PyTeal documentation](https://pyteal.readthedocs.io/en/stable/api.html#pyteal.BitwiseAnd)

for structs, there is nothing built in that will help with that yet

[Next page](https://forum.algorand.co/t/keccak256-hash-creation/7300.md?page=2)
