Search code examples
cryptographyrpc

Bitcoin / DefiChain RPC rawTransaction


Hello i try to figure out how to encode normal rpc calls in an raw transaction. Till now my problem is that i dont know what the hex must contain as string. f.e.

rpc command:  "method: 'compositeswap' {'from':'MyAddress','tokenFrom':'MyToken1','amountFrom':'0.001','to':'Address','tokenTo':'Token2','maxPrice':'0.01'}"

There seems to be OP-Codes to exists like OP_DEFI_TX_COMPOSITE_SWAP how does the chain knows to execute an operation with params?

i tried to figure it out by trying to decode an actual transaction from the test wallet: from the part

 "scriptPubKey": {
        "asm": "OP_RETURN 446654786917a914721d5b1c58d38af7b6797b385b6ac291b002f88c870000e1f5050000000017a914721d5b1c58d38af7b6797b385b6ac291b002f88c870b0000000000000000c74e71050000000000",
        "hex": "6a4c50446654786917a914721d5b1c58d38af7b6797b385b6ac291b002f88c870000e1f5050000000017a914721d5b1c58d38af7b6797b385b6ac291b002f88c870b0000000000000000c74e71050000000000",
        "type": "nulldata"
      },

of

{
  "txid": "9a98d693d4c5107647131ee1bb7a5b0cce0fcdbe390c9609a71f4b71157e39dc",
  "hash": "a18a6fd4abf0ba1885febcf37a333b1d1f34b4de954f13538a6619b1d7b20042",
  "version": 4,
  "size": 309,
  "vsize": 228,
  "weight": 909,
  "locktime": 0,
  "vin": [
    {
      "txid": "9e1140197138ba5e247ab3b3f1f4881bf7be624a939073d9795242caf3634409",
      "vout": 1,
      "scriptSig": {
        "asm": "0014451be7ab94ccd7eff0a33ab8fe997a75c62eb7dd",
        "hex": "160014451be7ab94ccd7eff0a33ab8fe997a75c62eb7dd"
      },
      "txinwitness": [
        "30440220552d8aa4e129f566bfe083b780e1dcf67a3ca0176e07407912451371f597bc620220698c6ac483e021b78c7d7bf42e14f1c619618d1941cf12fec7cf8302ece6d3ae01",
        "03c7d2dbe5ee429de5d88e8594cda6ceb84268ebbf9d0b16b33664e999307f33e8"
      ],
      "sequence": 4294967295
    }
  ],
  "vout": [
    {
      "value": 0,
      "n": 0,
      "scriptPubKey": {
        "asm": "OP_RETURN 446654786917a914721d5b1c58d38af7b6797b385b6ac291b002f88c870000e1f5050000000017a914721d5b1c58d38af7b6797b385b6ac291b002f88c870b0000000000000000c74e71050000000000",
        "hex": "6a4c50446654786917a914721d5b1c58d38af7b6797b385b6ac291b002f88c870000e1f5050000000017a914721d5b1c58d38af7b6797b385b6ac291b002f88c870b0000000000000000c74e71050000000000",
        "type": "nulldata"
      },
      "tokenId": 0
    },
    {
      "value": 183.03901748,
      "n": 1,
      "scriptPubKey": {
        "asm": "OP_HASH160 721d5b1c58d38af7b6797b385b6ac291b002f88c OP_EQUAL",
        "hex": "a914721d5b1c58d38af7b6797b385b6ac291b002f88c87",
        "reqSigs": 1,
        "type": "scripthash",
        "addresses": [
          "tgfbETCK2kYyvsnHbS41v9aicQzAXLsz9B"
        ]
      },
      "tokenId": 0
    }
  ]
}

the

OP_RETURN 446654786917a914721d5b1c58d38af7b6797b385b6ac291b002f88c870000e1f5050000000017a914721d5b1c58d38af7b6797b385b6ac291b002f88c870b0000000000000000c74e71050000000000

cant be decoded sucessfully back into a string. does s.o know what kind of encoding it is? tried

bytess=bytes.fromhex("446654786917a914721d5b1c58d38af7b6797b385b6ac291b002f88c870000e1f5050000000017a914721d5b1c58d38af7b6797b385b6ac291b002f88c870b0000000000000000c74e71050000000000")
print(bytess.decode("latin-1"))

but only get

INFO (MainThread) 14.05.2022 22:02:39 DfTxi©r
INFO (MainThread) 14.05.2022 22:02:39 [
INFO (MainThread) 14.05.2022 22:02:39 XÓŠ÷¶y{8[j‘°øŒ‡  áõ    ©r

Solution

  • You can't just decode the string after the OP Code. It is a concatenation of several input parameters depending on the function. In your example

    6a4c50446654786917a914721d5b1c58d38af7b6797b385b6ac291b002f88c870000e1f5050000000017a914721d5b1c58d38af7b6797b385b6ac291b002f88c870b0000000000000000c74e71050000000000
    

    6a is OP_RETURN 4c50 is Number of bytes following (in HEX) 44665478 is "DfTx" in HEX 6917a914721d5b1c58d38af7b6797b385b6ac291b002f88c870000e1f5050000000017a914721d5b1c58d38af7b6797b385b6ac291b002f88c870b0000000000000000c74e71050000000000 = the parameters from your json. For your address you need Base58 (in this case) or Bech32 encoding to get the hex (some backward engineering necessary). The part with lots of zeros is the amount/maxPrice. The tokens are inserted by their IDs not the name.

    I am thinking about writing a guide. So far, I am afraid you have to do some backward engineering. Hope this helps a little bit