@ethersproject/bytes#joinSignature TypeScript Examples

The following examples show how to use @ethersproject/bytes#joinSignature. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Signer.ts    From bodhi.js with Apache License 2.0 6 votes vote down vote up
async _signMessage(evmAddress: string, message: Bytes | string): Promise<string> {
    if (!evmAddress) {
      return logger.throwError('No binding evm address');
    }
    const messagePrefix = '\x19Ethereum Signed Message:\n';
    if (typeof message === 'string') {
      message = toUtf8Bytes(message);
    }
    const msg = u8aToHex(concat([toUtf8Bytes(messagePrefix), toUtf8Bytes(String(message.length)), message]));

    if (!this.signingKey.signRaw) {
      return logger.throwError('Need to implement signRaw method');
    }

    const result = await this.signingKey.signRaw({
      address: evmAddress,
      data: msg,
      type: 'bytes'
    });

    return joinSignature(result.signature);
  }
Example #2
Source File: createClaimSignature.ts    From bodhi.js with Apache License 2.0 6 votes vote down vote up
createClaimSignature = (privateKey: string, tx: ClaimPayload): string => {
  const payload = createClaimPayload(tx);

  const wallet = new Wallet(privateKey);

  return joinSignature(
    wallet._signingKey().signDigest(
      _TypedDataEncoder.hash(
        payload.domain,
        {
          Transaction: payload.types.Transaction
        },
        payload.message
      )
    )
  );
}
Example #3
Source File: parseTransaction.ts    From bodhi.js with Apache License 2.0 6 votes vote down vote up
function _parseEip712Signature(
  tx: AcalaEvmTX,
  fields: Array<string>,
  serialize: (tx: UnsignedAcalaEvmTX) => string
): void {
  try {
    const recid = handleNumber(fields[0]).toNumber();
    if (recid !== 0 && recid !== 1) {
      throw new Error('bad recid');
    }
    tx.v = recid;
  } catch (error) {
    logger.throwArgumentError('invalid v for transaction type: 1', 'v', fields[0]);
  }

  tx.r = hexZeroPad(fields[1], 32);
  tx.s = hexZeroPad(fields[2], 32);

  tx.from = verifyTransaction(
    {
      chainId: tx.chainId,
      salt: tx.salt,
      nonce: tx.nonce,
      gasLimit: tx.gasLimit,
      storageLimit: tx.storageLimit,
      to: tx.to,
      value: tx.value,
      data: tx.data,
      validUntil: tx.validUntil,
      tip: tx.tip,
      accessList: tx.accessList
    },
    joinSignature({ r: tx.r, s: tx.s, v: tx.v })
  );
}
Example #4
Source File: signTransaction.ts    From bodhi.js with Apache License 2.0 6 votes vote down vote up
signTransaction = (privateKey: string, tx: AcalaEvmTXPayload): string => {
  const payload = createTransactionPayload(tx);

  const wallet = new Wallet(privateKey);

  return joinSignature(
    wallet._signingKey().signDigest(
      _TypedDataEncoder.hash(
        payload.domain,
        {
          AccessList: payload.types.AccessList,
          Transaction: payload.types.Transaction
        },
        payload.message
      )
    )
  );
}
Example #5
Source File: Signer.ts    From evm-provider.js with Apache License 2.0 6 votes vote down vote up
async _signMessage(
    evmAddress: string,
    message: Bytes | string
  ): Promise<string> {
    if (!evmAddress) {
      return logger.throwError('No binding evm address');
    }
    const messagePrefix = '\x19Ethereum Signed Message:\n';
    if (typeof message === 'string') {
      message = toUtf8Bytes(message);
    }
    const msg = u8aToHex(
      concat([
        toUtf8Bytes(messagePrefix),
        toUtf8Bytes(String(message.length)),
        message
      ])
    );

    if (!this.signingKey.signRaw) {
      return logger.throwError('Need to implement signRaw method');
    }

    const result = await this.signingKey.signRaw({
      address: evmAddress,
      data: msg,
      type: 'bytes'
    });

    return joinSignature(result.signature);
  }
