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
Thanks!

Unfortunately that's not an option in terms of workflow, as Address A and Address B are on one node and Address C can be on a completely different node.
Is there any possibility of starting the workflow on the node with address C?
Your other option is to build the exchange up manually, using createrawtransaction and appendrawtransaction and signrawtransaction with the final parameter SINGLE|ANYONECANPAY - that last parameter ensures that the signature for an input only covers the corresponding position of output.

You'll get an error from decoderawexchange when doing this but you can use decoderawtransaction with a more manual process.

So the flow would be:

Address A: preparelockunspentfrom (0) + createrawtransaction (stream item only) + signrawtransaction (SINGLE|ANYONECANPAY)
Address B: preparelockunspentfrom (offer of exchange) + appendrawtransaction (ask of exchange back to same address) + signrawtransaction (SINGLE|ANYONECANPAY)
Address C: preparelockunspentfrom (offer of exchange) + appendrawtransaction (ask of exchange back to same address) + signrawtransaction (SINGLE|ANYONECANPAY)

Then anyone can send with sendrawtransaction.

Note that you should pass null for the 2nd and 3rd parameters of signrawtransaction.
I tried that and when trying to append the output for address C, I get the following message "Cannot append outputs without violating existing signatures", does it matter if Address B is a multi-sig address
This will happen if you don't call signrawtransaction with the SINGLE|ANYONECANPAY value for the sighashtype parameter. If you like you can post a transcript of what you did so we can take a look.
The Address B and Address C, keys are managed outside the node wallet.

Write to stream Address A:
preparelockunspentfrom:["1JzsAxnoVJPqLqQaPPmK4DytQCGoz8QcpVxv7n",0,true]

gettxout:["e6b35fe6fb0843199fa4bd63be5cd2ecafe9c5a8328cd8ab2f2851bf94a58860",1,true]

createrawtransaction:[[{"txid":"e6b35fe6fb0843199fa4bd63be5cd2ecafe9c5a8328cd8ab2f2851bf94a58860","vout":1,"scriptPubKey":"76a9148529d799ba14ba4b89985cd60c81d7bd53bbe15e88ac"}],{},[],""]

appendrawdata:["01000000016088a594bf51282fabd88c32a8c5e9afecd25cbe63bda49f194308fbe65fb3e60100000000ffffffff0000000000",{"for":"orders","key":"hello","data":"7b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d"}]

signrawtransaction:["01000000016088a594bf51282fabd88c32a8c5e9afecd25cbe63bda49f194308fbe65fb3e60100000000ffffffff010000000000000000481473706b65521083c2a9a7958fed5e524f35652ec9750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d00000000",[{"txid":"e6b35fe6fb0843199fa4bd63be5cd2ecafe9c5a8328cd8ab2f2851bf94a58860","vout":1,"scriptPubKey":"76a9148529d799ba14ba4b89985cd60c81d7bd53bbe15e88ac"}],["VEJzbqZ2QqRa7es2MSLe5baARaonyXhdGDvJTwFnnde2P3KTpmXJHiKp"],"SINGLE|ANYONECANPAY"]

Address B inputs (input is from a single signature address) and outputs (output is to a multisig address):
appendrawtransaction:["01000000016088a594bf51282fabd88c32a8c5e9afecd25cbe63bda49f194308fbe65fb3e6010000006a47304402202c4304115ea5545d1e6dbf3f73dd31775054885c300fb161f251cf50c98e775702202ed68e556e40465eb943e7ff0e2842fa833470bccd741d38ecb6e4ce214c428c8321022fdde223db2392c850cc8f71635d058738832d551cc7b34e52ac0f2411661ef3ffffffff010000000000000000481473706b65521083c2a9a7958fed5e524f35652ec9750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d00000000",[{"txid":"ab25c17b48c3bfb4ec43198db7a9294fd80599e25f0554694ee32358ab52671f","vout":0,"scriptPubKey":"76a9141d732ae9ea768ddf1f344f0cc1c9fe769fe62f3f88ac1c73706b71057e0936a8915918b7a501148a642f6e320000000000000075"}],{"4P44R2m3KUcdSVvgTeFmbFqtKXzB4NV82UuqC3":{"07c24814fbc0cf5252ac067f510c3ff0":29}},[],"sign"]

appendrawdata:["01000000026088a594bf51282fabd88c32a8c5e9afecd25cbe63bda49f194308fbe65fb3e6010000006b483045022100ea8e656fe010bd8fdef7569d538510259cc2904c84f320813ab7e0d7d9d0da640220109adfbb0c96320fd2dd3fffc6f44b0b4c909b752b48a7df8d919bbd94ffa1b90121022fdde223db2392c850cc8f71635d058738832d551cc7b34e52ac0f2411661ef3ffffffff1f6752ab5823e34e6954055fe29905d84f29a9b78d1943ecb4bfc3487bc125ab0000000000ffffffff020000000000000000481473706b65521083c2a9a7958fed5e524f35652ec9750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d000000000000000035a914aa9464cebf324262c7691b57639a8c0bdc0227ae871c73706b71579351ba4d25931fccfab029bd03828f1d000000000000007500000000","7b226d657472696373223a7b226d6574726963223a226f7574222c2266726f6d5f61646472657373223a22313479726852774679334e44364b585577617037593853635044796373354871755a744b7175222c22746f5f61646472657373223a2231614c7a467256667471794b584a784741794e3947464e7871396e596754466a436f426f6e33222c2261737365745f6964223a223037633234383134666263306366353235326163303637663531306333666630222c22746f6b656e5f6964223a223066376534303163333936623932643338363462323261393730666133323261222c22746f6b656e5f616d74223a35307d7d"]

