ビットコイン・インスティテュート

生のトランザクションを扱うRPC

1件のメッセージ BitcoinTalk ギャビン・アンドレセン 2012年6月14日 — 2012年6月14日

bitcoin-developmentメーリングリストからの転載:

昨日、トランザクションを「生の」形で直接扱う機能を実装するプルリクエストを出した。APIについての意見と、テストして壊してみる手伝いを求めている。

設計の文書:https://gist.github.com/2839617
プルリクエスト:https://github.com/bitcoin/bitcoin/pull/1456
テストの計画:https://secure.bettermeans.com/projects/4180/wiki/Raw_Transaction_RPC_Test_Plan

コマンドラインでこのAPIをいじってみて、抽象化の度合いと、既存のRPCコマンドとのかみ合い方に、かなり満足している。たとえば、createrawtxは、出力の指定の仕方がsendmanyとまったく同じだ。

新しいメソッドの要はsignrawtxだ。生のトランザクションを受け取り、署名できる入力に全部署名して、署名の付いた同じ生のトランザクションを返す。典型的な使い方は、次のようになる。

資金がマルチシグのトランザクションの出力に入っていて、署名を集めてそれを使うときが来た。

前提:そのマルチシグのトランザクションの[txid, outputNumber, amount]が分かっている。

createrawtxで、資金を使うための生のトランザクションを作る。
signrawtxで、自分の署名を足す(必要なら、先にウォレットのロックを解く)。
トランザクションをほかの人に渡して、署名してもらう。
自分か相手が、sendrawtxでトランザクションをネットワークに送る。

新しい技術を真っ先に試す人か筋金入りの技術好きを除けば、このRPCのメソッドをコマンドラインで呼んでこの手順を踏む人がいるとは思わない。本来は、bitcoindの上にサービスを書く人のためのものだ。サービスの側は、適切なトランザクション手数料を忘れずに付けること。そうしないと、sendrawtxはたぶん失敗する。

