Transaction Fail when trying to swap on Uniswap V3 programmatically - javascript

I'm trying to create a script to execute a swap on Uniswap V3.
The code below works perfectly well on the Goerli Testnet Network but NOT on Mainnet.
In order to make in run on mainnet:
i've changed the tokens addresses to match the mainnet ones and also changed the INFURA_URL to the mainnet url but I keep getting failed transactions..
const address0 = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
const address1 = '0x6B175474E89094C44Da98b954EedeAC495271d0F'
Seems like the error might come from the swapRouterAddress but not sure..
Moreover, when I execute the swap manually using Uniswap website, i can see that the swap was executed using Uniswap V3: Router 2, but even if I use this Router Address on my script the transaction keep failing..
Does anyone knows how to solve this ?
Thanks a lot !
const { ethers } = require('ethers')
const { abi: IUniswapV3PoolABI } = require('#uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json')
const { abi: SwapRouterABI} = require('#uniswap/v3-periphery/artifacts/contracts/interfaces/ISwapRouter.sol/ISwapRouter.json')
const { abi: UniswapV3Factory } = require('#uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json')
const { getPoolImmutables, getPoolState } = require('./helpers')
const ERC20ABI = require('./abi.json')
require('dotenv').config()
const INFURA_URL_TESTNET = process.env.INFURA_URL_TESTNET
// Wallets
const WALLETS = [process.env.WALLET_ADDRESS_1];
var SECRET = new Object();
SECRET[process.env.WALLET_ADDRESS_1] = process.env.WALLET_SECRET_1;
// Provider
const provider = new ethers.providers.JsonRpcProvider(INFURA_URL_TESTNET) // Goerli
const swapRouterAddress = '0xE592427A0AEce92De3Edee1F18E0157C05861564'
// Wrapped Ether
const name0 = 'Wrapped Ether'
const symbol0 = 'WETH'
const decimals0 = 18
const address0 = '0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6'
// Token info
const name1 = 'Token'
const decimals1 = 18
const address1 = '0x11fE4B6AE13d2a6055C8D9cF65c55bac32B5d844' // DAI
// SWAP
const factoryAddress = '0x1F98431c8aD98523631AE4a59f267346ea31F984'
async function buy(WETH_amount) {
const factoryContract = new ethers.Contract(
factoryAddress,
UniswapV3Factory,
provider
)
const poolAddress = await factoryContract.getPool(address0, address1, 500)
for (const WALLET of WALLETS)
{
const poolContract = new ethers.Contract(
poolAddress,
IUniswapV3PoolABI,
provider
)
const immutables = await getPoolImmutables(poolContract)
const state = await getPoolState(poolContract)
var WALLET_SECRET = SECRET[WALLET];
const wallet = new ethers.Wallet(WALLET_SECRET)
const connectedWallet = wallet.connect(provider)
const swapRouterContract = new ethers.Contract(
swapRouterAddress,
SwapRouterABI,
provider
)
const inputAmount = WETH_amount
// .001 => 1 000 000 000 000 000
const amountIn = ethers.utils.parseUnits(
inputAmount.toString(),
decimals0
)
const params = {
tokenIn: immutables.token1,
tokenOut: immutables.token0,
fee: immutables.fee,
recipient: WALLET,
deadline: Math.floor(Date.now() / 1000) + (60 * 5),
amountIn: amountIn,
amountOutMinimum: 0,
sqrtPriceLimitX96: 0,
}
const transaction = swapRouterContract.connect(connectedWallet).exactInputSingle(
params,
{
gasLimit: ethers.utils.hexlify(1000000)
}
).then(transaction => {
console.log(transaction)
})
}
}
// MAIN
buy(WETH_amount_=0.001)

Note that the token address for WETH is different on mainnet - your code has the address for WETH on Goerli. See https://docs.uniswap.org/protocol/reference/deployments for some token addresses

Related

Binance API-key Trading Bot - InvalidNonce: binance {"code":-1021,"msg":"Timestamp for this request is outside of the recvWindow."}