signrawtransaction:["01000000026088a594bf51282fabd88c32a8c5e9afecd25cbe63bda49f194308fbe65fb3e6010000006b483045022100ea8e656fe010bd8fdef7569d538510259cc2904c84f320813ab7e0d7d9d0da640220109adfbb0c96320fd2dd3fffc6f44b0b4c909b752b48a7df8d919bbd94ffa1b90121022fdde223db2392c850cc8f71635d058738832d551cc7b34e52ac0f2411661ef3ffffffff1f6752ab5823e34e6954055fe29905d84f29a9b78d1943ecb4bfc3487bc125ab0000000000ffffffff030000000000000000481473706b65521083c2a9a7958fed5e524f35652ec9750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d000000000000000035a914aa9464cebf324262c7691b57639a8c0bdc0227ae871c73706b71579351ba4d25931fccfab029bd03828f1d00000000000000750000000000000000f86a4cf57b226d657472696373223a7b226d6574726963223a226f7574222c2266726f6d5f61646472657373223a22313479726852774679334e44364b585577617037593853635044796373354871755a744b7175222c22746f5f61646472657373223a2231614c7a467256667471794b584a784741794e3947464e7871396e596754466a436f426f6e33222c2261737365745f6964223a223037633234383134666263306366353235326163303637663531306333666630222c22746f6b656e5f6964223a223066376534303163333936623932643338363462323261393730666133323261222c22746f6b656e5f616d74223a35307d7d00000000",[{"txid":"ab25c17b48c3bfb4ec43198db7a9294fd80599e25f0554694ee32358ab52671f","vout":0,"scriptPubKey":"76a9141d732ae9ea768ddf1f344f0cc1c9fe769fe62f3f88ac1c73706b71057e0936a8915918b7a501148a642f6e320000000000000075"}],["VCgs8QuhcogKTSY4bZesnAK6kQmmp2DLwkZ9kkY3QMfKjVdbZLuueFY7"],"SINGLE|ANYONECANPAY"]

Address C adding their inputs and outputs:
appendrawtransaction:["01000000026088a594bf51282fabd88c32a8c5e9afecd25cbe63bda49f194308fbe65fb3e6010000006b483045022100ea8e656fe010bd8fdef7569d538510259cc2904c84f320813ab7e0d7d9d0da640220109adfbb0c96320fd2dd3fffc6f44b0b4c909b752b48a7df8d919bbd94ffa1b90121022fdde223db2392c850cc8f71635d058738832d551cc7b34e52ac0f2411661ef3ffffffff1f6752ab5823e34e6954055fe29905d84f29a9b78d1943ecb4bfc3487bc125ab000000006b483045022100ffdc507ffb999a5afa9e84e0e6ebbf8fb9b211a9f2c69f4e1b202d8bba10bb8802203c2a0b77d37c9714791efc552c572e595f681e29ec77c704b60fca29c42364db8321024f5e95d9c2642e6f60a422442b3d98c8d6e49674ac50a1e871377248e5e25105ffffffff030000000000000000481473706b65521083c2a9a7958fed5e524f35652ec9750973706b6b68656c6c6f756a257b22736f6d655f64617461223a312c22736f6d655f6d6f7265223a2274657374696e67227d000000000000000035a914aa9464cebf324262c7691b57639a8c0bdc0227ae871c73706b71579351ba4d25931fccfab029bd03828f1d00000000000000750000000000000000f86a4cf57b226d657472696373223a7b226d6574726963223a226f7574222c2266726f6d5f61646472657373223a22313479726852774679334e44364b585577617037593853635044796373354871755a744b7175222c22746f5f61646472657373223a2231614c7a467256667471794b584a784741794e3947464e7871396e596754466a436f426f6e33222c2261737365745f6964223a223037633234383134666263306366353235326163303637663531306333666630222c22746f6b656e5f6964223a223066376534303163333936623932643338363462323261393730666133323261222c22746f6b656e5f616d74223a35307d7d00000000",[{"txid":"1cd0be822815b9f18f91d81808deba9877e0d33150e11d31eac5a3c9eb009397","vout":1,"scriptPubKey":"76a914f6b6da427b6060c305c1a0bd222f45b361786c9a88ac3473706b71057e0936a8915918b7a501148a642f6e8200000000000000579351ba4d25931fccfab029bd03828f870000000000000075"}],{"1aLzFrVftqyKXJxGAyN9GFNxq9nYgTFjCoBon3":{"0f7e401c396b92d3864b22a970fa322a":180,"07c24814fbc0cf5252ac067f510c3ff0":106}},[],"sign"]

this is where it errors out with "Cannot append outputs without violating existing signatures"
First FYI you can save all the appendrawdata steps by passing the data as a single array item in the 'data' parameter of createrawtransaction or appendrawtransaction.

Anyway we'll run through your scenario here and let you know.
OK, I think we see the problem (we didn't run it yet). The second appendrawdata is adding an output without an input and this is interfering with the way in which corresponding inputs and outputs are signed. Either remove this step (it wasn't in your original problem description) or ensure that a corresponding input is added (generated by preparelockunspent) whose private key is used for the next signrawtransaction.
I removed the step and I still get "Cannot append outputs without violating existing signatures"
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.
...