BitShares Forum

Main => Technical Support => Topic started by: monsterer on January 15, 2015, 08:40:49 am

Title: Automatically refunding transactions sent from unregistered accounts?
Post by: monsterer on January 15, 2015, 08:40:49 am
I can refund a transaction from a registered account by looking at the from_account field, which is their account name. An unregistered account has a stealth public key in place of the account name - so, how do I know where to send the refund?
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: xeroc on January 15, 2015, 10:10:16 am
every withdraw tx uses a balance_id (address) that holds funds to withdraw from ..
you can just send the funds there and the client should catch up that transaction ..
there's no need to 'identify' the unregistered account .. just refund to the address (balance_id) you received the money from ..
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: monsterer on January 15, 2015, 10:34:20 am
every withdraw tx uses a balance_id (address) that holds funds to withdraw from ..
you can just send the funds there and the client should catch up that transaction ..
there's no need to 'identify' the unregistered account .. just refund to the address (balance_id) you received the money from ..

Are there any docs on what all these fields mean? I had assumed balance_id was like a UTXO and that 'from_account' was the sender.

Take a look at part of this deposit:

Code: [Select]
{
          "type": "withdraw_op_type",
          "data": {
            "balance_id": "BTS6UCvWSmZAsSmhp5E4JRrTLqirBFu7Lfng",
            "amount": 10000,
            "claim_input_data": ""
          }
        },{
          "type": "withdraw_op_type",
          "data": {
            "balance_id": "BTSASw7dDN1u3VaSNw5SNYk1WNN9uKBWZoDU",
            "amount": 10000,
            "claim_input_data": ""
          }
        }

As you can see there are two balance_id's here, so which one do I pick? AFAIK, I can't send to more than one address in one transaction?
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: xeroc on January 15, 2015, 10:39:34 am
every withdraw tx uses a balance_id (address) that holds funds to withdraw from ..
you can just send the funds there and the client should catch up that transaction ..
there's no need to 'identify' the unregistered account .. just refund to the address (balance_id) you received the money from ..

Are there any docs on what all these fields mean? I had assumed balance_id was like a UTXO and that 'from_account' was the sender.

Take a look at part of this deposit:

Code: [Select]
{
          "type": "withdraw_op_type",
          "data": {
            "balance_id": "BTS6UCvWSmZAsSmhp5E4JRrTLqirBFu7Lfng",
            "amount": 10000,
            "claim_input_data": ""
          }
        },{
          "type": "withdraw_op_type",
          "data": {
            "balance_id": "BTSASw7dDN1u3VaSNw5SNYk1WNN9uKBWZoDU",
            "amount": 10000,
            "claim_input_data": ""
          }
        }

As you can see there are two balance_id's here, so which one do I pick? AFAIK, I can't send to more than one address in one transaction?

balance_id is indeed like a UTXO .. but in bitshares you don't have change-addresses .. so what you do is basically just substract the amount from the balance and leave the rest in the balance_id/address.

in your case the "output" is bigger as your balance_id so that your tx has to combine several "inputs"/utxo

the "output" of the transaction is called "deposit_op_type" and contains a "withdraw_condition" .. which is usually "withdraw_signature_type" .. so you can withdraw with a valid signature ..

as for the "claim_input_data" .. my guess is that this field is required for some specialized withdraw conditions such as the withdraw_by_password .. and contains a parameter of some kind
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: monsterer on January 15, 2015, 11:22:17 am
Here's the whole transaction for reference:

