question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

I need to add a field when I sign it.

See original GitHub issue

wallet.sign({ to: address, value: ethers.utils.parseEther('1'), type: 0, })

How to put type together with serialize

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
axu6705commented, Nov 22, 2019

Thanks! I have solved.

0reactions
axu6705commented, Nov 22, 2019

If you are creating a new blockchain, you should look at the transactions package and define how your field is encoded for the underlying RLP.

Ethereum

const transactionFields = [
  {name: 'nonce', maxLength: 32},
  {name: 'gasPrice', maxLength: 32},
  {name: 'gasLimit', maxLength: 32},
  {name: 'to', length: 20},
  {name: 'value', maxLength: 32},
  {name: 'data'},
  {name: 'type', maxLength: 8},
];


const utils = ethers.utils;
function serialize(transaction, signature) {
  // utils.checkProperties(transaction, allowedTransactionKeys);
  let raw = [];
  transactionFields.forEach(function(fieldInfo, i) {
    let value = transaction[fieldInfo.name] || [];
    value = utils.arrayify(utils.hexlify(value));
    // Fixed-width field
    if (
      fieldInfo.length &&
      value.length !== fieldInfo.length &&
      value.length > 0
    ) {
      utils.errors.throwError(
        'invalid length for ' + fieldInfo.name,
        utils.errors.INVALID_ARGUMENT,
        {arg: 'transaction' + fieldInfo.name, value: value},
      );
    }
    // Variable-width (with a maximum)
    if (fieldInfo.maxLength) {
      value = utils.stripZeros(value);
      if (value.length > fieldInfo.maxLength) {
        utils.errors.throwError(
          'invalid length for ' + fieldInfo.name,
          utils.errors.INVALID_ARGUMENT,
          {arg: 'transaction' + fieldInfo.name, value: value},
        );
      }
    }
    raw.push(utils.hexlify(value));
  });

  if (transaction.chainId != null && transaction.chainId !== 0) {
    raw.push(utils.hexlify(transaction.chainId));
    raw.push('0x');
    raw.push('0x');
  }

  const unsignedTransaction = utils.RLP.encode(raw);
  // Requesting an unsigned transation
  if (!signature) {
    return unsignedTransaction;
  }
  // The splitSignature will ensure the transaction has a recoveryParam in the
  // case that the signTransaction function only adds a v.
  const sig = utils.splitSignature(signature);
  // We pushed a chainId and null r, s on for hashing only; remove those
  let v = 27 + sig.recoveryParam;
  if (raw.length === 10) {
    raw.pop();
    raw.pop();
    raw.pop();
    v += transaction.chainId * 2 + 8;
  }
  raw.push(utils.hexlify(v));
  raw.push(utils.stripZeros(utils.arrayify(sig.r)));
  raw.push(utils.stripZeros(utils.arrayify(sig.s)));

  return utils.RLP.encode(raw);
}

export class Wallet extends ethers.Wallet {
  constructor(privateKey, provider) {
    super(privateKey, provider);
  }

  async sign(transaction) {
    const tx = await utils.resolveProperties(transaction);
    const rawTx = serialize(tx);
    const signature = this.signingKey.signDigest(utils.keccak256(rawTx));
    return serialize(tx, signature);
  }
}
const signedTransaction = await Wallet.sign({ to: address, value: ethers.utils.parseEther('1'), type: 0});
provider.sendTransaction(signedTransaction);
// Error: invalid raw transaction

Without Type

Error: rlp: too few elements for types.txdata

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add form fields to documents - Adobe Support
Learn how to add a Signature, Date, Signer Name, Initials, e-mail, Title, and Company Name field to a document, and how to collect...
Read more >
Add Fields to Documents - DocuSign Support
The Add Fields view has the following basic areas: Recipients. Lists all of the recipients who will receive your documents to sign.
Read more >
DocuSign: Adding Fields to a Document - UCSD Blink
Learn how to add fields to a document in DocuSign. Fields indicate to your recipients where they should sign, initial or add information...
Read more >
Insert Fields Document on any device - signNow
Click Edit Signer and add an email. You can include as many people as needed. Select Signature Field , put it anywhere in...
Read more >
Add a field to a form or report - Microsoft Support
Open the form or report in Design view by right-clicking it in the Navigation Pane and then clicking Design View. · Right-click the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found