Example #6
Source File: base-provider.ts    From bodhi.js with Apache License 2.0 5 votes vote down vote up
prepareTransaction = async (
    rawTx: string
  ): Promise<{
    extrinsic: SubmittableExtrinsic<'promise'>;
    transaction: AcalaEvmTX;
  }> => {
    await this.getNetwork();

    const signatureType = checkSignatureType(rawTx);
    const ethTx = parseTransaction(rawTx);

    if (!ethTx.from) {
      return logger.throwError('missing from address', Logger.errors.INVALID_ARGUMENT, ethTx);
    }

    const { storageLimit, validUntil, gasLimit, tip, accessList } = this._getSubstrateGasParams(ethTx);

    // check excuted error
    const callRequest: CallRequest = {
      from: ethTx.from,
      // @TODO Support create
      to: ethTx.to,
      gasLimit: gasLimit,
      storageLimit: storageLimit,
      value: ethTx.value.toString(),
      data: ethTx.data,
      accessList: ethTx.accessList
    };

    await (this.api.rpc as any).evm.call(callRequest);

    const extrinsic = this.api.tx.evm.ethCall(
      ethTx.to ? { Call: ethTx.to } : { Create: null },
      ethTx.data,
      ethTx.value.toString(),
      gasLimit,
      storageLimit,
      // @ts-ignore @TODO fix type
      accessList || [],
      validUntil
    );

    const subAddr = await this.getSubstrateAddress(ethTx.from);

    const sig = joinSignature({ r: ethTx.r!, s: ethTx.s, v: ethTx.v });

    extrinsic.addSignature(subAddr, { [signatureType]: sig } as any, {
      blockHash: '0x', // ignored
      era: '0x00', // mortal
      genesisHash: '0x', // ignored
      method: 'Bytes', // don't know waht is this
      specVersion: 0, // ignored
      transactionVersion: 0, // ignored
      nonce: ethTx.nonce,
      tip
    });

    logger.debug(
      {
        evmAddr: ethTx.from,
        address: subAddr,
        hash: extrinsic.hash.toHex()
      },
      'sending raw transaction'
    );

    return {
      extrinsic,
      transaction: ethTx
    };
  };
