libjs icon indicating copy to clipboard operation
libjs copied to clipboard

Need help to interact with a contract

Open GuillaumeFerreira opened this issue 1 year ago • 4 comments

Hello, I've seen a lot of documentation, I think it's very good but unfortunately I still can't sign a transaction. I've come here in the hope that someone will be able to help me. I'll try to describe my problem as fully as possible.

archethic
  .connect()
  .then(async () => {

    const secretKey = Crypto.deriveAddress(userSeed, 0)
    const cipher = Crypto.aesEncrypt(secretKey, seed);


    const authorizedKeys = [
      {
        publicKey: seed,
        encryptedSecretKey: Crypto.ecEncrypt(secretKey, seed),
      },
    ];
    const originPrivateKey = Utils.originPrivateKey;

    const tx = archethic.transaction
      .new()
      .setType('transfer')
      .addUCOTransfer(poolAddressesUcoAeETH, Utils.toBigInt(1))
      .addOwnership(cipher, authorizedKeys)
      .build(seed,0)
      .originSign(originPrivateKey);

    tx.on('confirmation', (nbConf, maxConf) => console.log(nbConf, maxConf))
      .on('error', (_, err) => console.log(err))
      .on('timeout', (_) => console.log('timeout'))
      .send();
  })
  .catch((a) => {
    console.error(a);
  });

my userseed looks like this : "pirate bateau chimie .... " my seed looks like this : "0000FD163D0CAD7CF62A6239A037EDABC27AD8467AD2898F899A88C7F9B52D771792"

I know that this code is not at all correct, and that I don't understand all these different keys, if anyone can help me.

GuillaumeFerreira avatar Jun 12 '24 06:06 GuillaumeFerreira

Hello, I see a few mixups here and there! Fortunately @Rudy-Perrin made a recent pull request to simplify the ownerships. You can see the code to do what you try to do (I think) here: https://github.com/archethic-foundation/libjs/pull/196/files#diff-cc63f119b1879db255abe3dbb493232f19c026790a1514a05a135ef618fdaa23R718

The function will ship in next version of libjs which is imminent I believe. Your best course of action is either:

  • wait for new version and use the new function
  • copy paste the function and use it

bchamagne avatar Jun 12 '24 07:06 bchamagne

Hello. The ownership's mechanism is a way to delegate secrets to set of authorized public keys. The secret is encrypted with a AES key, which is encrypted for a set of public keys. Here some remark about this on your code

const authorizedKeys = [
      {
        publicKey: seed, // It should be the authorized public key and not the seed
        encryptedSecretKey: Crypto.ecEncrypt(secretKey, seed), // The second parameter of the function should be the authorized public key
      },
    ];

You can also note that: const secretKey = Crypto.deriveAddress(userSeed, 0) this is not really secure, because the key can be disclosed if the address is known. You should rely on randomSecret generation.

But I don't see the contract's interaction you mentioned in your title ?

samuelmanzanera avatar Jun 12 '24 07:06 samuelmanzanera

Thank you both for your replies.

@bchamagne I'll probably take the first option and wait, because I think I'm having trouble understanding the path from the secret key to the signing of the transaction.

@samuelmanzanera I'm trying to interact with the dex , with the aeETH/UCO pool , to create a swap

// UCO --> aeETH
const poolAddressesUcoAeETH =
  '000090C5AFCC97C2357E964E3DDF5BE9948477F7C1DE2C633CDFC95B202970AEA036';

to interact with the dex contract, is this the right way to do it? to send UCOs and receive aeETHs?

archethic.transaction
      .new()
      .setType('transfer')
      .addUCOTransfer(poolAddressesUcoAeETH, Utils.toBigInt(1))
      .addOwnership(cipher, authorizedKeys)
      .build(seed,0)
      .originSign(originPrivateKey)

GuillaumeFerreira avatar Jun 12 '24 08:06 GuillaumeFerreira

I'd suggest to test on the testnet first ;) To interact with the dex you don't need ownership, but you need to transfer funds AND call the named action. If you don't call the contract, you'll just transfer funds and loose them.

archethic.transaction
      .new()
      .setType('transfer')
      .addUCOTransfer(poolAddressesUcoAeETH, Utils.toBigInt(1))
      .addRecipient(poolAddressesUcoAeETH "swap", [minAmountToReceive]);
      .build(seed,0)
      .originSign(originPrivateKey)

bchamagne avatar Jun 14 '24 12:06 bchamagne