Code: [Select]
[
  "d888ac6e13fe60d9cdf74c5752b8a4a3f827f4a2",{
    "trx": {
      "expiration": "2015-01-14T09:46:35",
      "delegate_slate_id": null,
      "operations": [{
          "type": "deposit_op_type",
          "data": {
            "amount": 10000,
            "condition": {
              "asset_id": 0,
              "slate_id": 0,
              "type": "withdraw_signature_type",
              "data": {
                "owner": "BTS82mTkM4nZLBUs7FZBux3zDV4TvdsUcdYR",
                "memo": {
                  "one_time_key": "BTS7P2Ped3mpLeQfuiH8HE9Smoy6cHom1YNNzEqgDg5EmPFPLMDrn",
                  "encrypted_memo_data": "bd8f6702454b0e04284fedcbaf7e4859603a2df1c32f83909ac88e95fc5d13ffb1d71b2f92cec504ef3599c76bd1a7f59e13a535dbed9f96489c906ef131fec3"
                }
              }
            }
          }
        },{
          "type": "withdraw_op_type",
          "data": {
            "balance_id": "BTS6UCvWSmZAsSmhp5E4JRrTLqirBFu7Lfng",
            "amount": 10000,
            "claim_input_data": ""
          }
        },{
          "type": "withdraw_op_type",
          "data": {
            "balance_id": "BTSASw7dDN1u3VaSNw5SNYk1WNN9uKBWZoDU",
            "amount": 10000,
            "claim_input_data": ""
          }
        }
      ],
      "signatures": [
        "1f15c6e8076429d5c6a26eb314a2fa0aba1a0650f9af592abd52b25c9377383b4210f01c8c0c18ce1029d311822817dfcea9ceff1fee05f4ad31a5d62bdda6198a",
        "1f6868afae9b4f8d4d7566ec95d71760e695fdd8d232bf0ca3c12b2c764504ec35152a9df4cba00afe46089141bcf5064d28aac6aaf5e36ec41b90f606b0fe83bf"
      ]
    },
    "current_op_index": 3,
    "signed_keys": [],
    "validation_error": null,
    "provided_deposits": [[
        "BTSKdc8euepN6bZ32K5sdvvQQA7GD3udToN7",{
          "amount": 10000,
          "asset_id": 0
        }
      ]
    ],
    "deposits": [[
        0,{
          "amount": 10000,
          "asset_id": 0
        }
      ]
    ],
    "withdraws": [[
        0,{
          "amount": 20000,
          "asset_id": 0
        }
      ]
    ],
    "yield": [],
    "deltas": [[
        0,{
          "amount": 10000,
          "asset_id": 0
        }
      ],[
        1,{
          "amount": -10000,
          "asset_id": 0
        }
      ],[
        2,{
          "amount": -10000,
          "asset_id": 0
        }
      ]
    ],
    "required_fees": {
      "amount": 0,
      "asset_id": 0
    },
    "alt_fees_paid": {
      "amount": 0,
      "asset_id": 0
    },
    "balance": [[
        0,
        10000
      ]
    ],
    "net_delegate_votes": [],
    "chain_location": {
      "block_num": 1520903,
      "trx_num": 0
    }
  }
]

As you can see, balance_id only occurs in the operations array and there are two of them.

deposit_op_type contains a single owner field, is that what you are talking about?
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: xeroc on January 15, 2015, 12:01:35 pm
Here's the whole transaction for reference:

Code: [Select]
[
  "d888ac6e13fe60d9cdf74c5752b8a4a3f827f4a2",{
    "trx": {
      "expiration": "2015-01-14T09:46:35",
      "delegate_slate_id": null,
      "operations": [{
          "type": "deposit_op_type",
          "data": {
            "amount": 10000,
            "condition": {
              "asset_id": 0,
              "slate_id": 0,
              "type": "withdraw_signature_type",
              "data": {
                "owner": "BTS82mTkM4nZLBUs7FZBux3zDV4TvdsUcdYR",
                "memo": {
                  "one_time_key": "BTS7P2Ped3mpLeQfuiH8HE9Smoy6cHom1YNNzEqgDg5EmPFPLMDrn",
                  "encrypted_memo_data": "bd8f6702454b0e04284fedcbaf7e4859603a2df1c32f83909ac88e95fc5d13ffb1d71b2f92cec504ef3599c76bd1a7f59e13a535dbed9f96489c906ef131fec3"
                }
              }
            }
          }
        },{
          "type": "withdraw_op_type",
          "data": {
            "balance_id": "BTS6UCvWSmZAsSmhp5E4JRrTLqirBFu7Lfng",
            "amount": 10000,
            "claim_input_data": ""
          }
        },{
          "type": "withdraw_op_type",
          "data": {
            "balance_id": "BTSASw7dDN1u3VaSNw5SNYk1WNN9uKBWZoDU",
            "amount": 10000,
            "claim_input_data": ""
          }
        }
      ],
      "signatures": [
        "1f15c6e8076429d5c6a26eb314a2fa0aba1a0650f9af592abd52b25c9377383b4210f01c8c0c18ce1029d311822817dfcea9ceff1fee05f4ad31a5d62bdda6198a",
        "1f6868afae9b4f8d4d7566ec95d71760e695fdd8d232bf0ca3c12b2c764504ec35152a9df4cba00afe46089141bcf5064d28aac6aaf5e36ec41b90f606b0fe83bf"
      ]
    },
    "current_op_index": 3,
    "signed_keys": [],
    "validation_error": null,
    "provided_deposits": [[
        "BTSKdc8euepN6bZ32K5sdvvQQA7GD3udToN7",{
          "amount": 10000,
          "asset_id": 0
        }
      ]
    ],
    "deposits": [[
        0,{
          "amount": 10000,
          "asset_id": 0
        }
      ]
    ],
    "withdraws": [[
        0,{
          "amount": 20000,
          "asset_id": 0
        }
      ]
    ],
    "yield": [],
    "deltas": [[
        0,{
          "amount": 10000,
          "asset_id": 0
        }
      ],[
        1,{
          "amount": -10000,
          "asset_id": 0
        }
      ],[
        2,{
          "amount": -10000,
          "asset_id": 0
        }
      ]
    ],
    "required_fees": {
      "amount": 0,
      "asset_id": 0
    },
    "alt_fees_paid": {
      "amount": 0,
      "asset_id": 0
    },
    "balance": [[
        0,
        10000
      ]
    ],
    "net_delegate_votes": [],
    "chain_location": {
      "block_num": 1520903,
      "trx_num": 0
    }
  }
]

