ethereumjs-util#toBuffer TypeScript Examples

The following examples show how to use ethereumjs-util#toBuffer. 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: util.ts    From remix-project with MIT License 7 votes vote down vote up
/**
  * sha3 the given @arg value (left pad to 32 bytes)
  *
  * @param {String} value - value to sha3
  * @return {Object} - return sha3ied value
  */
// eslint-disable-next-line camelcase
export function sha3_256 (value) {
  value = toBuffer(addHexPrefix(value))
  const retInBuffer: Buffer = keccak(setLengthLeft(value, 32))
  return bufferToHex(retInBuffer)
}
Example #2
Source File: web3.ts    From core with MIT License 6 votes vote down vote up
async signTypedData(dataToSign: any) {
    // console.log('private key', this.privateKey);
    let privateKeyBuffer = toBuffer(this.privateKey);
    // console.log('privateKeyBuffer', privateKeyBuffer);
    // console.log('dataToSign', dataToSign);
    let sig = web3SignTypeData({
      privateKey: privateKeyBuffer,
      data: dataToSign,
      version: SignTypedDataVersion.V3,
    });
    // console.log('sig', sig);
    return sig;
    // return (this.client.eth.accounts.sign(dataToSign, this.privateKey)).signature;
  }
Example #3
Source File: Mapping.ts    From remix-project with MIT License 6 votes vote down vote up
function getMappingLocation (key, position) {
  // mapping storage location decribed at http://solidity.readthedocs.io/en/develop/miscellaneous.html#layout-of-state-variables-in-storage
  // > the value corresponding to a mapping key k is located at keccak256(k . p) where . is concatenation.

  // key should be a hex string, and position an int
  const mappingK = toBuffer(addHexPrefix(key))
  let mappingP = toBuffer(addHexPrefix(position))
  mappingP = setLengthLeft(mappingP, 32)
  const mappingKeyBuf = concatTypedArrays(mappingK, mappingP)
  const mappingStorageLocation: Buffer = keccak(mappingKeyBuf)
  const mappingStorageLocationinBn: BN = new BN(mappingStorageLocation, 16)
  return mappingStorageLocationinBn
}
Example #4
Source File: txListener.ts    From remix-project with MIT License 6 votes vote down vote up
_decodeInputParams (data, abi) {
    data = toBuffer(addHexPrefix(data))
    if (!data.length) data = new Uint8Array(32 * abi.inputs.length) // ensuring the data is at least filled by 0 cause `AbiCoder` throws if there's not engouh data

    const inputTypes = []
    for (let i = 0; i < abi.inputs.length; i++) {
      const type = abi.inputs[i].type
      inputTypes.push(type.indexOf('tuple') === 0 ? makeFullTypeDefinition(abi.inputs[i]) : type)
    }
    const abiCoder = new ethers.utils.AbiCoder()
    const decoded = abiCoder.decode(inputTypes, data)
    const ret = {}
    for (const k in abi.inputs) {
      ret[abi.inputs[k].type + ' ' + abi.inputs[k].name] = decoded[k]
    }
    return ret
  }
Example #5
Source File: provider.ts    From cloud-cryptographic-wallet with MIT License 6 votes vote down vote up
public async signMessage(msgParams: MsgParams): Promise<string> {
    const from = msgParams.from;
    const signer = this.resolveSigner(from);

    if (!signer) {
      throw new Error(`Account not found: ${from}`);
    }

    const data = toBuffer(msgParams.data);
    const digest = hashPersonalMessage(data);
    const signature = await signer.sign(digest);

    return `0x${signature.toString()}`;
  }
Example #6
Source File: txListener.ts    From remix-project with MIT License 5 votes vote down vote up
constructor (opt, executionContext) {
    this.event = new EventManager()
    // has a default for now for backwards compatability
    this.executionContext = executionContext
    this._api = opt.api
    this._resolvedTransactions = {}
    this._resolvedContracts = {}
    this._isListening = false
    this._listenOnNetwork = false
    this._loopId = null
    this.init()
    this.executionContext.event.register('contextChanged', (context) => {
      if (this._isListening) {
        this.stopListening()
        this.startListening()
      }
    })

    opt.event.udapp.register('callExecuted', async (error, from, to, data, lookupOnly, txResult) => {
      if (error) return
      // we go for that case if
      // in VM mode
      // in web3 mode && listen remix txs only
      if (!this._isListening) return // we don't listen
      if (this._loopId) return // we seems to already listen on a "web3" network

      let returnValue
      let execResult
      if (this.executionContext.isVM()) {
        execResult = await this.executionContext.web3().eth.getExecutionResultFromSimulator(txResult.transactionHash)
        returnValue = execResult.returnValue
      } else {
        returnValue = toBuffer(addHexPrefix(txResult.result))
      }
      const call = {
        from: from,
        to: to,
        input: data,
        hash: txResult.transactionHash ? txResult.transactionHash : 'call' + (from || '') + to + data,
        isCall: true,
        returnValue,
        envMode: this.executionContext.getProvider()
      }

      addExecutionCosts(txResult, call, execResult)
      this._resolveTx(call, call, (error, resolvedData) => {
        if (!error) {
          this.event.trigger('newCall', [call])
        }
      })
    })

    opt.event.udapp.register('transactionExecuted', (error, from, to, data, lookupOnly, txResult) => {
      if (error) return
      if (lookupOnly) return
      // we go for that case if
      // in VM mode
      // in web3 mode && listen remix txs only
      if (!this._isListening) return // we don't listen
      if (this._loopId) return // we seems to already listen on a "web3" network
      this.executionContext.web3().eth.getTransaction(txResult.transactionHash, async (error, tx) => {
        if (error) return console.log(error)

        let execResult
        if (this.executionContext.isVM()) {
          execResult = await this.executionContext.web3().eth.getExecutionResultFromSimulator(txResult.transactionHash)
        }

        addExecutionCosts(txResult, tx, execResult)
        tx.envMode = this.executionContext.getProvider()
        tx.status = txResult.receipt.status // 0x0 or 0x1
        this._resolve([tx], () => {
        })
      })
    })
  }
Example #7
Source File: txResultHelper.ts    From remix-project with MIT License 5 votes vote down vote up
CONTRACT_ADDRESS_BUFFER = toBuffer(
  [105, 42, 112, 210, 228, 36, 165, 109, 44, 108, 39, 170, 151, 209, 168,
    99, 149, 135, 123, 58])
Example #8
Source File: txResultHelper.ts    From remix-project with MIT License 5 votes vote down vote up
RETURN_VALUE_BUFFER = toBuffer(
  [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
    0, 0, 0, 0, 0, 0, 0, 0, 1])
Example #9
Source File: fix-signature.ts    From ethereum-sdk with MIT License 5 votes vote down vote up
function toRpcSig(v: number, r: Buffer, s: Buffer) {
	return bufferToHex(Buffer.concat([setLengthLeft(r, 32), setLengthLeft(s, 32), toBuffer(v)]))
}
Example #10
Source File: is-signer.test.ts    From ethereum-sdk with MIT License 5 votes vote down vote up
function toRpcSig(v: number, r: Buffer, s: Buffer) {
	return bufferToHex(Buffer.concat([setLengthLeft(r, 32), setLengthLeft(s, 32), toBuffer(v)]))
}