何度か聞かれたことがある。signrawtxは、なぜBIP 0010(https://en.bitcoin.it/wiki/BIP_0010)のトランザクションの形式を扱わないのか、と。

生のトランザクションでBIP 10の形式を読み書きすることも考えた。しかし、BIP 10の形式の読み書きは、もっと上の層でやるべきで、土台のRPCでやることではないと判断した。だから「生のトランザクション」は、16進数にしてJSONの文字列に入れるだけだ。その変換は、すでに書かれてデバッグも済んだコードが2、3行あれば済む。


新しいRPCの全部について、ヘルプの出力と使用例を載せる。

listunspent [minconf=1] [maxconf=999999]
Returns array of unspent transaction outputs
with between minconf and maxconf (inclusive) confirmations.
Returns an array of 4-element arrays, each of which is:
[transaction id, output, amount, confirmations]

E.g:  listunspent 1 2
Returns:
[
    [
        "2881b33a8c0bbdb45b0a65b36aa6611a05201e316ea3ad718762d48ef9588fb3",
        0,
        40.00000000,
        2
    ],
    [
        "894a0fc535c7b49f434ceb633d8555ea24c8f9775144efb42da85b853280bcd7",
        0,
        50.00000000,
        1
    ]
]

getrawtx <txid>
Returns hexadecimal-encoded, serialized transaction data
for <txid>. Returns an error if <txid> is unknown.

E.g.: getrawtx fce46ea2448820f7bb8091b5f5e3fd75b7b267e60b9a22af88a9eeabfb084233
Returns:
01000000016d40da062b6a0edcaf643b6e25b943baf103941589d287e39d6f425d84ae8b1c000000004847304402203fb648ff8381d8961e66ef61ab88afe52826a5179b8a7312742c8d93785ca56302204240ea12de1211fffab49686f13ca0e78011d1985765be6e6aa8e747852f897d01ffffffff0100f2052a0100000017a914f96e358e80e8b3660256b211a23ce3377d2f9cb18700000000

createrawtx [["txid",n],...] {address:amount,...}
Create a transaction spending given inputs
(array of (hex transaction id, output number) pairs),
sending to given address(es).
Returns the same information as gettransaction, plus an
extra "rawtx" key with the hex-encoded transaction.
Note that the transaction's inputs are not signed, and
it is not stored in the wallet or transmitted to the network.

E.g.: createrawtx '[ ["fce46ea2448820f7bb8091b5f5e3fd75b7b267e60b9a22af88a9eeabfb084233",0] ]' '{"mqYmZSQQuAWNQcdwBrDwmtTXg2TLNz748L":50}'
Returns:
{
    "version" : 1,
    "locktime" : 0,
    "size" : 85,
    "vin" : [
        {
            "prevout" : {
                "hash" : "fce46ea2448820f7bb8091b5f5e3fd75b7b267e60b9a22af88a9eeabfb084233",
                "n" : 0
            },
            "scriptSig" : "",
            "sequence" : 4294967295
        }
    ],
    "vout" : [
        {
            "value" : 50.00000000,
            "scriptPubKey" : "OP_DUP OP_HASH160 6e0920fc26383dc7e6101bc417cf87169d0cedbd OP_EQUALVERIFY OP_CHECKSIG"
        }
    ],
    "rawtx" : "0100000001334208fbabeea988af229a0be667b2b775fde3f5b59180bbf7208844a26ee4fc0000000000ffffffff0100f2052a010000001976a9146e0920fc26383dc7e6101bc417cf87169d0cedbd88ac00000000"
}

signrawtx <hex string> [<prevtx1>,<prevtx2>...]
Sign inputs for raw transaction (serialized, hex-encoded).
Second argument is an array of raw previous transactions that
this transaction depends on but are not yet in the blockchain.
Returns json object with keys:
  rawtx : raw transaction with signature(s) (hex-encoded string)
  complete : 1 if transaction has a complete set of signature (0 if not)

E.g.: signrawtx "0100000001334208fbabeea988af229a0be667b2b775fde3f5b59180bbf7208844a26ee4fc0000000000ffffffff0100f2052a010000001976a9146e0920fc26383dc7e6101bc417cf87169d0cedbd88ac00000000" '["01000000016d40da062b6a0edcaf643b6e25b943baf103941589d287e39d6f425d84ae8b1c000000004847304402203fb648ff8381d8961e66ef61ab88afe52826a5179b8a7312742c8d93785ca56302204240ea12de1211fffab49686f13ca0e78011d1985765be6e6aa8e747852f897d01ffffffff0100f2052a0100000017a914f96e358e80e8b3660256b211a23ce3377d2f9cb18700000000"]'
Returns:
{
    "rawtx" : "0100000001334208fbabeea988af229a0be667b2b775fde3f5b59180bbf7208844a26ee4fc000000009100473044022007f3ba1b8bdc156f2340ef1222eb287c3f5481a8078a8dad43aa09fd289ba19002201cc72e97406d546dc918159978dc78aee8215a6418375956665ee44e6eacc1150147522102894ca6e7a6483d0f8fa6110c77c431035e8d462e3a932255d9dda65e8fada55c2103c556ef01e89a07ee9ba61581658fa007bf442232daed8b465c47c278550d3dab52aeffffffff0100f2052a010000001976a9146e0920fc26383dc7e6101bc417cf87169d0cedbd88ac00000000",
    "complete" : false
}

sendrawtx <hex string>
Submits raw transaction (serialized, hex-encoded) to local node and network.
E.g.: sendrawtx 0100000001334208fbabeea988af229a0be667b2b775fde3f5b59180bbf7208844a26ee4fc000000009100473044022007f3ba1b8bdc156f2340ef1222eb287c3f5481a8078a8dad43aa09fd289ba19002201cc72e97406d546dc918159978dc78aee8215a6418375956665ee44e6eacc1150147522102894ca6e7a6483d0f8fa6110c77c431035e8d462e3a932255d9dda65e8fada55c2103c556ef01e89a07ee9ba61581658fa007bf442232daed8b465c47c278550d3dab52aeffffffff0100f2052a010000001976a9146e0920fc26383dc7e6101bc417cf87169d0cedbd88ac00000000
Returns:
error: {"code":-22,"message":"TX rejected"}

(必要な署名がそろっていないので拒まれた。受け入れられていれば、トランザクションIDが返る)