As you can see, balance_id only occurs in the operations array and there are two of them.

deposit_op_type contains a single owner field, is that what you are talking about?


The owner is the receiver (at least one address that was derived from the registered account name) .. if you were the receiver you could figure out if that was your address (read: if you could derive the private key for it) by using the date in the memo (the "one_time_key" - which is a public key).
So: owner -> receiver of the funds
you can also see this as the owner is in the "deposit_op_type" operation and the withdraw condition says "withdraw_signature_type" .. so .. however holds the private key to "the owner" literally owns the shares/coins/assets

The "withdraw_op_type" operations are used to address the "inputs" of the transaction .. those are the operations that have to be "signed" (see signatures - there are two of them) ..
those balance_ids hold transactions that can be redeemed using signatures (so have been sent there with transactions with conditions "withdraw_signature_type")

To test this:
Just take one of your outgoing transactions .. figure out the balance_id and send some funds to that address using wallet_transfer_asset_to_address (or so) ..
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: monsterer on January 15, 2015, 03:09:16 pm
Thanks for the detail.

I'm still not sure if any of that helps me to figure out how to refund a deposit from an unregistered account in a single transaction, though?
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: xeroc on January 15, 2015, 05:00:13 pm
Just send it back to one of the balanceids or addresses in the withdraw operation ... give it a try .. should work nicely
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: toast on January 15, 2015, 05:17:53 pm
Get the balance record from the balance id:  get_balance BTS6UCvWSmZAsSmhp5E4JRrTLqirBFu7Lfng
{
  "condition": {
    "asset_id": 0,
    "slate_id": 0,
    "type": "withdraw_signature_type",
    "data": {
      "owner": "BTSKrYCvm4RcLNqekPiTXHzcrVkxejwAdhfL",
      "memo": null
    }
  },
  "balance": 0,
  "restricted_owner": null,
  "snapshot_info": null,
  "deposit_date": "2015-01-13T08:42:00",
  "last_update": "2015-01-14T08:46:30"
}


Transfer to the address in the withdraw condition

wallet_transfer_to_address blah blah blah BTSKrYCvm4RcLNqekPiTXHzcrVkxejwAdhfL
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: xeroc on January 15, 2015, 05:35:12 pm
Oh ... so the balanceids is sth. else ... how is the balance id derived? Would sending funds to the balance id also work?
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: toast on January 15, 2015, 05:52:30 pm
Oh ... so the balanceids is sth. else ... how is the balance id derived? Would sending funds to the balance id also work?

Balance ID is a hash of the withdraw condition. If you sent to it, it would be unrecoverable. Maybe we ought to check that there's not a balance ID equal to the "address" you are sending to to prevent it.
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: monsterer on January 15, 2015, 06:03:02 pm
Get the balance record from the balance id:  get_balance BTS6UCvWSmZAsSmhp5E4JRrTLqirBFu7Lfng

But there are multiple balance id's per transaction? In my case I have two of them.

Quote
Transfer to the address in the withdraw condition

Is this not the same as the owner field I have inside the main transaction?

