Atomic exchange and publish to a stream in the same transaction

+2 votes

Hi Multichain folks,

I've been using the rawtransaction API to try to build an atomic exchange transaction which also includes a stream publish. Let me explain:

  1. Address A has permissions to publish to Stream 1
  2. Address B is asking for Asset 1 in exchange for Asset 2
  3. Address C will accept Asset 2 for Asset
  4. Address B and Address C do not have permissions to publish to Stream 1

However, when the transaction is broadcast, it is rejected by the blockchain node because the Address B does not have permission to publish to Stream 1. I also noticed that when I decoderawtransaction, the output with the stream publishing info has 2 publishers: Address A and Address B.

Is it possible to publish to a stream and perform and atomic exchange in the same transaction?

Thanks. 

asked Dec 12, 2017 by Steven Chan

1 Answer

+1 vote

The best way to do this is for addresses B and C to first build a regular complete atomic exchange without the stream publish. Then address A adds the stream publish at the end using preparelockunspentfrom + appendrawtransaction with the send keyword.

That way addresses B and C will not have signed the stream publish, and A will have signed everything (but this does not matter). Is that an option in terms of workflow?

answered Dec 12, 2017 by MultiChain
The problem now is with appendrawtransaction created by B. You specified "sign" action there. So the input is already signed with "ALL". Subsequent signrawtransaction has no effect. As a result appending outputs by C violates B's signature.

Here is the sequence that worked for me (the fact that one of addresses is multisig should not play any role) :
address A: 1ZgjGQfo8VccVV65NZKjmsxRtJbdTLykPBJoXs can publish in stream "orders"
address B: 1DorxRWMcW4ptQzUoffvjKF9hYLtw83eQL8PWp has asset2
address C: 134euHtYkHRzoJ3CwatYNZJSz7sjjqQpJ6TaUg has asset1

address A: publish to stream

preparelockunspentfrom 1ZgjGQfo8VccVV65NZKjmsxRtJbdTLykPBJoXs 0

{
    "txid" : "d568f4aa8cd96fdd6b94fcd8e1d77096918883821ada3448beea3cb055f43202",
    "vout" : 1
}

