Skip to content
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

Swift Raw Transaction creation problem #308

Open
IhorStasiv opened this issue Jan 5, 2024 · 0 comments
Open

Swift Raw Transaction creation problem #308

IhorStasiv opened this issue Jan 5, 2024 · 0 comments

Comments

@IhorStasiv
Copy link

IhorStasiv commented Jan 5, 2024

Hi, I am using your SDK in "swift" programming language. I'm trying to create a Bitcoin Raw Transaction and your SDK generates a hash of the transaction and when it comes to sending it to the network, various services return me various errors as if something is missing in this generated hash. I do everything as in your example (https://github.com/trustwallet/developer/blob/master/wallet-core/wallet-core-usage.md). Maybe I missed something or something was not mentioned in the example, I don't know.
Here is part of the code I use

let coin: CoinType = .bitcoin
            guard let wallet = HDWallet(mnemonic: UserDefaultsService.getMnemonic(), passphrase: "") else { return "" }
            
            let senderAddress = wallet.getAddressForCoin(coin: coin)
            let changeAddress = senderAddress
            let secretPrivateKey = wallet.getKeyForCoin(coin: coin)
            let outPoint = BitcoinOutPoint.with {
                $0.hash = Data(hexString: String(model.utxo.transactionHash.reversed()))!
                $0.index = UInt32(model.utxo.index)
            }
            let utxo = BitcoinUnspentTransaction.with {
                $0.amount = Int64(model.utxo.value)
                $0.outPoint = outPoint
                $0.script = BitcoinScript.lockScriptForAddress(address: senderAddress, coin: coin).data
            }
            var input = BitcoinSigningInput.with {
                $0.amount = Int64(model.amount)
                $0.hashType = TWBitcoinSigHashTypeAll.rawValue
                $0.toAddress = model.recipientAddress
                $0.changeAddress = changeAddress
                $0.byteFee = Int64(model.fee)
                $0.coinType = coin.rawValue
            }
            input.utxo.append(utxo)
            input.privateKey.append(secretPrivateKey.data)
            
            let output: BitcoinSigningOutput = AnySigner.sign(input: input, coin: coin)
            return output.encoded.compactMap { String(format: "%02x", $0) }.joined()

I am trying to send this transaction to the network using different services: (https://blockchair.com/api/docs#link_201) (https://www.blockcypher.com/dev/bitcoin/#introduction)
Blockchair error says: "Invalid transaction. Error: bad-txns-inputs-missingorspent"
Blockcypher error says: "Error validating transaction: Transaction 59941837cb24649f473a61a8a5776abc7ce702924c800719c10134f3cc962ab5 orphaned, missing reference dbfbf1fa4400f3f50d46897ef0274e3c4ae1a95deca38fbb80c46e9ed92e568b."

I also tried to decode the transaction hash using this decoder: (https://live.blockcypher.com/btc/decodetx/)
Here is the transaction decoded:

{
    "addresses": [
        "bc1q9xz0h2wau4ufh0hmqmtq6r4ajmh3f4sahdm6kh",
        "bc1qr5kxndrur6em36txtz5gv7xvzn8sz8mvkpezu6"
    ],
    "block_height": -1,
    "block_index": -1,
    "confirmations": 0,
    "double_spend": false,
    "fees": 0,
    "hash": "59941837cb24649f473a61a8a5776abc7ce702924c800719c10134f3cc962ab5",
    "inputs": [
        {
            "age": 0,
            "output_index": 1,
            "prev_hash": "dbfbf1fa4400f3f50d46897ef0274e3c4ae1a95deca38fbb80c46e9ed92e568b",
            "script_type": "empty",
            "sequence": 0
        }
    ],
    "opt_in_rbf": true,
    "outputs": [
        {
            "addresses": [
                "bc1q9xz0h2wau4ufh0hmqmtq6r4ajmh3f4sahdm6kh"
            ],
            "script": "00142984fba9dde5789bbefb06d60d0ebd96ef14d61d",
            "script_type": "pay-to-witness-pubkey-hash",
            "value": 800
        },
        {
            "addresses": [
                "bc1qr5kxndrur6em36txtz5gv7xvzn8sz8mvkpezu6"
            ],
            "script": "00141d2c69b47c1eb3b8e96658a88678cc14cf011f6c",
            "script_type": "pay-to-witness-pubkey-hash",
            "value": 54302
        }
    ],
    "preference": "low",
    "received": "2024-01-05T08:24:43.087260392Z",
    "relayed_by": "3.236.203.132",
    "size": 223,
    "total": 55102,
    "ver": 1,
    "vin_sz": 1,
    "vout_sz": 2,
    "vsize": 141
}

I hope for your help, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant