Last active
August 12, 2023 06:31
-
-
Save mrbianchi/39b1d00104f7183e9eb68cb172dc90b1 to your computer and use it in GitHub Desktop.
Get BTC addresses from ETH Transaction
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { Web3 } = require('web3'); | |
const {TransactionFactory} = require('@ethereumjs/tx') | |
const bitcoin = require('bitcoinjs-lib'); | |
// Conéctate a un nodo local (puedes cambiar la URL si es necesario) | |
const web3 = new Web3('QUICKNODE API EP'); | |
async function getRawTransaction(txHash) { | |
return new Promise((resolve, reject) => { | |
web3.currentProvider.send({ | |
jsonrpc: '2.0', | |
method: 'eth_getRawTransactionByHash', | |
params: [txHash], | |
id: new Date().getTime() | |
}, (error, result) => { | |
if (error) { | |
reject(error); | |
} else { | |
resolve(result.result); | |
} | |
}); | |
}); | |
} | |
function compressPublicKey(uncompressedKey) { | |
if (uncompressedKey[0] !== 0x04) { | |
throw new Error("Not an uncompressed public key"); | |
} | |
const y = uncompressedKey.slice(33); | |
const isOdd = y[y.length - 1] % 2 !== 0; | |
if (isOdd) { | |
return Buffer.concat([Buffer.from([0x03]), uncompressedKey.slice(1, 33)]); | |
} else { | |
return Buffer.concat([Buffer.from([0x02]), uncompressedKey.slice(1, 33)]); | |
} | |
} | |
function getP2PKHAddress(pubKeyBuffer) { | |
const { address } = bitcoin.payments.p2pkh({ pubkey: pubKeyBuffer }); | |
return address; | |
} | |
function getP2SHP2WPKHAddress(pubKeyBuffer) { | |
const { address } = bitcoin.payments.p2sh({ | |
redeem: bitcoin.payments.p2wpkh({ pubkey: pubKeyBuffer }) | |
}); | |
return address; | |
} | |
function getBech32P2WPKHAddress(pubKeyBuffer) { | |
const { address } = bitcoin.payments.p2wpkh({ pubkey: pubKeyBuffer }); | |
return address; | |
} | |
function getBitcoinAddresses(pubKeyHex) { | |
let pubKeyBuffer = Buffer.from(pubKeyHex, 'hex'); | |
let P2PKH_uncompressed = getP2PKHAddress(pubKeyBuffer); | |
// Comprimir la clave pública si es no comprimida | |
if (pubKeyBuffer.length === 65 && pubKeyBuffer[0] === 0x04) { | |
pubKeyBuffer = compressPublicKey(pubKeyBuffer); | |
} | |
if (pubKeyBuffer.length !== 33) { | |
throw new Error('Invalid public key format.'); | |
} | |
return { | |
P2PKH_uncompressed, | |
P2PKH: getP2PKHAddress(pubKeyBuffer), | |
P2SHP2WPKH: getP2SHP2WPKHAddress(pubKeyBuffer), | |
Bech32P2WPKH: getBech32P2WPKHAddress(pubKeyBuffer) | |
}; | |
} | |
async function main() { | |
let rawTransaction = await getRawTransaction("ETHEREUM TXID"); | |
const tx = TransactionFactory.fromSerializedData((0, web3.utils.hexToBytes)(rawTransaction)); | |
const recoveredPubKey = Buffer.from(tx.getSenderPublicKey()).toString('hex');//its uncompressed | |
const btcPubKey = "04" + recoveredPubKey; //04 is for uncompressed pubkeys | |
// Imprime la transacción decodificada | |
console.log(getBitcoinAddresses(btcPubKey)); | |
} | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Of course, derivation schemes like BIP32/BIP39 protect user privacy by preventing this. So, it could only work if the wallet isn't using these standards.