I have re-created a Binance API-key trading bot which uses NodeJS, CCXT & Axios (It pulls the API+secret key from a .env file). I am getting the error when trying to execute the trading-bot;;
PS C:\Users\mwalk\Documents\Crypto-Bot> node index.js
C:\Users\mwalk\Documents\Crypto-Bot\node_modules\ccxt\js\base\Exchange.js:640
throw new exact[string] (message)
^
InvalidNonce: binance {"code":-1021,"msg":"Timestamp for this request
is outside of the recvWindow."}
at binance.throwExactlyMatchedException (C:\Users\mwalk\Documents\Crypto-Bot\node_modules\ccxt\js\base\Exchange.js:640:19)
require('dotenv').config();
const ccxt = require('ccxt');
const axios = require('axios');
const tick = async(config, binanceClient) => {
const { asset, base, spread, allocation } = config
const market = `${asset}/${base}`;
const orders = await binanceClient.fetchOpenOrders(market);
orders.forEach(async order => {
await binanceClient.cancelOrder(order.id);
});
const results = await Promise.all([
axios.get('https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd'),
axios.get('https://api.coingecko.com/api/v3/simple/price?ids=tether&vs_currencies=usd')
]);
const marketPrice = results [0].data.bitcoin.usd / results[1].data.tether.usd;
const sellPrice = marketPrice * (1 + spread);
const buyPrice = marketPrice * (1 - spread);
const balances = await binanceClient.fetchBalance();
const assetBalance = balances.free[asset];
const baseBalance = balances.free[base];
const sellVolume = assetBalance * allocation;
const buyVolume = (baseBalance * allocation) / marketPrice;
await binanceClient.createLimitSellOrder(market, sellVolume, sellPrice);
await binanceClient.createLimitBuyOrder(market, buyVolume, buyPrice);
console.log(`
New tick for ${market}...
Created limit sell order for ${sellVolume}#${sellPrice}
Create limit buy order for ${buyVolume}#${buyPrice}
`);
}
const run = () => {
const config = {
asset: 'BTC',
base: 'USDT',
allocation: 0.1,
spread: 0.1,
tickInterval: 2000
};
const binanceClient = new ccxt.binance({
apiKey: process.env.API_ENV,
secret: process.env.API_SECRET
});
tick(config, binanceClient);
setInterval(tick, config.tickInterval, config, binanceClient);
};
run();
sync your pc time with nist option from your control panel

Uniswap USDC=> ETH exchange

Trying to exchange USDC to ETH vith Uniswap and Ethers, but getting errors all the time.
async function swapUsdcToEth(amount, walledAddress) {
const usdc = await Fetcher.fetchTokenData(chainId, '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48');
const eth = await Fetcher.fetchTokenData(chainId, '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2');
const pair = await Fetcher.fetchPairData(usdc, eth);
const route = new Route([pair], usdc);
const amountIn = new TokenAmount(usdc, amount);
const trade = new Trade(route, amountIn, TradeType.EXACT_INPUT);
const slippageTolerance = new Percent('50', '10000');
const value = ethers.BigNumber.from(trade.inputAmount.raw.toString()).toHexString();
const amountOutMin = ethers.BigNumber.from(trade.minimumAmountOut(slippageTolerance).raw.toString()).toHexString();
const deadline = Math.floor(Date.now() / 1000) + 60 * 20;
const uniswapRouterV2Address = '0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D';
const provider = new ethers.providers.JsonRpcProvider('https://mainnet.infura.io/v3/b9fkdmkdkdv4937b52ea9637cf1d1bd');
const signer = new ethers.Wallet(walledAddress, provider);
const uniswap = new ethers.Contract(
uniswapRouterV2Address,
['function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'],
signer
);
try {
const tx = await uniswap.swapExactTokensForETH(value, amountOutMin, [walledAddress], walledAddress, deadline);
const receipt = await tx.wait();
console.log('transaction was mined in block', receipt.blockNumber);
} catch (e) {
console.log(e);
}
}
Receiving errors like that: ' Error: cannot estimate gas; transaction may fail or may require manual gas limit '. What I am doing wrong?
So it looks like possibly a few things. Have you approved your token to be spent by the router?
Also I'm not seeing any gas settings in there
here is a working version that I have modified to make work ( This is setup to test on a mainnet fork, however it's using live data (as the AlphaRouter is only for mainnet live so the live data and the fork data will change with time and cause trade errors, so reset fork before using)
to make main net only take out local provider and change all providers to main net provider.
USE WITH CAUTION, I'M NOT PERFECT AND GUARANTEE NOTHING : Check slippage and all variables
This is built As a TOKEN to TOKEN Exact_Input swap with WETH deposit of 1 ETH.
To use ETH you can remove Weth deposit and token in approval and use the BigNumber.from(typedValueParsed) as the value of the transaction instead of 0
As I don't know EtherJS to well the Gas Price and Gas Limit for the Deposit and approval is a flat 100 gwei & 300k Limit, and should be modified for current network gas price and estimated gas Limit.
import { AlphaRouter } from '#uniswap/smart-order-router'
import { Token, CurrencyAmount } from '#uniswap/sdk-core'
import { JSBI, Percent } from "#uniswap/sdk";
import { ethers, BigNumber } from "ethers";
const V3_SWAP_ROUTER_ADDRESS = "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45";
const TokenInput = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2";
const TokenOutput = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const web3Provider = new ethers.providers.JsonRpcProvider("https://eth-mainnet.alchemyapi.io/v2/");
const web3 = new ethers.providers.JsonRpcProvider("http://127.0.0.1:8545/");
const privateKey = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
const wallet = new ethers.Wallet(privateKey,web3);
const address = wallet.address;
import * as fs from 'fs';
let UniV3RouterAbi = fs.readFileSync('NewUniRouter.json');
const V3routerAbi = JSON.parse(UniV3RouterAbi);
let ERC20Abi = fs.readFileSync('ERC20.json');
const ERC20 = JSON.parse(ERC20Abi);
let WETHAbij = fs.readFileSync('WETHAbi.json');
const WETHAbi = JSON.parse(WETHAbij);
async function log(inpt){
console.log(inpt);
console.log("");
}
async function TokBal(tokens){
var ERC20contract = new ethers.Contract(tokens, ERC20, web3);
var myERC20bal = await ERC20contract.balanceOf(wallet.address);
return myERC20bal;
}
async function Deposit(amt){
var WethC = new ethers.Contract(TokenInput, WETHAbi, web3);
var datac = await WethC.populateTransaction["deposit"]();
var ncn = await wallet.getTransactionCount();
const transaction = {
data: datac.data,
nonce: ncn,
to: TokenInput,
value: BigNumber.from(amt),
from: wallet.address,
gasPrice: '0x174876e800',
gasLimit: '0x493e0',
};
const signedTx = await wallet.signTransaction(transaction);
const txHash = await web3.sendTransaction(signedTx);
log(txHash.hash);
}
async function Approve(Toked, amt){
var WethC = new ethers.Contract(Toked, ERC20, web3);
var datac = await WethC.populateTransaction["approve"](V3_SWAP_ROUTER_ADDRESS, amt);
var ncn = await wallet.getTransactionCount();
const transaction = {
data: datac.data,
nonce: ncn,
to: Toked,
value: BigNumber.from("0"),
from: wallet.address,
gasPrice: '0x174876e800',
gasLimit: '0x493e0',
};
const signedTx = await wallet.signTransaction(transaction);
const txHash = await web3.sendTransaction(signedTx);
log(txHash.hash);
var appFor = await WethC.callStatic.allowance(wallet.address, V3_SWAP_ROUTER_ADDRESS);
log("Approved : "+appFor.toString());
}
const router = new AlphaRouter({ chainId: 1, provider: web3Provider });
const WETH = new Token(
router.chainId,
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
18,
'WETH',
'Wrapped Ether'
);
const USDC = new Token(
router.chainId,
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
6,
'USDC',
'USD//C'
);
const typedValueParsed = '1000000000000000000';
const wethAmount = CurrencyAmount.fromRawAmount(WETH, JSBI.BigInt(typedValueParsed));
const IO = "Exact_Input"
const TradeType = IO == "Exact_Input" ? 0 : 1;
const route = await router.route(
wethAmount,
USDC,
TradeType,
{
recipient: wallet.address,
slippageTolerance: new Percent(5, 100),
deadline: Math.floor(Date.now()/1000 +1800)
}
);
var Ebal = await web3.getBalance(wallet.address);
log("Wallet Balance : "+Ebal.toString());
var tbal = await TokBal(TokenOutput);
log("Token Out Balance : "+tbal.toString());
await Deposit("1000000000000000000");
await Approve(TokenInput,"1000000000000000000");
var tbalW = await TokBal(TokenInput);
log("Token In Balance : "+tbalW.toString());
log(`Quote Exact In: ${route.quote.toFixed(wethAmount.currency === WETH ? USDC.decimals : WETH.decimals)}`);
log(`Gas Adjusted Quote In: ${route.quoteGasAdjusted.toFixed(wethAmount.currency === WETH ? USDC.decimals : WETH.decimals)}`);
var nc = await wallet.getTransactionCount();
const transaction = {
data: route.methodParameters.calldata,
nonce: nc,
to: V3_SWAP_ROUTER_ADDRESS,
value: BigNumber.from(0),
from: wallet.address,
gasPrice: BigNumber.from(route.gasPriceWei),
gasLimit: BigNumber.from(route.estimatedGasUsed).add(BigNumber.from("50000")),
};
const signedTx = await wallet.signTransaction(transaction);
const PretxHash = ethers.utils.keccak256(signedTx);
const txHash = await web3.sendTransaction(signedTx)
log(txHash.hash);
var Ebal = await web3.getBalance(wallet.address);
log("Wallet Balance : "+Ebal.toString());
var tbal = await TokBal(TokenOutput);
log("Token Out Balance : "+tbal.toString());
var tbalW = await TokBal(TokenInput);
log("Token In Balance : "+tbalW.toString());
to get ETH use this in place of the output token
outPutAddress === WETH9[chainId].address ? nativeOnChain(chainId) : outPutToken,
EDIT>>>>>>>>>>
modified for new versions
import { ethers } from 'ethers'
import {AlphaRouter, ChainId, SwapType, nativeOnChain} from '#uniswap/smart-order-router'
import { TradeType, CurrencyAmount, Percent, Token } from '#uniswap/sdk-core'
import { TickMath } from '#uniswap/v3-sdk';
import JSBI from 'jsbi';
import bn from 'bignumber.js'
async function main() {
const MY_ADDRESS = "<ADDRESS>";
const web3Provider = new ethers.providers.JsonRpcProvider('https://eth-mainnet.alchemyapi.io/v2/<RPC_KEY>')
const router = new AlphaRouter({ chainId: 1, provider: web3Provider });
const WETH = new Token(
1,
'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',
18,
'WETH',
'Wrapped Ether'
);
const USDC = new Token(
ChainId.MAINNET,
'0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48',
6,
'USDC',
'USD//C'
);
const AAVE = new Token(
ChainId.MAINNET,
'0x7Fc66500c84A76Ad7e9c93437bFc5Ac33E2DDaE9',
18,
'AAVE',
'AAVE'
);
const options = {
recipient: MY_ADDRESS,
slippageTolerance: new Percent(10, 1000),
deadline: Math.floor(Date.now() / 1000 + 1800),
type: SwapType.SWAP_ROUTER_02,
}
const typedValueParsed = '10000000000000000000'
const route = await router.route(
CurrencyAmount.fromRawAmount(
AAVE,
typedValueParsed.toString()
),
nativeOnChain(ChainId.MAINNET),
TradeType.EXACT_INPUT,
options
)
console.log(`Quote Exact In: ${route.quote.toFixed(route.quote.currency.decimals)}`);
console.log(`Gas Adjusted Quote In: ${route.quoteGasAdjusted.toFixed(route.quote.currency.decimals)}`);
console.log(`Gas Used USD: ${route.estimatedGasUsedUSD.toFixed(2)}`);
console.log(route.methodParameters.calldata);
}
main()

Listing NFT on OpenSea via JS API: Failed to extract transfer calldata Error 400

I am trying to use the OpenSea JavaScript API in order to list NFTs for sale automatically. I can't seem to figure out why I keep getting the error Error: API Error 400: ['Failed to extract transfer calldata']. Perhaps I have not entered some data correctly? My code is adapted from this example.
SOURCE
const opensea = require("opensea-js");
const OpenSeaPort = opensea.OpenSeaPort;
const Network = opensea.Network;
const MnemonicWalletSubprovider = require("#0x/subproviders")
.MnemonicWalletSubprovider;
const RPCSubprovider = require("web3-provider-engine/subproviders/rpc");
const Web3ProviderEngine = require("web3-provider-engine");
const MNEMONIC = "SECRET ...";
const NODE_API_KEY = "FROM INFURA";
const isInfura = true;
//const FACTORY_CONTRACT_ADDRESS = process.env.FACTORY_CONTRACT_ADDRESS;
const NFT_CONTRACT_ADDRESS = "0x495f947276749Ce646f68AC8c248420045cb7b5e";
const OWNER_ADDRESS = "0x3eb8eea9565418281f4cae934dd69e7ff2bb5949";
const NETWORK = "mainnet";
const API_KEY = process.env.API_KEY || ""; // API key is optional but useful if you're doing a high volume of requests.
const BASE_DERIVATION_PATH = `44'/60'/0'/0`;
const mnemonicWalletSubprovider = new MnemonicWalletSubprovider({
mnemonic: MNEMONIC,
baseDerivationPath: BASE_DERIVATION_PATH,
});
const network =
NETWORK === "mainnet" || NETWORK === "live" ? "mainnet" : "rinkeby";
const infuraRpcSubprovider = new RPCSubprovider({
rpcUrl: isInfura
? "https://" + network + ".infura.io/v3/" + NODE_API_KEY
: "https://eth-" + network + ".alchemyapi.io/v2/" + NODE_API_KEY,
});
const providerEngine = new Web3ProviderEngine();
providerEngine.addProvider(mnemonicWalletSubprovider);
providerEngine.addProvider(infuraRpcSubprovider);
providerEngine.start();
const seaport = new OpenSeaPort(
providerEngine,
{
networkName:
NETWORK === "mainnet" || NETWORK === "live"
? Network.Main
: Network.Rinkeby,
apiKey: API_KEY,
},
(arg) => console.log(arg)
);
exports.helloWorld = async (req, res) => {
// Example: simple fixed-price sale of an item owned by a user.
console.log("Auctioning an item for a fixed price...");
try {
const fixedPriceSellOrder = await seaport.createSellOrder({
asset: {
tokenId: "28370143653034713195993216915191765879963367987017833025925208213530804748289",
tokenAddress: NFT_CONTRACT_ADDRESS,
},
startAmount: 0.1,
expirationTime: 0,
accountAddress: OWNER_ADDRESS,
});
console.log(
`Successfully created a fixed-price sell order! ${fixedPriceSellOrder.asset.openseaLink}\n`
);
}
catch (error) {
console.log("ERROR",error);
}
};
FULL LOGS
WHERE I GOT DATA
ASSET URL
What is your Token Standard?
If it is not ERC721, you need to specify it as your schemaName inside the asset object, like this :
const fixedPriceSellOrder = await seaport.createSellOrder({
asset: {
tokenId: "28370143653034713195993216915191765879963367987017833025925208213530804748289",
tokenAddress: NFT_CONTRACT_ADDRESS,
schemaName: "ERC1155"
},
startAmount: 0.1,
expirationTime: 0,
accountAddress: OWNER_ADDRESS,
});
You can find Token Standard in Details tab, on Opensea asset page :
Remove the dash and you're good. ERC1155 was fine for me.

Lowdb return undefined

I'm doing a Discord bot with command handling, but on a file, I can't get the content of my JSON file out with lowdb... I proceed exactly the same way with success in the other files, I don't understand... Here is my code:
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('../db.json')
const db = low(adapter)
const adapter2 = new FileSync('../users.json')
const users = low(adapter2)
const fetch = require('node-fetch');
const config = require('../config.json');
const api = config.api;
module.exports = {
name: 'rent',
description: 'Rent a number',
usage: '<country>',
guildOnly: true,
async execute(message, args) {
return console.log(db.get().value())
...
Here's my db.json:
{
"numbers": [
{
"test": "1234"
}
]
}
When I console.log db alone, it takes me out the object, but as soon as I try to console.log with lowdb like above it takes me out undefined ....
So I'm not sure why, but you have to remove a point on the road to lowdb files.
Code not working:
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('../db.json')
const db = low(adapter)
const adapter2 = new FileSync('../users.json')
const users = low(adapter2)
const fetch = require('node-fetch');
const config = require('../config.json');
const api = config.api;
Code after modification and functional:
const low = require('lowdb')
const FileSync = require('lowdb/adapters/FileSync')
const adapter = new FileSync('./db.json')
const db = low(adapter)
const adapter2 = new FileSync('./users.json')
const users = low(adapter2)
const fetch = require('node-fetch');
const config = require('../config.json');
const api = config.api;

how to sign bitcoin psbt with ledger?

I'm trying to sign a Psbt transaction from bitcoinjs-lib following what I found here:
https://github.com/helperbit/helperbit-wallet/blob/master/app/components/dashboard.wallet/bitcoin.service/ledger.ts
I've checked that the compressed publicKey both from ledger, and the one from bitcoinjsLib returned the same value.
I could sign it with the bitcoinjs-lib ECPair, but when I tries to sign it using ledger, it is always invalid.
Can someone helps me point out where did I made a mistake?
These variables is already mentioned in the code below, but for clarity purpose:
- mnemonics:
abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about
- previousTx:
02000000000101869362410c61a69ab9390b2167d08219662196e869626e8b0350f1a8e4075efb0100000017160014ef3fdddccdb6b53e6dd1f5a97299a6ba2e1c11c3ffffffff0240420f000000000017a914f748afee815f78f97672be5a9840056d8ed77f4887df9de6050000000017a9142ff4aa6ffa987335c7bdba58ef4cbfecbe9e49938702473044022061a01bf0fbac4650a9b3d035b3d9282255a5c6040aa1d04fd9b6b52ed9f4d20a022064e8e2739ef532e6b2cb461321dd20f5a5d63cf34da3056c428475d42c9aff870121025fb5240daab4cee5fa097eef475f3f2e004f7be702c421b6607d8afea1affa9b00000000
- paths:
["0'/0/0"]
- redeemScript: (non-multisig segwit)
00144328adace54072cd069abf108f97cf80420b212b
This is my minimum reproducible code I've got.
/* tslint:disable */
// #ts-check
require('regenerator-runtime');
const bip39 = require('bip39');
const { default: Transport } = require('#ledgerhq/hw-transport-node-hid');
const { default: AppBtc } = require('#ledgerhq/hw-app-btc');
const bitcoin = require('bitcoinjs-lib');
const mnemonics = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
const NETWORK = bitcoin.networks.regtest;
/**
* #param {string} pk
* #returns {string}
*/
function compressPublicKey(pk) {
const { publicKey } = bitcoin.ECPair.fromPublicKey(Buffer.from(pk, 'hex'));
return publicKey.toString('hex');
}
/** #returns {Promise<any>} */
async function appBtc() {
const transport = await Transport.create();
const btc = new AppBtc(transport);
return btc;
}
const signTransaction = async() => {
const ledger = await appBtc();
const paths = ["0'/0/0"];
const [ path ] = paths;
const previousTx = "02000000000101869362410c61a69ab9390b2167d08219662196e869626e8b0350f1a8e4075efb0100000017160014ef3fdddccdb6b53e6dd1f5a97299a6ba2e1c11c3ffffffff0240420f000000000017a914f748afee815f78f97672be5a9840056d8ed77f4887df9de6050000000017a9142ff4aa6ffa987335c7bdba58ef4cbfecbe9e49938702473044022061a01bf0fbac4650a9b3d035b3d9282255a5c6040aa1d04fd9b6b52ed9f4d20a022064e8e2739ef532e6b2cb461321dd20f5a5d63cf34da3056c428475d42c9aff870121025fb5240daab4cee5fa097eef475f3f2e004f7be702c421b6607d8afea1affa9b00000000"
const utxo = bitcoin.Transaction.fromHex(previousTx);
const segwit = utxo.hasWitnesses();
const txIndex = 0;
// ecpairs things.
const seed = await bip39.mnemonicToSeed(mnemonics);
const node = bitcoin.bip32.fromSeed(seed, NETWORK);
const ecPrivate = node.derivePath(path);
const ecPublic = bitcoin.ECPair.fromPublicKey(ecPrivate.publicKey, { network: NETWORK });
const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: ecPublic.publicKey, network: NETWORK });
const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network: NETWORK });
const redeemScript = p2sh.redeem.output;
const fromLedger = await ledger.getWalletPublicKey(path, { format: 'p2sh' });
const ledgerPublicKey = compressPublicKey(fromLedger.publicKey);
const bitcoinJsPublicKey = ecPublic.publicKey.toString('hex');
console.log({ ledgerPublicKey, bitcoinJsPublicKey, address: p2sh.address, segwit, fromLedger, redeemScript: redeemScript.toString('hex') });
var tx1 = ledger.splitTransaction(previousTx, true);
const psbt = new bitcoin.Psbt({ network: NETWORK });
psbt.addInput({
hash: utxo.getId(),
index: txIndex,
nonWitnessUtxo: Buffer.from(previousTx, 'hex'),
redeemScript,
});
psbt.addOutput({
address: 'mgWUuj1J1N882jmqFxtDepEC73Rr22E9GU',
value: 5000,
});
psbt.setMaximumFeeRate(1000 * 1000 * 1000); // ignore maxFeeRate we're testnet anyway.
psbt.setVersion(2);
/** #type {string} */
// #ts-ignore
const newTx = psbt.__CACHE.__TX.toHex();
console.log({ newTx });
const splitNewTx = await ledger.splitTransaction(newTx, true);
const outputScriptHex = await ledger.serializeTransactionOutputs(splitNewTx).toString("hex");
const expectedOutscriptHex = '0188130000000000001976a9140ae1441568d0d293764a347b191025c51556cecd88ac';
// stolen from: https://github.com/LedgerHQ/ledgerjs/blob/master/packages/hw-app-btc/tests/Btc.test.js
console.log({ outputScriptHex, expectedOutscriptHex, eq: expectedOutscriptHex === outputScriptHex });
const inputs = [ [tx1, 0, p2sh.redeem.output.toString('hex') /** ??? */] ];
const ledgerSignatures = await ledger.signP2SHTransaction(
inputs,
paths,
outputScriptHex,
0, // lockTime,
undefined, // sigHashType = SIGHASH_ALL ???
utxo.hasWitnesses(),
2, // version??,
);
const signer = {
network: NETWORK,
publicKey: ecPrivate.publicKey,
/** #param {Buffer} $hash */
sign: ($hash) => {
const expectedSignature = ecPrivate.sign($hash); // just for comparison.
const [ ledgerSignature0 ] = ledgerSignatures;
const decodedLedgerSignature = bitcoin.script.signature.decode(Buffer.from(ledgerSignature0, 'hex'));
console.log({
$hash: $hash.toString('hex'),
expectedSignature: expectedSignature.toString('hex'),
actualSignature: decodedLedgerSignature.signature.toString('hex'),
});
// return signature;
return decodedLedgerSignature.signature;
},
};
psbt.signInput(0, signer);
const validated = psbt.validateSignaturesOfInput(0);
psbt.finalizeAllInputs();
const hex = psbt.extractTransaction().toHex();
console.log({ validated, hex });
};
if (process.argv[1] === __filename) {
signTransaction().catch(console.error)
}
Ooof, finally got it working.
My mistake was I was trying to sign a p2sh-p2ms, By following a reference on how to sign a p2sh-p2wsh-p2ms.
And, also, that missing last 2 bit (01), which I think represent SIGHASH_ALL caused an error when I try to decode the signature.
this is my finalized working example.
// #ts-check
require('regenerator-runtime');
const bip39 = require('bip39');
const { default: Transport } = require('#ledgerhq/hw-transport-node-hid');
const { default: AppBtc } = require('#ledgerhq/hw-app-btc');
const serializer = require('#ledgerhq/hw-app-btc/lib/serializeTransaction');
const bitcoin = require('bitcoinjs-lib');
const mnemonics = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about';
const NETWORK = bitcoin.networks.regtest;
const DEFAULT_LOCK_TIME = 0;
const SIGHASH_ALL = 1;
const PATHS = ["m/49'/1'/0'/0/0", "m/49'/1'/0'/0/1"];
async function appBtc() {
const transport = await Transport.create();
const btc = new AppBtc(transport);
return btc;
}
/**
* #param {string} pk
* #returns {string}
*/
function compressPublicKey(pk) {
const {
publicKey
} = bitcoin.ECPair.fromPublicKey(Buffer.from(pk, 'hex'));
return publicKey.toString('hex');
}
/**
* #param {AppBtc} ledger
* #param {bitcoin.Transaction} tx
*/
function splitTransaction(ledger, tx) {
return ledger.splitTransaction(tx.toHex(), tx.hasWitnesses());
}
const signTransaction = async() => {
const seed = await bip39.mnemonicToSeed(mnemonics);
const node = bitcoin.bip32.fromSeed(seed, NETWORK);
const signers = PATHS.map((p) => node.derivePath(p));
const publicKeys = signers.map((s) => s.publicKey);
const p2ms = bitcoin.payments.p2ms({ pubkeys: publicKeys, network: NETWORK, m: 1 });
const p2shP2ms = bitcoin.payments.p2sh({ redeem: p2ms, network: NETWORK });
const previousTx = '02000000000101588e8fc89afea9adb79de2650f0cdba762f7d0880c29a1f20e7b468f97da9f850100000017160014345766130a8f8e83aef8621122ca14fff88e6d51ffffffff0240420f000000000017a914a0546d83e5f8876045d7025a230d87bf69db893287df9de6050000000017a9142ff4aa6ffa987335c7bdba58ef4cbfecbe9e49938702483045022100c654271a891af98e46ca4d82ede8cccb0503a430e50745f959274294c98030750220331b455fed13ff4286f6db699eca06aa0c1c37c45c9f3aed3a77a3b0187ff4ac0121037ebcf3cf122678b9dc89b339017c5b76bee9fedd068c7401f4a8eb1d7e841c3a00000000';
const utxo = bitcoin.Transaction.fromHex(previousTx);
const txIndex = 0;
const destination = p2shP2ms;
const redeemScript = destination.redeem.output;
// const witnessScript = destination.redeem.redeem.output;
const ledgerRedeemScript = redeemScript;
// use witness script if the outgoing transaction was from a p2sh-p2wsh-p2ms instead of p2sh-p2ms
const fee = 1000;
/** #type {number} */
// #ts-ignore
const amount = utxo.outs[txIndex].value;
const withdrawAmount = amount - fee;
const psbt = new bitcoin.Psbt({ network: NETWORK });
const version = 1;
psbt.addInput({
hash: utxo.getId(),
index: txIndex,
nonWitnessUtxo: utxo.toBuffer(),
redeemScript,
});
psbt.addOutput({
address: '2MsK2NdiVEPCjBMFWbjFvQ39mxWPMopp5vp',
value: withdrawAmount
});
psbt.setVersion(version);
/** #type {bitcoin.Transaction} */
// #ts-ignore
const newTx = psbt.__CACHE.__TX;
const ledger = await appBtc();
const inLedgerTx = splitTransaction(ledger, utxo);
const outLedgerTx = splitTransaction(ledger, newTx);
const outputScriptHex = await serializer.serializeTransactionOutputs(outLedgerTx).toString('hex');
/** #param {string} path */
const signer = (path) => {
const ecPrivate = node.derivePath(path);
// actually only publicKey is needed, albeit ledger give an uncompressed one.
// const { publicKey: uncompressedPublicKey } = await ledger.getWalletPublicKey(path);
// const publicKey = compressPublicKey(publicKey);
return {
network: NETWORK,
publicKey: ecPrivate.publicKey,
/** #param {Buffer} $hash */
sign: async ($hash) => {
const ledgerTxSignatures = await ledger.signP2SHTransaction({
inputs: [[inLedgerTx, txIndex, ledgerRedeemScript.toString('hex')]],
associatedKeysets: [ path ],
outputScriptHex,
lockTime: DEFAULT_LOCK_TIME,
segwit: newTx.hasWitnesses(),
transactionVersion: version,
sigHashType: SIGHASH_ALL,
});
const [ ledgerSignature ] = ledgerTxSignatures;
const expectedSignature = ecPrivate.sign($hash);
const finalSignature = (() => {
if (newTx.hasWitnesses()) {
return Buffer.from(ledgerSignature, 'hex');
};
return Buffer.concat([
ledgerSignature,
Buffer.from('01', 'hex'), // SIGHASH_ALL
]);
})();
console.log({
expectedSignature: expectedSignature.toString('hex'),
finalSignature: finalSignature.toString('hex'),
});
const { signature } = bitcoin.script.signature.decode(finalSignature);
return signature;
},
};
}
await psbt.signInputAsync(0, signer(PATHS[0]));
const validate = await psbt.validateSignaturesOfAllInputs();
await psbt.finalizeAllInputs();
const hex = psbt.extractTransaction().toHex();
console.log({ validate, hex });
};
if (process.argv[1] === __filename) {
signTransaction().catch(console.error)
}
My guess is you have a space in the string passed to toByteArray function. This function doesn't trim input. Also doesn't check if input's length is even.

Categories