-
Notifications
You must be signed in to change notification settings - Fork 416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to sign an transanction #166
Comments
https://github.com/Bit-Wasp/bitcoin-php/blob/master/examples/offlinetx.p2pkh.testnet.php#L34 You can probably just do: $signed = TransactionFactory::sign($transaction)
-> sign (0, $private_key, $output->getScript())
-> get(); This assumes $output is a |
If I have not full raw transaction hex , And only have transaction index hash like
|
$private_key = PrivateKeyFactory::fromWif('address1 wif private key');
try {
$transaction = TransactionFactory::build()
->input('unspent hash of address1','2')// unspent hash value : 147900
->payToAddress('39479',AddressFactory::fromString('address2'))
->payToAddress('97200',AddressFactory::fromString('address1'))// Give change
->payToAddress('1221',AddressFactory::fromString('address3'))
->get();
$hex = $transaction->getHex();
$myTx = TransactionFactory::fromHex($hex);
$hex = TransactionFactory::sign($transaction)
-> sign (0, $private_key, $myTx->getOutputs()->get(2)->getScript())
-> get() -> getHex();
echo ($hex);
} catch (Exception $e) {
echo $e->getMessage().PHP_EOL;
}
|
First, I created a test transaction, and broadcast using the same block explorer - it worked! I think I see your problem however. $myTx is the transaction you just created, so you are passing the wrong output script to sign(). Just so it's clear, when you pass an output script to sign(), it needs to be the output script of the [txid,vout] you want to spend. Meaning TransactionFactory::fromHex() isn't called on your new transactions hex, but actually the transaction who's hash you are now spending. |
@afk11 How to create a transaction and sign it on offline wallet ? only have from address's unspent hash (isn't full raw transaction). use
updateMaybe I know how to do. the sign script it's unspent transaction ~ use code : function createTransaction(Array $inputs,$private_key,Array $outputs) {
try {
// Load private key
$private_key = PrivateKeyFactory::fromWif($private_key);
// Generate raw transaction
$transaction = TransactionFactory::build();
foreach ($inputs as $input) {
$transaction->input($input['txid'],$input['vout'], ScriptFactory::fromHex($input['scriptPubKey']));
}
foreach ($outputs as $address=>$amount)
$transaction->payToAddress($amount,AddressFactory::fromString($address));
// Make transaction
$transaction = $transaction->get();
// Sign transaction
$signer = TransactionFactory::sign($transaction);
foreach ($transaction->getInputs() as $index=>$input) {
$signer->sign($index,$private_key,$input->getScript());
}
// Signing and get output
$hex = $signer -> get() -> getHex();
return $hex;
} catch(Exception $e) {
self::$error = $e->getMessage();
return false;
}
} get raw transaction hex code:
got an error. Other error
|
The Gmp error is because you passed the transactions output value as an integer but they need to be strings, otherwise they get casted to floats. |
So, I must loop all inputs to set transaction input for function And in signing process, sign all inputs(unspent transactions) use the from address's private key and the input's Or in signing process should be sign all output and use My custom function maybe like below ? // Load private key
$private_key = PrivateKeyFactory::fromWif($private_key);
// Generate raw transaction
$transaction = TransactionFactory::build();
foreach ($inputs as $input) {
$transaction->input(
$input['txid'],
$input['vout'],
ScriptFactory::fromHex($input['scriptPubKey']),
'4294967295');
}
foreach ($outputs as $address=>$amount)
$transaction->payToAddress($amount,AddressFactory::fromString($address));
// Make transaction
$transaction = $transaction->get();
// Sign transaction
$signer = TransactionFactory::sign($transaction);
// Here!!
foreach ($transaction->getInputs() as $index=>$input)
$signer->sign($index,$private_key,$input->getScript());
// Signing and get output
$hex = $signer -> get() -> getHex();
return $hex; Platform error:E..... Maybe it's the platform have some errors. I changed to other platform use the code, It's successfully. Error platform:
Success platform:
Maybe Bitcoind have a new changes to verify transaction. Lastdefault |
The script should be the output script, since that's what actually locked the input you are spending. |
Closing due to inactivity. |
Updating topic... |
@Gwinest Don't post questions on very old issues - I've created another one for your question. |
And then ? How to sign it?
The text was updated successfully, but these errors were encountered: