์ด๋ฒˆ์—” ํŠธ๋ž™์žญ์…˜ ์ž…๋ ฅ์— ๋Œ€ํ•ด ์•Œ์•„๋ณด์ž.

Inputs

010000000181379011acb80925dfe69b3def355fe914bd1d96a3f5f71bf8303c6a989c7d10000000
06b483045022100ed81ff192e75a3fd2304004dcadb746fa5e24c5031ccfcf2132060277457c98f02
207a986d955c6e0cb35d446a89d3f56100f4d7f67801c31967743a9c8e10615bed01210349fc4e631
e3624a545de3f89f5d8684c7b8138bd94bdd531d2e213bf016b278afeffffff02a135ef0100000000
1976a914bc3b654dca7e56b04dca18f2566cdaf02e8d9ada88ac99c39800000000001976a9141c4bc
762dd5423e332166702cb75f40df79fea1288ac19430600

Inputs: 0181379011acb80925dfe69b3def355fe914bd1d96a3f5f71bf8303c6a989c7d10000000
06b483045022100ed81ff192e75a3fd2304004dcadb746fa5e24c5031ccfcf2132060277457c98f02
207a986d955c6e0cb35d446a89d3f56100f4d7f67801c31967743a9c8e10615bed01210349fc4e631
e3624a545de3f89f5d8684c7b8138bd94bdd531d2e213bf016b278afeffffff
  - Input counts(Varint): 01
  - Previous Tx ID(hash256): 81379011acb80925dfe69b3def355fe914bd1d96a3f5f71bf8303c6a989c7d1
  - Previous Tx index: 0000000
0
  - ScriptSig(Varint): 3045022100ed81ff192e75a3fd2304004dcadb746fa5e24c5031ccfcf2132060277457c98f02
207a986d955c6e0cb35d446a89d3f56100f4d7f67801c31967743a9c8e10615bed01210349fc4e631
e3624a545de3f89f5d8684c7b8138bd94bdd531d2e213bf016b278a
  - Sequence: feffffff

  • ๊ฑฐ๋ž˜๋ผ๋Š” ๊ฒƒ์€ ๋ฌด์—‡์ผ๊นŒ?
  • ์œ„์™€ ๊ฐ™์ด ์ƒ๊ฐํ•˜๋Š” ๊ฒƒ์ด ์ผ๋ฐ˜์ ์ผ ๊ฒƒ์ด๋‹ค. ํ•˜์ง€๋งŒ ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์ƒ๊ฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

  • A์—์„œ B๋กœ ๊ฐ€์น˜๊ฐ€ ์ด๋™ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ƒ๊ฐํ•ด๋ณด์ž.
  • A๊ฐ€ ๊ฐ€์ง„ ์ž์‚ฐ์ด ๋ถ„ํ•ด๋˜์–ด ๋‹ค๋ฅธ ๊ณณ์œผ๋กœ ๋„˜์–ด๊ฐ„๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๋ฉด ํ•œ๋ฒˆ์˜ ๊ฑฐ๋ž˜์—์„œ Input์ด ์—ฌ๋Ÿฌ๊ฐœ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค๋Š” ๋ง์ธ๊ฐ€? ๊ทธ๋ ‡๋‹ค.
  • Inputs์˜ ๊ฐœ์ˆ˜๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด Varint ํ˜•์‹์„ ์‚ฌ์šฉํ•œ๋‹ค.

Previous Tx ID

  • ์ž…๋ ฅ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๋น„ํŠธ์ฝ”์ธ์€ ๋ถ„๋ช… ์ด์ „ ํŠธ๋ž™์žญ์…˜์—์„œ์˜ ๊ฒฐ๊ณผ๋กœ ๋ถ€ํ„ฐ ์˜ฌ ๊ฒƒ์ด๋‹ค.
    • ์ด๊ฑด ์ž๋ช…ํ•œ๋ฐ, ๋น„ํŠธ์ฝ”์ธ์„ ๊ธˆ๋ฉ์–ด๋ฆฌ๋กœ ๋น„์œ ํ•ด์„œ ์ƒ๊ฐํ•ด๋ณด์ž.
    • ์ž๋ฅธ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๊ฑฐ๋ž˜๋ผ๋Š” ๊ฒƒ์€ ์‹œ๊ฐ„์ถ•์˜ ํ๋ฆ„์— ๋”ฐ๋ผ ์ˆœ์ฐจ์ ์œผ๋กœ ์ง„ํ–‰๋  ์ˆ˜ ๋ฐ–์— ์—†๋‹ค.
    • ์ฆ‰, ๋™์‹œ์— ํ•˜๋‚˜์˜ ๊ธˆ๋ฉ์ด๋กœ ๋‘๊ฐœ์˜ ๊ฑฐ๋ž˜๋ฅผ ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
      • ์ด๊ฒƒ์„ ์ด์ค‘ ์ง€๋ถˆ ๋ฌธ์ œ๋ผ ํ•œ๋‹ค.
      • ๋””์ง€ํ„ธ ํ™˜๊ฒฝ์—์„œ ์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์–ด๋ ค์›Œ ๋””์ง€ํ„ธ ํ™”ํ๊ฐ€ ๋‚˜์˜ค์ง€ ์•Š์•˜์—ˆ๋‹ค.
      • ๋น„ํŠธ์ฝ”์ธ์ด ์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค. ์ด๋Š” ์•„์ง ๋‚˜์˜ค์ง€ ์•Š์•˜๋‹ค.
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์—, ์ด์ „ ํŠธ๋žœ์žญ์…˜์„ ๊ฐ€๋ฆฌํ‚ฌ ์ˆ˜ ์žˆ์–ด์•ผ ๋น„ํŠธ์ฝ”์ธ์ด ์˜จ ๊ทผ๊ฑฐ๋ฅผ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ด ๋•Œ ์ด์ „ ํŠธ๋žœ์žญ์…˜์„ ๋‚˜ํƒ€๋‚ด๋Š” ํ•ด์‹œ๊ฐ’์€ hash256์„ ์‚ฌ์šฉํ•œ๋‹ค.
  • hash256์€ ์•„์ง โ€œํ•ด์‹œ ์ถฉ๋Œโ€์ด ๊ฑฐ์˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํŠน์ • ํŠธ๋ž™์žญ์…˜์„ ๋‚˜ํƒ€๋‚ด๋Š” Id์˜ ์—ญํ• ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

Previous Tx index

  • ์œ„์˜ ์„ค๋ช…์„ ๋ณด๋‹ค๊ฐ€ โ€œ์Œ? ์ด์ƒํ•œ๋ฐโ€๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค๋ฉด ๋งž๋‹ค.
  • โ€œ์ด์ „ ํŠธ๋žœ์žญ์…˜โ€๋งŒ์„ ์•Œ๊ณ ์„œ๋Š” ์–ด๋–ค ๋น„ํŠธ์ฝ”์ธ์„ ์‚ฌ์šฉํ• ์ง€ ํŠน์ • ํ•  ์ˆ˜ ์—†๋‹ค.
  • ์œ„์˜ ๊ทธ๋ฆผ์„ ๋ณด๋ฉด, A์˜ ๋น„ํŠธ์ฝ”์ธ์ด A, B์˜ ์ง€๊ฐ‘์œผ๋กœ ๊ฐ๊ฐ ์ด๋™ํ–ˆ๋‹ค.
  • ๊ทธ๋ ‡๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ A๊ฐ€ ๋น„ํŠธ์ฝ”์ธ์„ ์ „์†กํ•œ๋‹ค๋ฉด, ์ด์ „ ํŠธ๋ž™์žญ์…˜์—์„œ 2๋ฒˆ์งธ Output์ธ A์˜ ์ง€๊ฐ‘์—์„œ ์ถœ๊ธˆํ•ด์•ผ ํ•  ๊ฒƒ์ด๋‹ค.
  • ์ฆ‰, ์ด์ „ ํŠธ๋ž™์žญ์…˜์—์„œ ๋ช‡๋ฒˆ์จฐ ์ถœ๋ ฅ์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
  • ์ด๋ฅผ ํŠน์ •ํ•˜๋Š” ๊ฒƒ์ด Tx index์ด๋‹ค.
  • 4๋ฐ”์ดํŠธ๋กœ ์ฝ์œผ๋ฉด ๋œ๋‹ค.

ScriptSig

  • ์ž…๋ ฅ์˜ ์œ ํšจ์„ฑ ๊ฒ€์ฆ
  • ์ฃผ๋Š” ์ชฝ์—์„œ ๋น„ํŠธ์ฝ”์ธ์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์„œ๋ช… ๋ฐ ์ธ์ฆ๊ณผ ๊ด€๋ จ๋œ ์ •๋ณด๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.
  • ์ด ์ •๋ณด๋Š” ํŠธ๋žœ์žญ์…˜ ์ถœ๋ ฅ(Output) ๋ถ€๋ถ„์—์„œ ์‚ฌ์šฉ๋œ ScriptPubKey์˜ ์Šคํฌ๋ฆฝํŠธ์™€ ๋Œ€์กฐ๋˜์–ด ๊ฒ€์ฆ๋œ๋‹ค.
  • ์ด์ œ ๋น„ํŠธ์ฝ”์ธ์ด ์–ด๋Š ํŠธ๋žœ์žญ์…˜ ์ถœ๋ ฅ์—์„œ ๋‚˜์™”๋Š”์ง€๋ฅผ ์•Œ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๋ฉด ๊ฒฐ๊ตญ ์ด ๋ˆ์œผ๋กœ โ€œ๋ฌด์—‡์„โ€ ํ•˜๊ฒ ๋ƒ๋ฅผ ์•Œ๋ ค์ฃผ์–ด์•ผ ํ•œ๋‹ค.
  • ์ด๊ฑธ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•œ ๊ฒƒ์ด ํ•ด์ œ ์Šคํฌ๋ฆฝํŠธ์ด๋‹ค.
  • ๋น„ํŠธ์ฝ”์ธ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์Šค๋งˆํŠธ ๊ณ„์•ฝ ์–ธ์–ด์ธ Script๋ฅผ ๊ตฌ์„ฑํ•˜๋Š” ๋ถ€๋ถ„์ด ScriptSig์ด๋‹ค.
  • ์ด๋Š” ๋‚˜์ค‘์— ์ œ๋Œ€๋กœ ๋‹ค๋ฃจ๋ฏ€๋กœ ์ผ๋‹จ์€ ๋„˜์–ด๊ฐ€๊ฒ ๋‹ค.
  • ํ•ด๋‹น ํ•„๋“œ ์—ญ์‹œ ๊ธธ์ด๊ฐ€ ๋ณ€ํ•˜๊ธฐ ๋•Œ๋ฌธ์— Varint ํ˜•์‹์œผ๋กœ ๊ธธ์ด๋ฅผ ์„ค์ •ํ›„ ์ ์–ด์ค€๋‹ค.

Sequence

  • ์ด๊ฑด ๋ญ˜๊นŒ?
  • ์ด๋Š” โ€œ๋งค์šฐ ๋นˆ๋ฒˆํ•œ ๊ฑฐ๋ž˜โ€๋ฅผ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ๋งŒ๋“ค์—ˆ๋‹ค.
  • ๋’ค์— ๋‚˜์˜ฌ โ€œLocktimeโ€์ด๋ผ๋Š” ๊ฐœ๋…๊ณผ โ€œSequenceโ€๋ฅผ ํ†ตํ•ด ๋นˆ๋ฒˆํ•œ ๊ฑฐ๋ž˜์‹œ ๋ธ”๋ก์ฒด์ธ์— ์ฆ‰๊ฐ ๋ฐ˜์˜ํ•˜์ง€ ์•Š๊ณ  ์ตœ์ข… ๊ฒฐ๊ณผ๋ฅผ ๊ฑฐ๋ž˜ํ•จ์œผ๋กœ์จ ํšจ์œจ์„ฑ์„ ๋„๋ชจํ•˜๊ธฐ ์œ„ํ•ด ์ถ”๊ฐ€ํ–ˆ๋‹ค.
  • ๋นˆ๋ฒˆํ•œ ๊ฑฐ๋ž˜๊ฐ€ 10๋ฒˆ์ด ์žˆ๋‹ค๊ณ  ํ–ˆ์„ ๋–„, sequence ๊ฐ’์€ ๊ฑฐ๋ž˜๋งˆ๋‹ค 1์”ฉ ์ฆ๊ฐ€ํ•˜๊ฒŒ ๋œ๋‹ค.
  • ๊ทธ๋Ÿฌ๋‹ค๊ฐ€ Locktime์ด ์œ ํšจํ•œ ์ƒํƒœ๊ฐ€ ๋  ๊ฒฝ์šฐ, ํ•˜๋‚˜์˜ ํŠธ๋žœ์žญ์…˜์œผ๋กœ ๋ฌถ์–ด ๊ธฐ๋กํ•˜๋„๋ก ํ•œ๋‹ค.
  • ์ข‹์€ ์ƒ๊ฐ์ด๋‚˜ ์•…์šฉํ•˜๊ธฐ ์‰ฌ์šธ ์ˆ˜ ์žˆ๋‹ค.
  • A์™€ B๊ฐ€ ๊ฑฐ๋ž˜๋ฅผ ํ•˜๋Š”๋ฐ, A๊ฐ€ ์ฑ„๊ตด์ž๋ผ๊ณ  ํ•˜์ž.
    • ์ฑ„๊ตด์ž๋Š” ๊ฑฐ๋ž˜๋ฅผ ๋ชจ์•„ ๋ธ”๋ก์ฒด์ธ์— ์‹ค์ œ๋กœ ๊ธฐ๋ก์„ ํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒŒ์ž„์— ์ฐธ์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ง‘๋‹จ์ด๋ผ ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.
  • sequence๊ฐ€ ์˜ฌ๋ผ๊ฐˆ ๋•Œ, B๊ฐ€ A์—๊ฒŒ ๋ˆ์„ ์ฃผ๋Š” ํŠธ๋žœ์žญ์…˜๋งŒ ๊ผฝ์•„์„œ ๋จผ์ € ์ฒด์ธ์— ๋„ฃ์–ด๋ฒ„๋ฆด ์ˆ˜ ์žˆ๋‹ค.
  • ํ•˜์ง€๋งŒ ์ด๋Ÿฐ ๋ฐœ์ƒ ์ž์ฒด๋Š” ๋งค์šฐ ํ›Œ๋ฅญํ•˜์—ฌ ๋ผ์ดํŠธ๋‹ ๋„คํŠธ์›Œํฌ์˜ ํ† ๋Œ€๊ฐ€ ๋˜์—ˆ๋‹ค.
    • ์ˆ˜์ˆ˜๋ฃŒ๋ฅผ ๋‚ฎ์ถœ ์ˆ˜ ์žˆ์Œ
    • ์†๋„๋ฅผ ๋†’ํž ์ˆ˜ ์žˆ์Œ

TxIn

class TxIn:
    def __init__(self, prev_tx, prev_index, script_sig=None, sequence=0xffffffff):
        self.prev_tx = prev_tx
        self.prev_index = prev_index
        if script_sig is None:
            self.script_sig = Script()
        else:
            self.script_sig = script_sig
        self.sequence = sequence
 
    def __repr__(self):
        return '{}:{}'.format(
            self.prev_tx.hex(),
            self.prev_index,
        )
 
    @classmethod
    def parse(cls, s):
        '''
        byte ์ŠคํŠธ๋ฆผ์„ ๋ฐ›์•„์„œ ์‹œ์ž‘๋ถ€๋ถ„์˜ ํŠธ๋žœ์žญ์…˜ ์ž…๋ ฅ์„ ํŒŒ์‹ฑํ•œ๋‹ค.
        TxIn ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
        '''
        # prev_tx is 32 bytes, little endian
        prev_tx = s.read(32)[::-1]
        # prev_index is an integer in 4 bytes, little endian
        prev_index = little_endian_to_int(s.read(4))
        # use Script.parse to get the ScriptSig
        script_sig = Script.parse(s)
        # sequence is an integer in 4 bytes, little-endian
        sequence = little_endian_to_int(s.read(4))
        # return an instance of the class (see __init__ for args)
        return cls(prev_tx, prev_index, script_sig, sequence)
 
    def serialize(self):
        '''Returns the byte serialization of the transaction input'''
        result = self.prev_tx[::-1]
        result += int_to_little_endian(self.prev_index, 4)
        result += self.script_sig.serialize()
        result += int_to_little_endian(self.sequence, 4)
        return result
 
    def fetch_tx(self, testnet=False):
        return TxFetcher.fetch(self.prev_tx.hex(), testnet=testnet)
 
    def value(self, testnet=False):
        '''Get the output value by looking up the tx hash.
        Returns the amount in satoshi.
        '''
        tx = self.fetch_tx(testnet=testnet)
        return tx.tx_outs[self.prev_index].amount
 
    def script_pubkey(self, testnet=False):
        '''Get the ScriptPubKey by looking up the tx hash.
        Returns a Script object.
        '''
        tx = self.fetch_tx(testnet=testnet)
        return tx.tx_outs[self.prev_index].script_pubkey
 

Reference