Code: [Select]
"condition": {
              "asset_id": 0,
              "slate_id": 0,
              "type": "withdraw_signature_type",
              "data": {
                "owner": "BTS82mTkM4nZLBUs7FZBux3zDV4TvdsUcdYR",
                "memo": {
                  "one_time_key": "BTS7P2Ped3mpLeQfuiH8HE9Smoy6cHom1YNNzEqgDg5EmPFPLMDrn",
                  "encrypted_memo_data": "bd8f6702454b0e04284fedcbaf7e4859603a2df1c32f83909ac88e95fc5d13ffb1d71b2f92cec504ef3599c76bd1a7f59e13a535dbed9f96489c906ef131fec3"
                }
              }
            }

?
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: toast on January 15, 2015, 06:06:29 pm
Get the balance record from the balance id:  get_balance BTS6UCvWSmZAsSmhp5E4JRrTLqirBFu7Lfng

But there are multiple balance id's per transaction? In my case I have two of them.

Quote
Transfer to the address in the withdraw condition

Is this not the same as the owner field I have inside the main transaction?

Code: [Select]
"condition": {
              "asset_id": 0,
              "slate_id": 0,
              "type": "withdraw_signature_type",
              "data": {
                "owner": "BTS82mTkM4nZLBUs7FZBux3zDV4TvdsUcdYR",
                "memo": {
                  "one_time_key": "BTS7P2Ped3mpLeQfuiH8HE9Smoy6cHom1YNNzEqgDg5EmPFPLMDrn",
                  "encrypted_memo_data": "bd8f6702454b0e04284fedcbaf7e4859603a2df1c32f83909ac88e95fc5d13ffb1d71b2f92cec504ef3599c76bd1a7f59e13a535dbed9f96489c906ef131fec3"
                }
              }
            }

?

If there are multiple balances that just means someone merged two inputs. Do you want to refund the exact amount to each input? I'd just pick one arbitrarily since you know they have access to both, like how we credited AGS.

The owner shown in the the raw transaction is in the DEPOSITED balance - you wouldn't be refunding anything but just sending it to yourself.
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: xeroc on January 15, 2015, 06:07:52 pm
Oh ... so the balanceids is sth. else ... how is the balance id derived? Would sending funds to the balance id also work?

Balance ID is a hash of the withdraw condition. If you sent to it, it would be unrecoverable. Maybe we ought to check that there's not a balance ID equal to the "address" you are sending to to prevent it.
Learnd sth ... thx for the insights
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: monsterer on January 15, 2015, 06:54:46 pm
Do you want to refund the exact amount to each input? I'd just pick one arbitrarily since you know they have access to both, like how we credited AGS.

No, all I want is for the transaction to be fully refunded to the sender account - looks like I can do this now, great, thanks! :)
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: monsterer on January 16, 2015, 08:06:38 am
toast, I don't suppose you have a code reference for how you do sharedrops this way? I'm having trouble getting it to work - the funds are sent ok, but never arrive in the recipient's wallet.

psudocode:

Code: [Select]
BitsharesTransaction t = m_bitshares.BlockchainGetTransaction(depositId);

// get the sender's address from the balance id, from the first op of type withdraw_op_type
BitsharesOperation op = t.trx.operations.First(o => o.type == BitsharesTransactionOp.withdraw_op_type);

BitsharesBalanceRecord balance = m_bitshares.GetBalance(op.data.balance_id);
string senderAddress = balance.condition.data.owner;

response = m_bitshares.WalletTransferToAddress(amount, m_asset.symbol, m_bitsharesAccount, senderAddress, memo);
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: xeroc on January 16, 2015, 08:14:35 am
I noticed that the transaction history does not show transactions that are send to an address directly instead of a TITAN address ... not sure if this is fixed.
You could check this by comparing your "balance" before and after your "refund"
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: monsterer on January 16, 2015, 09:02:07 am
I noticed that the transaction history does not show transactions that are send to an address directly instead of a TITAN address ... not sure if this is fixed.
You could check this by comparing your "balance" before and after your "refund"

Oh, right you are! Thank you! Yes, ok that's great - so this is working, but its just not shown in the client. I guess this is a bug to be fixed in the client?
Title: Re: Automatically refunding transactions sent from unregistered accounts?
Post by: xeroc on January 16, 2015, 09:53:42 am
I posted a github issue already about this .. gonna reopen it
https://github.com/BitShares/bitshares/issues/1080

edit: here's the open ticket: https://github.com/BitShares/bitshares/issues/1153