Example #7
Source File: transaction.test.ts    From bodhi.js with Apache License 2.0 5 votes vote down vote up
//[chainId, nonce, gasLimit, to, value, data, eip712sig]
describe('transaction', () => {
  it('serializeTransaction signed', async () => {
    const ethersHash = transactionHash(data);
    const ethersSig = signTransaction(privateKey, data);

    const tx = serializeTransaction(data, ethersSig);

    const parsedTx = parseTransaction(tx);

    expect(data).deep.equal({
      chainId: parsedTx.chainId,
      nonce: parsedTx.nonce,
      gasLimit: parsedTx.gasLimit.toNumber(),
      to: parsedTx.to || undefined,
      value: parsedTx.value.toNumber(),
      salt: (parsedTx as any).salt,
      data: parsedTx.data,
      type: parsedTx.type,
      accessList: parsedTx.accessList
    });

    expect(parsedTx.hash).equal(ethersHash);

    const parsedSig = joinSignature({ r: parsedTx.r!, s: parsedTx.s, v: parsedTx.v });

    expect(parsedSig).equal(ethersSig);
    expect(parsedTx.from).equal(address);
  });

  it('serializeTransaction unsigned', async () => {
    const tx = serializeTransaction(data);

    const parsedTx = parseTransaction(tx);

    expect(data).deep.equal({
      chainId: parsedTx.chainId,
      nonce: parsedTx.nonce,
      gasLimit: parsedTx.gasLimit.toNumber(),
      to: parsedTx.to || undefined,
      value: parsedTx.value.toNumber(),
      salt: (parsedTx as any).salt,
      data: parsedTx.data,
      type: parsedTx.type,
      accessList: parsedTx.accessList
    });
  });

  it('parseTransaction legacyRawTx', () => {
    const legacyRawTx =
      '0xf903fa80870186a00000271083030dcf8080b903a360806040526040518060400160405280600c81526020017f48656c6c6f20576f726c642100000000000000000000000000000000000000008152506000908051906020019061004f929190610062565b5034801561005c57600080fd5b50610166565b82805461006e90610134565b90600052602060002090601f01602090048101928261009057600085556100d7565b82601f106100a957805160ff19168380011785556100d7565b828001600101855582156100d7579182015b828111156100d65782518255916020019190600101906100bb565b5b5090506100e491906100e8565b5090565b5b808211156101015760008160009055506001016100e9565b5090565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061014c57607f821691505b602082108114156101605761015f610105565b5b50919050565b61022e806101756000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c605f76c14610030575b600080fd5b61003861004e565b6040516100459190610175565b60405180910390f35b6000805461005b906101c6565b80601f0160208091040260200160405190810160405280929190818152602001828054610087906101c6565b80156100d45780601f106100a9576101008083540402835291602001916100d4565b820191906000526020600020905b8154815290600101906020018083116100b757829003601f168201915b505050505081565b600081519050919050565b600082825260208201905092915050565b60005b838110156101165780820151818401526020810190506100fb565b83811115610125576000848401525b50505050565b6000601f19601f8301169050919050565b6000610147826100dc565b61015181856100e7565b93506101618185602086016100f8565b61016a8161012b565b840191505092915050565b6000602082019050818103600083015261018f818461013c565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806101de57607f821691505b602082108114156101f2576101f1610197565b5b5091905056fea26469706673582212204d363ed34111d1be492d4fd086e9f2df62b3c625e89ade31f30e63201ed1e24f64736f6c634300080900338204caa062ae978500ad43be5c137afeefe9326abd00eb40b84da69dcf481fbc7671a112a042e6b77747d4cbed5dde145ad15d96a8c762ac3d872ce965d8dba02759dcf976';

    const result = parseTransaction(legacyRawTx);
    console.log(result);
    expect(result.type).equal(null);
    expect(result.nonce).equal(0);
    expect(result.data).equal(
      '0x60806040526040518060400160405280600c81526020017f48656c6c6f20576f726c642100000000000000000000000000000000000000008152506000908051906020019061004f929190610062565b5034801561005c57600080fd5b50610166565b82805461006e90610134565b90600052602060002090601f01602090048101928261009057600085556100d7565b82601f106100a957805160ff19168380011785556100d7565b828001600101855582156100d7579182015b828111156100d65782518255916020019190600101906100bb565b5b5090506100e491906100e8565b5090565b5b808211156101015760008160009055506001016100e9565b5090565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b6000600282049050600182168061014c57607f821691505b602082108114156101605761015f610105565b5b50919050565b61022e806101756000396000f3fe608060405234801561001057600080fd5b506004361061002b5760003560e01c8063c605f76c14610030575b600080fd5b61003861004e565b6040516100459190610175565b60405180910390f35b6000805461005b906101c6565b80601f0160208091040260200160405190810160405280929190818152602001828054610087906101c6565b80156100d45780601f106100a9576101008083540402835291602001916100d4565b820191906000526020600020905b8154815290600101906020018083116100b757829003601f168201915b505050505081565b600081519050919050565b600082825260208201905092915050565b60005b838110156101165780820151818401526020810190506100fb565b83811115610125576000848401525b50505050565b6000601f19601f8301169050919050565b6000610147826100dc565b61015181856100e7565b93506101618185602086016100f8565b61016a8161012b565b840191505092915050565b6000602082019050818103600083015261018f818461013c565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052602260045260246000fd5b600060028204905060018216806101de57607f821691505b602082108114156101f2576101f1610197565b5b5091905056fea26469706673582212204d363ed34111d1be492d4fd086e9f2df62b3c625e89ade31f30e63201ed1e24f64736f6c63430008090033'
    );
    expect(result.from).equal('0x75E480dB528101a381Ce68544611C169Ad7EB342');
    expect(result.hash).equal('0x7ce5cf166bb1cb68b494db1e50cffbddc2c32c4014eaae106ebdb9a41946cc17');
  });
});