nvíe transacciones firmadas a Ropsten o Truffle para desarrollar una red con Trezor (billetera de hardware)
Estoy tratando de integrar web3js con Trezor en untruf dev network o usandoropsten test network.
La idea es firmar las transacciones usando elhardware wallet y luego envíe una transacción sin procesar usando web3js
Estoy entendiendo que no tenemos saldo para hacer la transacción, probablemente porque web3js no está tomando una de las 10 cuentas de trufas y está usando la dirección de trezor que no está en mi red local ..
En Ropsten tengo algunos éteres y obtengo "dirección no válida"
¿Hay alguna forma de enviar transacciones firmadas (con trezor) usando web3js en una red de desarrollo de trufas? Quiero decir, ¿hay alguna manera de incluir la dirección de trezor en la red de trufas?
La situación de la trufa se explica más detalladamente aquí, pero la pregunta podría generalizarse a "Existe alguna forma de incluir billeteras de hardware en la red de desarrollo de trufas? ":https: //github.com/trufflesuite/truffle/issues/97
Utilizando ropsten, he logrado enviar una transacción y recibir un hash de transacción en la devolución de llamada, pero si consultamos esa transacción, obtenemos que la transacción no existe ... entonces ... ¿cómo es eso posible?
Traté de implementar un contrato en Ropsten también y ahora obtengo "Dirección no válida" al invocar una función de contrato inteligente. Tal vez la función de firma es incorrecta? ¿Alguien podría integrar la firma de transacciones de Trezor con web3js?
¿Ves algo malo en el proceso de firma y envío que hemos seguido? Tal vez hay algo mal en el manejo de los parámetros R, V y S ..
Otra cosa importante es que estoy usandohttps: //github.com/ethereumjs/ethereumjs-t para crear las transacciones en bruto
Los temas publicados en web3js, truffle y trezzor se conectan con más información:
https: //github.com/trufflesuite/truffle/issues/97https: //github.com/ethereum/web3.js/issues/166https: //github.com/trezor/connect/issues/13Saludos cordiale
trezorLogin = async()=> {
let trezor= await this.getTrezor();
// site icon, optional. at least 48x48px
var hosticon = 'https://doc.satoshilabs.com/trezor-apps/_images/copay_logo.png';
// server-side generated and randomized challenges
var challenge_hidden = '';
var challenge_visual = '';
//use anonimous functions on callback otherwise returns cross origin errors
trezor.requestLogin(hosticon, challenge_hidden, challenge_visual, function (result){
if (result.success) {
console.log('Public key:', result.public_key); // pubkey in hex
console.log('Signature:', result.signature); // signature in hex
console.log('Version 2:', result.version === 2); // version field
console.log(result);
}else {
console.error('Error:', result.error);
}
});}
trezorSignTx= async(transaction)=> {
let trezor= await this.getTrezor();
// spend one change output
var address_n = "m/44'/60'/0'/0/0"
// var address_n = [44 | 0x80000000,
// 60 | 0x80000000,
// 0 | 0x80000000 ,
// 0 ]; // same, in raw form
var nonce = transaction.nonce.substring(2); // note - it is hex, not number!!!
var gas_price = transaction.gasPrice.substring(2);
var gas_limit = transaction.gasLimit.substring(2);
var to = transaction.to.substring(2);
// var value = '01'; // in hexadecimal, in wei - this is 1 wei
var value = transaction.value.substring(2); // in hexadecimal, in wei - this is about 18 ETC
var data = transaction.data.substring(2); // some contract data
// var data = null // for no data
var chain_id = 5777; // 1 for ETH, 61 for ETC
return new Promise (function (resolve,reject) {
trezor.ethereumSignTx(
address_n,
nonce,
gas_price,
gas_limit,
to,
value,
data,
chain_id,
function (response) {
if (response.success) {
console.log('Signature V (recovery parameter):', response.v); // number
console.log('Signature R component:', response.r); // bytes
console.log('Signature S component:', response.s); // bytes
resolve(response);
} else {
console.error('Error:', response.error); // error message
resolve(null);
}
});
})
}
getTrezorAddress = async() => {
let trezor= await this.getTrezor();
// spend one change output
var address_n = "m/44'/60'/0'/0/0";
trezor.ethereumGetAddress(address_n, function (result) {
if (result.success) { // success
console.log('Address: ', result.address);
} else {
console.error('Error:', result.error); // error message
}
});
}
getTrezor = async() => {
let trezorC;
await getTrezorConnect
.then(trezorConnect => {
trezorC= trezorConnect;
})
.catch((error) => {
console.log(error)
})
return trezorC;
}
sendTransaction= async(address, amount, id)=>{
let tokenInstance = this.props.smartContractInstance;
var getData = tokenInstance.mint.getData(address, amount);
var tx = {
nonce: '0x00',
, gasPrice: '0x09184e72a000',
gasLimit: '0x2710',
to: CONTRACT_ADDRESS,
value: '0x00',
from:CONTRACT_OWNER_ADDRESS,
data: getData
};
let response = await this.trezorSignTx(tx);
let web3;
let _this = this;
if (response!=null){
getWeb3
.then(results => {
web3= results.web3;
let v = response.v.toString();
if (v.length % 2 != 0){
v="0"+v;
}
tx.r=Buffer.from(response.r,'hex');
tx.v=Buffer.from(v,'hex');
tx.s=Buffer.from(response.s,'hex');
let ethtx = new ethereumjs(tx);
console.dir(ethtx.getSenderAddress().toString('hex'), );
const serializedTx = ethtx.serialize();
const rawTx = '0x' + serializedTx.toString('hex');
console.log(rawTx);
//finally pass this data parameter to send Transaction
web3.eth.sendRawTransaction(rawTx, function (error, result) {
if(!error){
_this.props.addTokens(id)
.then(()=>{
_this.setState({modalOpen: true});
_this.props.getAllTransactions();
}
);
}else{
alert(error)
}
});
})
.catch((error) => {
console.log(error)
})
}else{
alert("There was an error signing with trezor hardware wallet")
}
}
La función getTrezorConnect es simplemente obtener window.trezorConnect de forma asincrónica porque el objeto se inyecta como script
<script src="https://connect.trezor.io/4/connect.js"></script>
let getTrezorConnect = new Promise(function(resolve, reject) {
// Wait for loading completion
window.addEventListener('load', function() {
var trezorConnect = window.TrezorConnect
return resolve(trezorConnect)
})});
export default getTrezorConnect