gettxout d568f4aa8cd96fdd6b94fcd8e1d77096918883821ada3448beea3cb055f43202 1

    "scriptPubKey" : {
        "hex" : "76a914f1d56f040d7007213412a05bdca3d6218e57738788ac",


createrawtransaction '[{"txid" : "d568f4aa8cd96fdd6b94fcd8e1d77096918883821ada3448beea3cb055f43202","vout" : 1}]' '{}' '[{"for":"orders","key":"hello","data":"7b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374"}]'

01000000010232f455b03ceabe4834da1a82...

signrawtransaction 01000000010232f455b03ceabe4834da1a82... '[{"txid" : "d568f4aa8cd96fdd6b94fcd8e1d77096918883821ada3448beea3cb055f43202","vout" : 1,"scriptPubKey":"76a914f1d56f040d7007213412a05bdca3d6218e57738788ac"}]' null SINGLE

{
    "hex" : "01000000010232f455b03ce....",
    "complete" : true
}

address B: offers asset2, asks asset1

preparelockunspentfrom 1DorxRWMcW4ptQzUoffvjKF9hYLtw83eQL8PWp '{"asset2":180}'

{
    "txid" : "65925b880fbb2517d7ad1aa32b87345e2f36d9bd456a45852f856b88f68a82f2",
    "vout" : 0
}

gettxout 65925b880fbb2517d7ad1aa32b87345e2f36d9bd456a45852f856b88f68a82f2 0

    "scriptPubKey" : {
        "hex" : "76a9145ec3dd684bccfa3f81ec0a04ea16604cc6fd277a88ac1c73706b71a4cd6855fe8872985339538c712665b9b40000000000000075",


appendrawtransaction 01000000010232f... '[{"txid" : "65925b880fbb2517d7ad1aa32b87345e2f36d9bd456a45852f856b88f68a82f2","vout" : 0}]' '{"1DorxRWMcW4ptQzUoffvjKF9hYLtw83eQL8PWp":{"asset1":29}}'

01000000020232f455b03ceabe4834da1a82...

signrawtransaction 01000000020232f45... '[{"txid" : "65925b880fbb2517d7ad1aa32b87345e2f36d9bd456a45852f856b88f68a82f2","vout" : 0,"scriptPubKey":"76a9145ec3dd684bccfa3f81ec0a04ea16604cc6fd277a88ac1c73706b71a4cd6855fe8872985339538c712665b9b40000000000000075"}]' null SINGLE|ANYONECANPAY

{
    "hex" : "01000000020232f455b03ceabe4834da1a82...",
    "complete" : true
}

address C: offers asset1, asks asset2

preparelockunspentfrom 134euHtYkHRzoJ3CwatYNZJSz7sjjqQpJ6TaUg '{"asset1":29}'

{
    "txid" : "775b37f7c0d5a4ee52816e157b601b30e809a0e33986d5cd9e773bc46bded799",
    "vout" : 0
}

gettxout 775b37f7c0d5a4ee52816e157b601b30e809a0e33986d5cd9e773bc46bded799 0

    "scriptPubKey" : {
        "hex" : "76a9140f43900f6ee8226418e76b3cb29b412c68794b7888ac1c73706b719f3941ad4c763bab0d8b33452b65fa0a1d0000000000000075",

appendrawtransaction 01000000020232f... '[{"txid" : "775b37f7c0d5a4ee52816e157b601b30e809a0e33986d5cd9e773bc46bded799","vout" : 0,"scriptPubKey":"76a9140f43900f6ee8226418e76b3cb29b412c68794b7888ac1c73706b719f3941ad4c763bab0d8b33452b65fa0a1d0000000000000075"}]' '{"134euHtYkHRzoJ3CwatYNZJSz7sjjqQpJ6TaUg":{"asset2":180}}'

01000000030232f455b03ceabe4834da1a82...

signrawtransaction 01000000030232f455b03ceabe4834da1a82... '[{"txid" : "775b37f7c0d5a4ee52816e157b601b30e809a0e33986d5cd9e773bc46bded799","vout" : 0,"scriptPubKey":"76a9140f43900f6ee8226418e76b3cb29b412c68794b7888ac1c73706b719f3941ad4c763bab0d8b33452b65fa0a1d0000000000000075"}]' null SINGLE|ANYONECANPAY

{
    "hex" : "01000000030232f455b03cea...",
    "complete" : true
}

sendrawtransaction 01000000030232f455b03ceabe4834da1a82...

ad491d6456175317b35cbed7be10ce093c9fc61103af906a6e9c73f97d155b14
Unfortunately still getting errors: "16: ConnectInputs failed"

Address A - write to stream 'orders'
preparelockunspentfrom:["1EB9WL4hBruCyqcseiTLUhU7vYMTeUwAQfXjUn",0,true]

gettxout:["70cecee7539e814574d013d34c02096d86952c3a77c23915b5f085f4fc794acd",1,true]

createrawtransaction:[[{"txid":"70cecee7539e814574d013d34c02096d86952c3a77c23915b5f085f4fc794acd","vout":1,"scriptPubKey":"76a914617afcc6e808790b4cac2d2ffc8d67439c195c4f88ac"}],{},[],""]

appendrawdata:["0100000001cd4a79fcf485f0b51539c2773a2c95866d09024cd313d07445819e53e7cece700100000000ffffffff0000000000",{"for":"orders","key":"hello","data":"7b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d"}]

signrawtransaction:["0100000001cd4a79fcf485f0b51539c2773a2c95866d09024cd313d07445819e53e7cece700100000000ffffffff010000000000000000481473706b6527bbd8f193b5218d8b53c3d647deac40750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d00000000",[{"txid":"70cecee7539e814574d013d34c02096d86952c3a77c23915b5f085f4fc794acd","vout":1,"scriptPubKey":"76a914617afcc6e808790b4cac2d2ffc8d67439c195c4f88ac"}],["V9qLARz1sLrZBsEJsomtsSzLBESvrUT2pFrfKdaGVaH95nHZooHv55ib"],"SINGLE"]

Address B - start exchange
appendrawtransaction:["0100000001cd4a79fcf485f0b51539c2773a2c95866d09024cd313d07445819e53e7cece70010000006b483045022100e585cf24c5c80d60f14a2371d9b59950e6d95f2d720e44fcb6d5b5112854ee6c02203d852621130e2b4f2a32aee05e0dc90c22c874df66a7280b5a03d8728ffe4718032102913095c432913d16b3590fbccec01e9771da65bbd876bceae432972cef295f1fffffffff010000000000000000481473706b6527bbd8f193b5218d8b53c3d647deac40750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d00000000",[{"txid":"9929a91991fe12c4d353e930b9f2c70bc82ba8abd1a16c598cc3553ab4f19799","vout":0,"scriptPubKey":"76a914a958ce78706174805483f6630038bfe4ea455c5488ac1c73706b71af8c66fde7c8f86728b6d597950adaac320000000000000075"}],{"4LuZVSj2PWJCAccofZcpFchjxp96cqn4V5qymq":{"07c24814fbc0cf5252ac067f510c3ff0":29}},[],""]

signrawtransaction:["0100000002cd4a79fcf485f0b51539c2773a2c95866d09024cd313d07445819e53e7cece70010000006b483045022100e585cf24c5c80d60f14a2371d9b59950e6d95f2d720e44fcb6d5b5112854ee6c02203d852621130e2b4f2a32aee05e0dc90c22c874df66a7280b5a03d8728ffe4718032102913095c432913d16b3590fbccec01e9771da65bbd876bceae432972cef295f1fffffffff9997f1b43a55c38c596ca1d1aba82bc80bc7f2b930e953d3c412fe9119a929990000000000ffffffff020000000000000000481473706b6527bbd8f193b5218d8b53c3d647deac40750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d000000000000000035a9149ab2996eb0a91d04ad93fa3e50f35676d43034f2871c73706b71d6a44f963e6fae19ab179d3c020eec8e1d000000000000007500000000",[{"txid":"9929a91991fe12c4d353e930b9f2c70bc82ba8abd1a16c598cc3553ab4f19799","vout":0,"scriptPubKey":"76a914a958ce78706174805483f6630038bfe4ea455c5488ac1c73706b71af8c66fde7c8f86728b6d597950adaac320000000000000075"}],["VAW9P2aomY7Z5r14WE3JEL5MNZrwgiqaEj1YNPMLFN3Xz8Twjq2opifk"],"SINGLE|ANYONECANPAY"]

Address C - accept exchange
appendrawtransaction:["0100000002cd4a79fcf485f0b51539c2773a2c95866d09024cd313d07445819e53e7cece70010000006b483045022100e585cf24c5c80d60f14a2371d9b59950e6d95f2d720e44fcb6d5b5112854ee6c02203d852621130e2b4f2a32aee05e0dc90c22c874df66a7280b5a03d8728ffe4718032102913095c432913d16b3590fbccec01e9771da65bbd876bceae432972cef295f1fffffffff9997f1b43a55c38c596ca1d1aba82bc80bc7f2b930e953d3c412fe9119a92999000000006b483045022100cc43649c13c04289f58d2844a85f0ded419e98b7541ed807e6d6c0f24eefb2ec02204614fb16bd64dc4c52c86bb985425226a876d59ed41b99d3403da2c2b49773e2832103331ca982916ef2f7aa401e9b15a171ca6c9b84e4444fa82c9a8f31ff7966ae67ffffffff020000000000000000481473706b6527bbd8f193b5218d8b53c3d647deac40750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d000000000000000035a9149ab2996eb0a91d04ad93fa3e50f35676d43034f2871c73706b71d6a44f963e6fae19ab179d3c020eec8e1d000000000000007500000000",[{"txid":"3405038377b57d499317858991e0081be6feca2a6298edeaacbf89c40febb410","vout":1,"scriptPubKey":"76a91422de768a3fd97de63898a3e0a4953a58f7555f5188ac3473706b71af8c66fde7c8f86728b6d597950adaac3200000000000000d6a44f963e6fae19ab179d3c020eec8edd0000000000000075"}],{"15iLZQEkbEyVcpcytYtaSgfqLTcuEuxsWxihc5":{"0f7e401c396b92d3864b22a970fa322a":100,"07c24814fbc0cf5252ac067f510c3ff0":192}},[],""]

signrawtransaction:["0100000003cd4a79fcf485f0b51539c2773a2c95866d09024cd313d07445819e53e7cece70010000006b483045022100e585cf24c5c80d60f14a2371d9b59950e6d95f2d720e44fcb6d5b5112854ee6c02203d852621130e2b4f2a32aee05e0dc90c22c874df66a7280b5a03d8728ffe4718032102913095c432913d16b3590fbccec01e9771da65bbd876bceae432972cef295f1fffffffff9997f1b43a55c38c596ca1d1aba82bc80bc7f2b930e953d3c412fe9119a92999000000006b483045022100cc43649c13c04289f58d2844a85f0ded419e98b7541ed807e6d6c0f24eefb2ec02204614fb16bd64dc4c52c86bb985425226a876d59ed41b99d3403da2c2b49773e2832103331ca982916ef2f7aa401e9b15a171ca6c9b84e4444fa82c9a8f31ff7966ae67ffffffff10b4eb0fc489bfaceaed98622acafee61b08e09189851793497db577830305340100000000ffffffff030000000000000000481473706b6527bbd8f193b5218d8b53c3d647deac40750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d000000000000000035a9149ab2996eb0a91d04ad93fa3e50f35676d43034f2871c73706b71d6a44f963e6fae19ab179d3c020eec8e1d000000000000007500000000000000004f76a91422de768a3fd97de63898a3e0a4953a58f7555f5188ac3473706b71af8c66fde7c8f86728b6d597950adaac6400000000000000d6a44f963e6fae19ab179d3c020eec8ec0000000000000007500000000",[{"txid":"3405038377b57d499317858991e0081be6feca2a6298edeaacbf89c40febb410","vout":1,"scriptPubKey":"76a91422de768a3fd97de63898a3e0a4953a58f7555f5188ac3473706b71af8c66fde7c8f86728b6d597950adaac3200000000000000d6a44f963e6fae19ab179d3c020eec8edd0000000000000075"}],["VDCML5FPf3gcw87UkduRD88DknfBjBKEhPGMyRUGYhKfjsfCM4cAboq2"],"SINGLE|ANYONECANPAY"]

It fails here: sendrawtransaction:["0100000003cd4a79fcf485f0b51539c2773a2c95866d09024cd313d07445819e53e7cece70010000006b483045022100e585cf24c5c80d60f14a2371d9b59950e6d95f2d720e44fcb6d5b5112854ee6c02203d852621130e2b4f2a32aee05e0dc90c22c874df66a7280b5a03d8728ffe4718032102913095c432913d16b3590fbccec01e9771da65bbd876bceae432972cef295f1fffffffff9997f1b43a55c38c596ca1d1aba82bc80bc7f2b930e953d3c412fe9119a92999000000006b483045022100cc43649c13c04289f58d2844a85f0ded419e98b7541ed807e6d6c0f24eefb2ec02204614fb16bd64dc4c52c86bb985425226a876d59ed41b99d3403da2c2b49773e2832103331ca982916ef2f7aa401e9b15a171ca6c9b84e4444fa82c9a8f31ff7966ae67ffffffff10b4eb0fc489bfaceaed98622acafee61b08e09189851793497db57783030534010000006b483045022100e85fcf81b645b4a3bb6390529e376290ac8cdb6338b1bedbed985c24ce350cae02205b733f22c222785ea5e0a0000ccae9dea5162d1ea2653a8f620e291ecba604528321031243b3b435b382408d4546108f2dc63154866aaa060c8522f2d656a3dd4a642fffffffff030000000000000000481473706b6527bbd8f193b5218d8b53c3d647deac40750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d000000000000000035a9149ab2996eb0a91d04ad93fa3e50f35676d43034f2871c73706b71d6a44f963e6fae19ab179d3c020eec8e1d000000000000007500000000000000004f76a91422de768a3fd97de63898a3e0a4953a58f7555f5188ac3473706b71af8c66fde7c8f86728b6d597950adaac6400000000000000d6a44f963e6fae19ab179d3c020eec8ec0000000000000007500000000"]

If I don't add the stream write, the transaction goes through.
Sorry, but I don't see preparelockunspentfrom for addresses B and C. Where do you take txid/vout for appendrawtransaction from?

The most likely explanation for "ConnectInputs failed" error is: input was already spent in another transaction.

preparelockunspentfrom ensures you are using unspent input in createrawtransaction/appendrawtransaction
ok I found out the problem, for the stream write when signing the raw transaction, you can't use SINGLE it has to be SINGLE|ANYONECANPAY
You are right. My resulting transaction indeed has  SINGLE|ANYONECANPAY. Probably I lost ANYONECANPAY in my script at some stage.
...