์ฝ˜ํ…์ธ  ๋Œ€ํ‘œ ์ด๋ฏธ์ง€ - ๐Ÿ”— ํŒŒ์ด์ฌ๊ณผ IPFS๋กœํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ ๊ตฌ์ถ•

๐Ÿ”— ํŒŒ์ด์ฌ๊ณผ IPFS๋กœ
ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ ๊ตฌ์ถ•

๋ธ”๋ก์ฒด์ธ/Web3 ร— ํ”„๋กœ๊ทธ๋žจ๊ฐœ๋ฐœ | ์ง„์งœ ๋‚ด ํŒŒ์ผ์€ ๋‚ด๊ฐ€ ์ง€ํ‚จ๋‹ค ๐Ÿ›ก๏ธ
Python IPFS Web3 ํƒˆ์ค‘์•™ํ™” ๋ธ”๋ก์ฒด์ธ

์•ผ ์†”์งํžˆ ๋งํ•ด๋ด. ๊ตฌ๊ธ€ ๋“œ๋ผ์ด๋ธŒ๋‚˜ ๋“œ๋กญ๋ฐ•์Šค์— ์ค‘์š”ํ•œ ํŒŒ์ผ ์˜ฌ๋ ค๋†“๊ณ  "์ด๊ฑฐ ์•ˆ์ „ํ•˜๊ฒ ์ง€~" ํ•˜๊ณ  ์•ˆ์‹ฌํ•œ ์  ์žˆ์ง€? ๐Ÿค”
๊ทผ๋ฐ ์ƒ๊ฐํ•ด๋ด. ๊ทธ ์„œ๋ฒ„๊ฐ€ ๊ฐ‘์ž๊ธฐ ํ„ฐ์ง€๊ฑฐ๋‚˜, ํšŒ์‚ฌ๊ฐ€ ๋งํ•˜๊ฑฐ๋‚˜, ์•„๋‹ˆ๋ฉด ๊ทธ๋ƒฅ ๋‹ˆ ๊ณ„์ •์ด ์ •์ง€๋˜๋ฉด? ํŒŒ์ผ ๊ทธ๋ƒฅ ์ฆ๋ฐœ์ด์•ผ ใ…‹ใ…‹ใ…‹

๊ทธ๋ž˜์„œ ์˜ค๋Š˜ ์šฐ๋ฆฌ๊ฐ€ ๋ฐฐ์šธ ๊ฒŒ ๋ฐ”๋กœ IPFS(InterPlanetary File System)๋ž‘ Python์„ ์กฐํ•ฉํ•ด์„œ ๋งŒ๋“œ๋Š” ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ์ด์•ผ.
์ค‘์•™ ์„œ๋ฒ„ ์—†์ด, ์•„๋ฌด๋„ ๋‚ด ํŒŒ์ผ ๋ชป ๊ฑด๋“œ๋ฆฌ๊ณ , ๊ฒ€์—ด๋„ ์—†๊ณ , ์˜๊ตฌ ๋ณด์กด ๊ฐ€๋Šฅํ•œ ๊ทธ ์‹œ์Šคํ…œ ๋ง์ด์•ผ. ์™„์ „ Web3 ๊ฐ์„ฑ ๋ฟœ๋ฟœ ๐Ÿš€

โŒ ์ค‘์•™ํ™” ์ €์žฅ ์ค‘์•™ ์„œ๋ฒ„ ๋‹จ์ผ ์ง€์  ์œ ์ €1 ์œ ์ €2 ์œ ์ €3 ์œ ์ €4 ๐Ÿ’ฅ ์„œ๋ฒ„ ๋‹ค์šด = ์ „์ฒด ๋งˆ๋น„ ๊ฒ€์—ด ๊ฐ€๋Šฅ / ๋ฐ์ดํ„ฐ ์†์‹ค ์œ„ํ—˜ โœ… IPFS ํƒˆ์ค‘์•™ ์ €์žฅ ๋…ธ๋“œA ํŒŒ์ผ์กฐ๊ฐ ๋…ธ๋“œB ํŒŒ์ผ์กฐ๊ฐ ๋…ธ๋“œC ํŒŒ์ผ์กฐ๊ฐ ๋…ธ๋“œD ํŒŒ์ผ์กฐ๊ฐ ๋…ธ๋“œE ํŒŒ์ผ์กฐ๊ฐ ๋…ธ๋“œ ํ•˜๋‚˜ ์ฃฝ์–ด๋„ ํŒŒ์ผ ์‚ด์•„์žˆ์Œ! ๊ฒ€์—ด ๋ถˆ๊ฐ€ / ์˜๊ตฌ ๋ณด์กด / ๋ถ„์‚ฐ ์ €์žฅ

๐ŸŒ IPFS๊ฐ€ ๋ญ”๋ฐ? ์ง„์งœ ์‰ฝ๊ฒŒ ์„ค๋ช…ํ•ด์ค„๊ฒŒ

IPFS๋Š” InterPlanetary File System์˜ ์•ฝ์ž์•ผ. ์ง์—ญํ•˜๋ฉด "ํ–‰์„ฑ ๊ฐ„ ํŒŒ์ผ ์‹œ์Šคํ…œ"์ธ๋ฐ, ์ด๋ฆ„๋ถ€ํ„ฐ ์Šค์ผ€์ผ์ด ์šฐ์ฃผ๊ธ‰์ด์ง€? ๐Ÿ˜‚
2015๋…„์— Protocol Labs์˜ Juan Benet์ด ๋งŒ๋“ค์—ˆ๊ณ , ์ง€๊ธˆ์€ Web3 ์ƒํƒœ๊ณ„์—์„œ ์—†์–ด์„œ๋Š” ์•ˆ ๋  ํ•ต์‹ฌ ์ธํ”„๋ผ๊ฐ€ ๋์–ด.

๊ธฐ์กด ์ธํ„ฐ๋„ท(HTTP)์€ ์œ„์น˜ ๊ธฐ๋ฐ˜ ์ฃผ์†Œ์•ผ. ์˜ˆ๋ฅผ ๋“ค์–ด https://example.com/file.pdf ์ด๋Ÿฐ ์‹์œผ๋กœ "์–ด๋””์—" ์žˆ๋Š”์ง€๋กœ ํŒŒ์ผ์„ ์ฐพ์•„.
๊ทผ๋ฐ IPFS๋Š” ์ฝ˜ํ…์ธ  ๊ธฐ๋ฐ˜ ์ฃผ์†Œ(Content Addressing)์•ผ. ํŒŒ์ผ์˜ ๋‚ด์šฉ ์ž์ฒด๋ฅผ ํ•ด์‹œํ•ด์„œ ๊ทธ ํ•ด์‹œ๊ฐ’์ด ์ฃผ์†Œ๊ฐ€ ๋ผ.

๐Ÿ’ก ์ฝ˜ํ…์ธ  ์ฃผ์†Œ ๋ฐฉ์‹์ด๋ž€? ํŒŒ์ผ ๋‚ด์šฉ์„ SHA-256 ๊ฐ™์€ ํ•ด์‹œ ํ•จ์ˆ˜๋กœ ๋Œ๋ฆฌ๋ฉด ๊ณ ์œ ํ•œ ํ•ด์‹œ๊ฐ’์ด ๋‚˜์™€.
์˜ˆ: QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG
์ด๊ฒŒ ๋ฐ”๋กœ IPFS์˜ CID(Content Identifier)์•ผ. ํŒŒ์ผ ๋‚ด์šฉ์ด ๊ฐ™์œผ๋ฉด CID๋„ ํ•ญ์ƒ ๊ฐ™๊ณ , ๋‚ด์šฉ์ด 1๋น„ํŠธ๋ผ๋„ ๋‹ค๋ฅด๋ฉด ์™„์ „ํžˆ ๋‹ค๋ฅธ CID๊ฐ€ ๋‚˜์™€. ์œ„๋ณ€์กฐ ๊ฐ์ง€ ์ž๋™ ๋‚ด์žฅ ใ„ทใ„ท
๐Ÿ“ฆ IPFS์˜ ํ•ต์‹ฌ ๊ฐœ๋… 3๊ฐ€์ง€
1
DHT (Distributed Hash Table)
์–ด๋–ค ๋…ธ๋“œ๊ฐ€ ์–ด๋–ค ํŒŒ์ผ์„ ๊ฐ–๊ณ  ์žˆ๋Š”์ง€ ๋ถ„์‚ฐ ์ €์žฅํ•˜๋Š” ๋ผ์šฐํŒ… ํ…Œ์ด๋ธ”์ด์•ผ. ์ค‘์•™ ์„œ๋ฒ„ ์—†์ด "์ด CID ๊ฐ€์ง„ ๋…ธ๋“œ ์–ด๋”จ์–ด?" ํ•˜๊ณ  ๋„คํŠธ์›Œํฌ ์ „์ฒด์— ๋ฌผ์–ด๋ณด๋Š” ๋ฐฉ์‹.
2
Merkle DAG (Directed Acyclic Graph)
ํŒŒ์ผ์„ 256KB ๋‹จ์œ„ ์ฒญํฌ๋กœ ์ชผ๊ฐœ์„œ ํŠธ๋ฆฌ ๊ตฌ์กฐ๋กœ ์—ฐ๊ฒฐํ•ด. ๊ฐ ์ฒญํฌ๊ฐ€ ํ•ด์‹œ๋กœ ์—ฐ๊ฒฐ๋˜๋‹ˆ๊นŒ ๋ฐ์ดํ„ฐ ๋ฌด๊ฒฐ์„ฑ์ด ์ˆ˜ํ•™์ ์œผ๋กœ ๋ณด์žฅ๋ผ. ๋ธ”๋ก์ฒด์ธ์ด๋ž‘ ๊ฐ™์€ ์›๋ฆฌ์•ผ!
3
Bitswap ํ”„๋กœํ† ์ฝœ
๋…ธ๋“œ๋“ค๋ผ๋ฆฌ ํŒŒ์ผ ์กฐ๊ฐ์„ ๊ตํ™˜ํ•˜๋Š” ํ”„๋กœํ† ์ฝœ์ด์•ผ. BitTorrent๋ž‘ ๋น„์Šทํ•œ ๊ฐœ๋…์ธ๋ฐ, ๋‚ด๊ฐ€ ํŒŒ์ผ ์กฐ๊ฐ์„ ๊ณต์œ ํ•˜๋ฉด ๋‹ค๋ฅธ ์กฐ๊ฐ์„ ๋ฐ›์„ ์ˆ˜ ์žˆ๋Š” ์ผ์ข…์˜ ๋ฌผ๋ฌผ๊ตํ™˜ ์‹œ์Šคํ…œ.

โš™๏ธ ๊ฐœ๋ฐœ ํ™˜๊ฒฝ ์„ธํŒ… - ์ผ๋‹จ ๊น”์•„์•ผ ์‹œ์ž‘ํ•˜์ง€

์ž ์ด์ œ ์ง„์งœ ์ฝ”๋”ฉ ๋“ค์–ด๊ฐ€๊ธฐ ์ „์— ํ™˜๊ฒฝ๋ถ€ํ„ฐ ์„ธํŒ…ํ•˜์ž. ํ•„์š”ํ•œ ๊ฑฐ ๋ฆฌ์ŠคํŠธ์—… ํ•ด์ค„๊ฒŒ ๐Ÿ“‹

๐Ÿ› ๏ธ ํ•„์š”ํ•œ ๊ฒƒ๋“ค

Python 3.8+ IPFS Desktop ๋˜๋Š” IPFS CLI ipfshttpclient ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ web3.py (์„ ํƒ) requests

๐Ÿ Step 1: IPFS ๋ฐ๋ชฌ ์„ค์น˜

๋จผ์ € IPFS ๊ณต์‹ ์‚ฌ์ดํŠธ(dist.ipfs.tech)์—์„œ Kubo(๊ตฌ go-ipfs)๋ฅผ ๋‹ค์šด๋ฐ›์•„. ์„ค์น˜ ํ›„ ์ดˆ๊ธฐํ™”:

ํ„ฐ๋ฏธ๋„
# IPFS ์ดˆ๊ธฐํ™” (์ฒ˜์Œ ํ•œ ๋ฒˆ๋งŒ)
ipfs init

# IPFS ๋ฐ๋ชฌ ์‹คํ–‰ (๋ฐฑ๊ทธ๋ผ์šด๋“œ์—์„œ ๊ณ„์† ๋Œ์•„์•ผ ํ•ด)
ipfs daemon

# ์ •์ƒ ์‹คํ–‰๋˜๋ฉด ์ด๋Ÿฐ ๋ฉ”์‹œ์ง€ ๋‚˜์˜ด:
# Daemon is ready
# API server listening on /ip4/127.0.0.1/tcp/5001
๐Ÿ’ก IPFS ๋ฐ๋ชฌ์ด ์‹คํ–‰๋˜๋ฉด ๋กœ์ปฌ์—์„œ http://127.0.0.1:5001๋กœ API ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๊ณ ,
http://127.0.0.1:8080์œผ๋กœ ๊ฒŒ์ดํŠธ์›จ์ด ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด. ํŒŒ์ด์ฌ์—์„œ ์ด API๋ฅผ ํ†ตํ•ด ํ†ต์‹ ํ•  ๊ฑฐ์•ผ!
๐Ÿ“ฆ Step 2: Python ํŒจํ‚ค์ง€ ์„ค์น˜
ํ„ฐ๋ฏธ๋„
# ๊ฐ€์ƒํ™˜๊ฒฝ ๋งŒ๋“ค๊ณ  (๊ถŒ์žฅ)
python -m venv ipfs-env
source ipfs-env/bin/activate  # Windows: ipfs-env\Scripts\activate

# ํ•„์š”ํ•œ ํŒจํ‚ค์ง€ ์„ค์น˜
pip install ipfshttpclient
pip install requests
pip install web3
pip install python-dotenv
pip install cryptography  # ์•”ํ˜ธํ™” ๊ธฐ๋Šฅ ์“ธ ๋•Œ
โš ๏ธ ์ฃผ์˜! ipfshttpclient๋Š” IPFS API ๋ฒ„์ „์— ๋ฏผ๊ฐํ•ด.
Kubo 0.11.0 ์ด์ƒ์ด๋ฉด ipfshttpclient==0.8.0a2 ์ด์ƒ ์จ์•ผ ํ•ด.
๋ฒ„์ „ ์•ˆ ๋งž์œผ๋ฉด ์—ฐ๊ฒฐ ์—๋Ÿฌ ๋œจ๋‹ˆ๊นŒ ๊ผญ ํ™•์ธํ•˜๊ณ !

๐Ÿ—๏ธ ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ ์•„ํ‚คํ…์ฒ˜ โ‘  ์‚ฌ์šฉ์ž ๋ ˆ์ด์–ด Python ํด๋ผ์ด์–ธํŠธ ๋ฌธ์„œ ์•”ํ˜ธํ™” ๋ชจ๋“ˆ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ CID ์ธ๋ฑ์Šค DB ๊ฒ€์ƒ‰/์กฐํšŒ API โ‘ก IPFS ๋„คํŠธ์›Œํฌ ๋ ˆ์ด์–ด ๋กœ์ปฌ IPFS ๋…ธ๋“œ Pinning ์„œ๋น„์Šค DHT ๋ผ์šฐํŒ… Bitswap ๊ตํ™˜ ๊ฒŒ์ดํŠธ์›จ์ด โ‘ข ๋ธ”๋ก์ฒด์ธ ๋ ˆ์ด์–ด (์„ ํƒ์ ) ์Šค๋งˆํŠธ ์ปจํŠธ๋ž™ํŠธ CID ์˜จ์ฒด์ธ ๋“ฑ๋ก ์†Œ์œ ๊ถŒ ์ฆ๋ช… ํƒ€์ž„์Šคํƒฌํ”„ ๊ธฐ๋ก ํŒŒ์ด์ฌ โ†” IPFS API โ†” ๋ถ„์‚ฐ ๋„คํŠธ์›Œํฌ โ†” (์„ ํƒ) ๋ธ”๋ก์ฒด์ธ ์™„์ „ํ•œ ํƒˆ์ค‘์•™ํ™” ์Šคํƒ์œผ๋กœ ๊ฒ€์—ด ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ ๊ตฌํ˜„ * Pinata, Infura, Web3.Storage ๊ฐ™์€ Pinning ์„œ๋น„์Šค๋กœ ์˜๊ตฌ ๋ณด์กด ๊ฐ€๋Šฅ

๐Ÿ ํŒŒ์ด์ฌ์œผ๋กœ IPFS ์—ฐ๋™ํ•˜๊ธฐ - ๊ธฐ์ดˆํŽธ

์ž ์ด์ œ ์ง„์งœ ์ฝ”๋“œ ์งœ๋ณด์ž! ์ฒ˜์Œ์—” ๊ธฐ๋ณธ์ ์ธ ํŒŒ์ผ ์—…๋กœ๋“œ/๋‹ค์šด๋กœ๋“œ๋ถ€ํ„ฐ ์‹œ์ž‘ํ•ด์„œ ์ ์  ๊ณ ๋„ํ™”ํ•  ๊ฑฐ์•ผ ๐Ÿ’ช

๐Ÿ“ค ํŒŒ์ผ ์—…๋กœ๋“œ (Add)
Python - ipfs_basic.py
import ipfshttpclient
import json
import os
from datetime import datetime

class IPFSClient:
    """IPFS ํด๋ผ์ด์–ธํŠธ ๊ธฐ๋ณธ ํด๋ž˜์Šค"""
    
    def __init__(self, api_url='/ip4/127.0.0.1/tcp/5001'):
        """
        IPFS ๋ฐ๋ชฌ์— ์—ฐ๊ฒฐ
        api_url: ๋กœ์ปฌ IPFS ๋ฐ๋ชฌ API ์ฃผ์†Œ
        """
        try:
            self.client = ipfshttpclient.connect(api_url)
            print(f"โœ… IPFS ์—ฐ๊ฒฐ ์„ฑ๊ณต!")
            # ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ ์ •๋ณด ์ถœ๋ ฅ
            node_info = self.client.id()
            print(f"๐Ÿ“ ๋…ธ๋“œ ID: {node_info['ID'][:20]}...")
        except Exception as e:
            print(f"โŒ IPFS ์—ฐ๊ฒฐ ์‹คํŒจ: {e}")
            raise
    
    def upload_file(self, file_path: str) -> dict:
        """
        ํŒŒ์ผ์„ IPFS์— ์—…๋กœ๋“œ
        Returns: {'cid': str, 'size': int, 'timestamp': str}
        """
        if not os.path.exists(file_path):
            raise FileNotFoundError(f"ํŒŒ์ผ ์—†์Œ: {file_path}")
        
        print(f"๐Ÿ“ค ์—…๋กœ๋“œ ์ค‘: {file_path}")
        
        # IPFS์— ํŒŒ์ผ ์ถ”๊ฐ€
        result = self.client.add(file_path)
        
        # ๊ฒฐ๊ณผ ์ •๋ฆฌ
        upload_info = {
            'cid': result['Hash'],          # Content Identifier
            'name': result['Name'],          # ํŒŒ์ผ๋ช…
            'size': int(result['Size']),     # ๋ฐ”์ดํŠธ ํฌ๊ธฐ
            'timestamp': datetime.now().isoformat(),
            'ipfs_url': f"ipfs://{result['Hash']}",
            'gateway_url': f"https://ipfs.io/ipfs/{result['Hash']}"
        }
        
        print(f"โœ… ์—…๋กœ๋“œ ์™„๋ฃŒ!")
        print(f"   CID: {upload_info['cid']}")
        print(f"   ํฌ๊ธฐ: {upload_info['size']:,} bytes")
        print(f"   ๊ฒŒ์ดํŠธ์›จ์ด: {upload_info['gateway_url']}")
        
        return upload_info
    
    def upload_text(self, text: str, filename: str = "document.txt") -> dict:
        """
        ํ…์ŠคํŠธ ๋‚ด์šฉ์„ ์ง์ ‘ IPFS์— ์—…๋กœ๋“œ
        """
        # ์ž„์‹œ ํŒŒ์ผ๋กœ ์ €์žฅ ํ›„ ์—…๋กœ๋“œ
        temp_path = f"/tmp/{filename}"
        with open(temp_path, 'w', encoding='utf-8') as f:
            f.write(text)
        
        result = self.upload_file(temp_path)
        os.remove(temp_path)  # ์ž„์‹œ ํŒŒ์ผ ์‚ญ์ œ
        return result
    
    def upload_json(self, data: dict, filename: str = "data.json") -> dict:
        """
        ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ JSON์œผ๋กœ ๋ณ€ํ™˜ํ•ด์„œ IPFS์— ์—…๋กœ๋“œ
        ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ €์žฅ์— ์œ ์šฉ!
        """
        json_str = json.dumps(data, ensure_ascii=False, indent=2)
        return self.upload_text(json_str, filename)
    
    def download_file(self, cid: str, output_path: str) -> str:
        """
        CID๋กœ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ
        """
        print(f"๐Ÿ“ฅ ๋‹ค์šด๋กœ๋“œ ์ค‘: {cid}")
        self.client.get(cid, target=output_path)
        print(f"โœ… ๋‹ค์šด๋กœ๋“œ ์™„๋ฃŒ: {output_path}")
        return output_path
    
    def read_content(self, cid: str) -> bytes:
        """
        CID์˜ ๋‚ด์šฉ์„ ๋ฐ”์ดํŠธ๋กœ ์ฝ๊ธฐ
        """
        return self.client.cat(cid)
    
    def read_text(self, cid: str, encoding: str = 'utf-8') -> str:
        """
        CID์˜ ๋‚ด์šฉ์„ ํ…์ŠคํŠธ๋กœ ์ฝ๊ธฐ
        """
        return self.read_content(cid).decode(encoding)
    
    def pin_file(self, cid: str) -> bool:
        """
        ํŒŒ์ผ ํ•€(Pin) - ๋กœ์ปฌ ๋…ธ๋“œ์—์„œ ์‚ญ์ œ ๋ฐฉ์ง€
        ํ•€ ์•ˆ ํ•˜๋ฉด ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์œผ๋กœ ์‚ฌ๋ผ์งˆ ์ˆ˜ ์žˆ์–ด!
        """
        try:
            self.client.pin.add(cid)
            print(f"๐Ÿ“Œ ํ•€ ์™„๋ฃŒ: {cid}")
            return True
        except Exception as e:
            print(f"โŒ ํ•€ ์‹คํŒจ: {e}")
            return False
    
    def get_node_stats(self) -> dict:
        """๋…ธ๋“œ ํ†ต๊ณ„ ์ •๋ณด"""
        stats = self.client.stats.repo()
        return {
            'num_objects': stats['NumObjects'],
            'repo_size': stats['RepoSize'],
            'storage_max': stats['StorageMax']
        }


# ์‚ฌ์šฉ ์˜ˆ์‹œ
if __name__ == "__main__":
    # ํด๋ผ์ด์–ธํŠธ ์ƒ์„ฑ
    ipfs = IPFSClient()
    
    # ํ…์ŠคํŠธ ๋ฌธ์„œ ์—…๋กœ๋“œ
    result = ipfs.upload_text(
        "์•ˆ๋…•ํ•˜์„ธ์š”! ์ด ๋ฌธ์„œ๋Š” IPFS์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ํƒˆ์ค‘์•™ํ™” ๋งŒ์„ธ!",
        "hello_ipfs.txt"
    )
    
    # CID๋กœ ๋‚ด์šฉ ์ฝ๊ธฐ
    content = ipfs.read_text(result['cid'])
    print(f"\n๐Ÿ“– ์ฝ์€ ๋‚ด์šฉ: {content}")
    
    # ํ•€์œผ๋กœ ์˜๊ตฌ ๋ณด์กด
    ipfs.pin_file(result['cid'])
โœ… CID์˜ ๋งˆ๋ฒ•! ๊ฐ™์€ ๋‚ด์šฉ์˜ ํŒŒ์ผ์„ 100๋ฒˆ ์˜ฌ๋ ค๋„ CID๋Š” ํ•ญ์ƒ ๊ฐ™์•„. ์ฆ‰ IPFS๋Š” ์ž๋™์œผ๋กœ ์ค‘๋ณต ์ œ๊ฑฐ(deduplication)๋ฅผ ํ•ด์ค˜.
"hello world"๋ฅผ ์˜ฌ๋ฆฌ๋ฉด ์ „ ์„ธ๊ณ„ ์–ด๋””์„œ ์˜ฌ๋ ค๋„ ๊ฐ™์€ CID๊ฐ€ ๋‚˜์˜จ๋‹ค๋Š” ๊ฑฐ ใ„ทใ„ท

๐Ÿ“š ๋ฌธ์„œ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ ๊ตฌ์ถ• - ์ค‘๊ธ‰ํŽธ

๊ธฐ๋ณธ ์—…/๋‹ค์šด๋กœ๋“œ๋Š” ๋๊ณ , ์ด์ œ ์ง„์งœ ์“ธ๋งŒํ•œ ๋ฌธ์„œ ๊ด€๋ฆฌ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด๋ณด์ž.
๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ด€๋ฆฌ, ๋ฒ„์ „ ๊ด€๋ฆฌ, ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ๊นŒ์ง€ ๋„ฃ์„ ๊ฑฐ์•ผ. ์ด๊ฑฐ ์™„์„ฑํ•˜๋ฉด ์ง„์งœ ์“ธ ์ˆ˜ ์žˆ๋Š” ๋ ˆ๋ฒจ์ž„ ๐Ÿ”ฅ

๐Ÿ—‚๏ธ ๋ฌธ์„œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์Šคํ‚ค๋งˆ ์„ค๊ณ„
Python - document_schema.py
from dataclasses import dataclass, asdict
from typing import Optional, List
from datetime import datetime
import hashlib
import json

@dataclass
class DocumentMetadata:
    """๋ฌธ์„œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์Šคํ‚ค๋งˆ"""
    
    # ๊ธฐ๋ณธ ์ •๋ณด
    title: str                          # ๋ฌธ์„œ ์ œ๋ชฉ
    author: str                         # ์ž‘์„ฑ์ž
    description: str                    # ์„ค๋ช…
    tags: List[str]                     # ํƒœ๊ทธ ๋ชฉ๋ก
    
    # IPFS ์ •๋ณด
    content_cid: str                    # ์‹ค์ œ ๋ฌธ์„œ ๋‚ด์šฉ์˜ CID
    
    # ๋ฒ„์ „ ๊ด€๋ฆฌ
    version: int = 1                    # ๋ฒ„์ „ ๋ฒˆํ˜ธ
    previous_cid: Optional[str] = None  # ์ด์ „ ๋ฒ„์ „ CID (๋ฒ„์ „ ์ฒด์ธ)
    
    # ํƒ€์ž„์Šคํƒฌํ”„
    created_at: str = ""
    updated_at: str = ""
    
    # ํŒŒ์ผ ์ •๋ณด
    file_type: str = "text/plain"       # MIME ํƒ€์ž…
    file_size: int = 0                  # ๋ฐ”์ดํŠธ
    
    # ์ ‘๊ทผ ์ œ์–ด
    is_public: bool = True              # ๊ณต๊ฐœ ์—ฌ๋ถ€
    owner_address: Optional[str] = None # ์ด๋”๋ฆฌ์›€ ์ฃผ์†Œ (์„ ํƒ)
    
    def __post_init__(self):
        now = datetime.now().isoformat()
        if not self.created_at:
            self.created_at = now
        self.updated_at = now
    
    def to_dict(self) -> dict:
        return asdict(self)
    
    def to_json(self) -> str:
        return json.dumps(self.to_dict(), ensure_ascii=False, indent=2)
    
    @classmethod
    def from_dict(cls, data: dict) -> 'DocumentMetadata':
        return cls(**data)
    
    def get_fingerprint(self) -> str:
        """๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์˜ ๊ณ ์œ  ์ง€๋ฌธ ์ƒ์„ฑ"""
        key_data = f"{self.title}{self.author}{self.content_cid}{self.version}"
        return hashlib.sha256(key_data.encode()).hexdigest()[:16]
๐Ÿ“‹ ๋ฌธ์„œ ์ €์žฅ์†Œ ํด๋ž˜์Šค
Python - document_store.py
import ipfshttpclient
import json
import sqlite3
import os
from typing import Optional, List, Dict
from document_schema import DocumentMetadata
from datetime import datetime

class DecentralizedDocumentStore:
    """
    ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ์†Œ
    - IPFS: ์‹ค์ œ ํŒŒ์ผ ์ €์žฅ
    - SQLite: ๋กœ์ปฌ ์ธ๋ฑ์Šค (CID, ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰์šฉ)
    """
    
    def __init__(self, 
                 ipfs_url: str = '/ip4/127.0.0.1/tcp/5001',
                 db_path: str = 'doc_index.db'):
        
        # IPFS ์—ฐ๊ฒฐ
        self.ipfs = ipfshttpclient.connect(ipfs_url)
        
        # ๋กœ์ปฌ SQLite ์ธ๋ฑ์Šค ์ดˆ๊ธฐํ™”
        self.db_path = db_path
        self._init_database()
        
        print("๐Ÿš€ ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ์†Œ ์ดˆ๊ธฐํ™” ์™„๋ฃŒ!")
    
    def _init_database(self):
        """SQLite ์ธ๋ฑ์Šค DB ์ดˆ๊ธฐํ™”"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS documents (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                meta_cid TEXT UNIQUE NOT NULL,    -- ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ CID
                content_cid TEXT NOT NULL,         -- ๋‚ด์šฉ CID
                title TEXT NOT NULL,
                author TEXT NOT NULL,
                description TEXT,
                tags TEXT,                         -- JSON ๋ฐฐ์—ด๋กœ ์ €์žฅ
                version INTEGER DEFAULT 1,
                previous_cid TEXT,
                created_at TEXT,
                updated_at TEXT,
                file_type TEXT,
                file_size INTEGER,
                is_public INTEGER DEFAULT 1,
                owner_address TEXT
            )
        ''')
        
        # ๊ฒ€์ƒ‰ ์„ฑ๋Šฅ์„ ์œ„ํ•œ ์ธ๋ฑ์Šค
        cursor.execute('CREATE INDEX IF NOT EXISTS idx_title ON documents(title)')
        cursor.execute('CREATE INDEX IF NOT EXISTS idx_author ON documents(author)')
        cursor.execute('CREATE INDEX IF NOT EXISTS idx_content_cid ON documents(content_cid)')
        
        conn.commit()
        conn.close()
    
    def store_document(self, 
                       content: str, 
                       metadata: DocumentMetadata,
                       pin: bool = True) -> Dict:
        """
        ๋ฌธ์„œ๋ฅผ IPFS์— ์ €์žฅํ•˜๊ณ  ์ธ๋ฑ์Šค์— ๋“ฑ๋ก
        
        Args:
            content: ๋ฌธ์„œ ๋‚ด์šฉ (ํ…์ŠคํŠธ)
            metadata: ๋ฌธ์„œ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
            pin: ๋กœ์ปฌ ํ•€ ์—ฌ๋ถ€
        
        Returns:
            {'meta_cid': str, 'content_cid': str, 'success': bool}
        """
        print(f"\n๐Ÿ“ ๋ฌธ์„œ ์ €์žฅ ์‹œ์ž‘: '{metadata.title}'")
        
        # 1๋‹จ๊ณ„: ๋ฌธ์„œ ๋‚ด์šฉ IPFS ์—…๋กœ๋“œ
        content_bytes = content.encode('utf-8')
        content_result = self.ipfs.add_bytes(content_bytes)
        content_cid = content_result
        
        print(f"   โœ… ๋‚ด์šฉ ์—…๋กœ๋“œ: {content_cid}")
        
        # 2๋‹จ๊ณ„: ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์—…๋ฐ์ดํŠธ ๋ฐ ์—…๋กœ๋“œ
        metadata.content_cid = content_cid
        metadata.file_size = len(content_bytes)
        metadata.updated_at = datetime.now().isoformat()
        
        meta_json = metadata.to_json()
        meta_result = self.ipfs.add_bytes(meta_json.encode('utf-8'))
        meta_cid = meta_result
        
        print(f"   โœ… ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์—…๋กœ๋“œ: {meta_cid}")
        
        # 3๋‹จ๊ณ„: ํ•€ ์„ค์ • (์˜๊ตฌ ๋ณด์กด)
        if pin:
            self.ipfs.pin.add(content_cid)
            self.ipfs.pin.add(meta_cid)
            print(f"   ๐Ÿ“Œ ํ•€ ์™„๋ฃŒ")
        
        # 4๋‹จ๊ณ„: ๋กœ์ปฌ ์ธ๋ฑ์Šค์— ๋“ฑ๋ก
        self._index_document(meta_cid, metadata)
        print(f"   ๐Ÿ“Š ์ธ๋ฑ์Šค ๋“ฑ๋ก ์™„๋ฃŒ")
        
        result = {
            'meta_cid': meta_cid,
            'content_cid': content_cid,
            'title': metadata.title,
            'version': metadata.version,
            'gateway_url': f"https://ipfs.io/ipfs/{content_cid}",
            'success': True
        }
        
        print(f"\n๐ŸŽ‰ ์ €์žฅ ์™„๋ฃŒ!")
        print(f"   ๋ฉ”ํƒ€ CID: {meta_cid}")
        print(f"   ๋‚ด์šฉ CID: {content_cid}")
        
        return result
    
    def _index_document(self, meta_cid: str, metadata: DocumentMetadata):
        """๋กœ์ปฌ SQLite์— ๋ฌธ์„œ ์ธ๋ฑ์‹ฑ"""
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        cursor.execute('''
            INSERT OR REPLACE INTO documents 
            (meta_cid, content_cid, title, author, description, tags,
             version, previous_cid, created_at, updated_at, 
             file_type, file_size, is_public, owner_address)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
        ''', (
            meta_cid,
            metadata.content_cid,
            metadata.title,
            metadata.author,
            metadata.description,
            json.dumps(metadata.tags, ensure_ascii=False),
            metadata.version,
            metadata.previous_cid,
            metadata.created_at,
            metadata.updated_at,
            metadata.file_type,
            metadata.file_size,
            1 if metadata.is_public else 0,
            metadata.owner_address
        ))
        
        conn.commit()
        conn.close()
    
    def retrieve_document(self, cid: str) -> Dict:
        """
        CID๋กœ ๋ฌธ์„œ ์กฐํšŒ (๋ฉ”ํƒ€ CID ๋˜๋Š” ๋‚ด์šฉ CID ๋ชจ๋‘ ๊ฐ€๋Šฅ)
        """
        # ๋กœ์ปฌ ์ธ๋ฑ์Šค์—์„œ ๋จผ์ € ์ฐพ๊ธฐ
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        cursor.execute('''
            SELECT * FROM documents 
            WHERE meta_cid = ? OR content_cid = ?
        ''', (cid, cid))
        
        row = cursor.fetchone()
        conn.close()
        
        if not row:
            print(f"โš ๏ธ ์ธ๋ฑ์Šค์— ์—†์Œ. IPFS์—์„œ ์ง์ ‘ ์กฐํšŒ ์‹œ๋„...")
            # ์ธ๋ฑ์Šค์— ์—†์œผ๋ฉด IPFS์—์„œ ์ง์ ‘ ์ฝ๊ธฐ
            try:
                content = self.ipfs.cat(cid).decode('utf-8')
                return {'content': content, 'cid': cid, 'from_ipfs': True}
            except:
                raise ValueError(f"CID๋ฅผ ์ฐพ์„ ์ˆ˜ ์—†์Œ: {cid}")
        
        # ์ธ๋ฑ์Šค์—์„œ ์ฐพ์€ ๊ฒฝ์šฐ
        columns = ['id', 'meta_cid', 'content_cid', 'title', 'author', 
                   'description', 'tags', 'version', 'previous_cid',
                   'created_at', 'updated_at', 'file_type', 'file_size',
                   'is_public', 'owner_address']
        
        doc_info = dict(zip(columns, row))
        doc_info['tags'] = json.loads(doc_info['tags'])
        
        # ์‹ค์ œ ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๊ธฐ
        content = self.ipfs.cat(doc_info['content_cid']).decode('utf-8')
        doc_info['content'] = content
        
        return doc_info
    
    def search_documents(self, 
                         query: str = None,
                         author: str = None,
                         tags: List[str] = None) -> List[Dict]:
        """
        ๋ฌธ์„œ ๊ฒ€์ƒ‰ (์ œ๋ชฉ, ์ž‘์„ฑ์ž, ํƒœ๊ทธ ๊ธฐ๋ฐ˜)
        """
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        conditions = []
        params = []
        
        if query:
            conditions.append("(title LIKE ? OR description LIKE ?)")
            params.extend([f"%{query}%", f"%{query}%"])
        
        if author:
            conditions.append("author LIKE ?")
            params.append(f"%{author}%")
        
        if tags:
            for tag in tags:
                conditions.append("tags LIKE ?")
                params.append(f"%{tag}%")
        
        where_clause = " AND ".join(conditions) if conditions else "1=1"
        
        cursor.execute(f'''
            SELECT meta_cid, content_cid, title, author, description, 
                   tags, version, created_at, file_size
            FROM documents 
            WHERE {where_clause}
            ORDER BY updated_at DESC
        ''', params)
        
        rows = cursor.fetchall()
        conn.close()
        
        results = []
        for row in rows:
            results.append({
                'meta_cid': row[0],
                'content_cid': row[1],
                'title': row[2],
                'author': row[3],
                'description': row[4],
                'tags': json.loads(row[5]),
                'version': row[6],
                'created_at': row[7],
                'file_size': row[8],
                'gateway_url': f"https://ipfs.io/ipfs/{row[1]}"
            })
        
        return results
    
    def update_document(self, 
                        original_meta_cid: str,
                        new_content: str,
                        update_note: str = "") -> Dict:
        """
        ๋ฌธ์„œ ์—…๋ฐ์ดํŠธ (๋ฒ„์ „ ์ฒด์ธ ์œ ์ง€)
        ์ด์ „ ๋ฒ„์ „์€ IPFS์— ์˜๊ตฌ ๋ณด์กด๋จ!
        """
        # ๊ธฐ์กด ๋ฌธ์„œ ์กฐํšŒ
        old_doc = self.retrieve_document(original_meta_cid)
        
        # ์ƒˆ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ƒ์„ฑ (๋ฒ„์ „ ์ฆ๊ฐ€)
        new_metadata = DocumentMetadata(
            title=old_doc['title'],
            author=old_doc['author'],
            description=f"{old_doc['description']} | ์—…๋ฐ์ดํŠธ: {update_note}",
            tags=old_doc['tags'],
            content_cid="",  # store_document์—์„œ ์ฑ„์›Œ์ง
            version=old_doc['version'] + 1,
            previous_cid=original_meta_cid,  # ์ด์ „ ๋ฒ„์ „ ์—ฐ๊ฒฐ!
            file_type=old_doc['file_type'],
            owner_address=old_doc.get('owner_address')
        )
        
        return self.store_document(new_content, new_metadata)
    
    def get_version_history(self, meta_cid: str) -> List[Dict]:
        """
        ๋ฌธ์„œ์˜ ๋ฒ„์ „ ํžˆ์Šคํ† ๋ฆฌ ์กฐํšŒ (์ฒด์ธ ์—ญ์ถ”์ )
        """
        history = []
        current_cid = meta_cid
        
        while current_cid:
            conn = sqlite3.connect(self.db_path)
            cursor = conn.cursor()
            cursor.execute(
                'SELECT meta_cid, title, version, created_at, previous_cid FROM documents WHERE meta_cid = ?',
                (current_cid,)
            )
            row = cursor.fetchone()
            conn.close()
            
            if not row:
                break
            
            history.append({
                'meta_cid': row[0],
                'title': row[1],
                'version': row[2],
                'created_at': row[3]
            })
            
            current_cid = row[4]  # previous_cid๋กœ ์ด๋™
        
        return history
๐Ÿ’ก ๋ฒ„์ „ ์ฒด์ธ์˜ ํ•ต์‹ฌ! IPFS๋Š” ๋ถˆ๋ณ€(immutable)์ด์•ผ. ํ•œ๋ฒˆ ์˜ฌ๋ฆฐ ํŒŒ์ผ์€ ์ ˆ๋Œ€ ์ˆ˜์ • ๋ถˆ๊ฐ€.
๊ทธ๋ž˜์„œ ์—…๋ฐ์ดํŠธํ•  ๋•Œ๋งˆ๋‹ค ์ƒˆ CID๊ฐ€ ์ƒ๊ธฐ๊ณ , previous_cid๋กœ ์ด์ „ ๋ฒ„์ „์„ ์—ฐ๊ฒฐํ•˜๋ฉด
๋ธ”๋ก์ฒด์ธ์ฒ˜๋Ÿผ ๋ณ€๊ฒฝ ๋ถˆ๊ฐ€๋Šฅํ•œ ๋ฒ„์ „ ํžˆ์Šคํ† ๋ฆฌ๊ฐ€ ๋งŒ๋“ค์–ด์ ธ. ์™„์ „ ์ฉ”์ง€? ๐Ÿ˜Ž

๐Ÿ” ๋ฌธ์„œ ์•”ํ˜ธํ™” - ๋‚ด ํŒŒ์ผ์€ ๋‚ด๊ฐ€ ์ง€ํ‚จ๋‹ค

IPFS์— ์˜ฌ๋ฆฐ ํŒŒ์ผ์€ CID๋งŒ ์•Œ๋ฉด ๋ˆ„๊ตฌ๋‚˜ ๋ณผ ์ˆ˜ ์žˆ์–ด. ๊ณต๊ฐœ ํŒŒ์ผ์ด๋ฉด ์ƒ๊ด€์—†๋Š”๋ฐ,
๋ฏผ๊ฐํ•œ ๋ฌธ์„œ๋ผ๋ฉด? ์•”ํ˜ธํ™” ํ•„์ˆ˜์•ผ! ํŒŒ์ด์ฌ์˜ cryptography ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋กœ AES-256 ์•”ํ˜ธํ™” ๊ตฌํ˜„ํ•ด๋ณด์ž ๐Ÿ”’

Python - encryption.py
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.backends import default_backend
import os
import base64
import json

class DocumentEncryptor:
    """
    AES-256-GCM ๊ธฐ๋ฐ˜ ๋ฌธ์„œ ์•”ํ˜ธํ™”/๋ณตํ˜ธํ™”
    - AES-GCM: ์•”ํ˜ธํ™” + ๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ ๋™์‹œ์—!
    - PBKDF2: ํŒจ์Šค์›Œ๋“œ ๊ธฐ๋ฐ˜ ํ‚ค ์œ ๋„
    """
    
    def __init__(self):
        self.backend = default_backend()
    
    def _derive_key(self, password: str, salt: bytes) -> bytes:
        """
        ํŒจ์Šค์›Œ๋“œ์—์„œ 256๋น„ํŠธ ํ‚ค ์œ ๋„ (PBKDF2-SHA256)
        """
        kdf = PBKDF2HMAC(
            algorithm=hashes.SHA256(),
            length=32,          # 256๋น„ํŠธ
            salt=salt,
            iterations=100000,  # 10๋งŒ ๋ฒˆ ๋ฐ˜๋ณต (๋ธŒ๋ฃจํŠธํฌ์Šค ๋ฐฉ์–ด)
            backend=self.backend
        )
        return kdf.derive(password.encode('utf-8'))
    
    def encrypt(self, content: str, password: str) -> dict:
        """
        ๋ฌธ์„œ ๋‚ด์šฉ ์•”ํ˜ธํ™”
        
        Returns:
            {
                'encrypted_data': base64 ์ธ์ฝ”๋”ฉ๋œ ์•”ํ˜ธ๋ฌธ,
                'salt': base64 ์ธ์ฝ”๋”ฉ๋œ ์†”ํŠธ,
                'nonce': base64 ์ธ์ฝ”๋”ฉ๋œ ๋…ผ์Šค,
                'algorithm': 'AES-256-GCM'
            }
        """
        # ๋žœ๋ค ์†”ํŠธ์™€ ๋…ผ์Šค ์ƒ์„ฑ
        salt = os.urandom(16)   # 128๋น„ํŠธ ์†”ํŠธ
        nonce = os.urandom(12)  # 96๋น„ํŠธ ๋…ผ์Šค (GCM ํ‘œ์ค€)
        
        # ํ‚ค ์œ ๋„
        key = self._derive_key(password, salt)
        
        # AES-GCM ์•”ํ˜ธํ™”
        aesgcm = AESGCM(key)
        encrypted = aesgcm.encrypt(nonce, content.encode('utf-8'), None)
        
        return {
            'encrypted_data': base64.b64encode(encrypted).decode(),
            'salt': base64.b64encode(salt).decode(),
            'nonce': base64.b64encode(nonce).decode(),
            'algorithm': 'AES-256-GCM',
            'kdf': 'PBKDF2-SHA256',
            'iterations': 100000
        }
    
    def decrypt(self, encrypted_package: dict, password: str) -> str:
        """
        ์•”ํ˜ธํ™”๋œ ํŒจํ‚ค์ง€ ๋ณตํ˜ธํ™”
        """
        # base64 ๋””์ฝ”๋”ฉ
        encrypted_data = base64.b64decode(encrypted_package['encrypted_data'])
        salt = base64.b64decode(encrypted_package['salt'])
        nonce = base64.b64decode(encrypted_package['nonce'])
        
        # ํ‚ค ์œ ๋„
        key = self._derive_key(password, salt)
        
        # AES-GCM ๋ณตํ˜ธํ™” (๋ฌด๊ฒฐ์„ฑ ๊ฒ€์ฆ ํฌํ•จ)
        aesgcm = AESGCM(key)
        try:
            decrypted = aesgcm.decrypt(nonce, encrypted_data, None)
            return decrypted.decode('utf-8')
        except Exception:
            raise ValueError("โŒ ๋ณตํ˜ธํ™” ์‹คํŒจ! ํŒจ์Šค์›Œ๋“œ๊ฐ€ ํ‹€๋ ธ๊ฑฐ๋‚˜ ๋ฐ์ดํ„ฐ๊ฐ€ ๋ณ€์กฐ๋จ")
    
    def encrypt_to_json(self, content: str, password: str) -> str:
        """์•”ํ˜ธํ™” ๊ฒฐ๊ณผ๋ฅผ JSON ๋ฌธ์ž์—ด๋กœ ๋ฐ˜ํ™˜ (IPFS ์—…๋กœ๋“œ์šฉ)"""
        encrypted = self.encrypt(content, password)
        return json.dumps(encrypted, ensure_ascii=False)
    
    def decrypt_from_json(self, json_str: str, password: str) -> str:
        """JSON ๋ฌธ์ž์—ด์—์„œ ๋ณตํ˜ธํ™”"""
        encrypted_package = json.loads(json_str)
        return self.decrypt(encrypted_package, password)


# ์•”ํ˜ธํ™” + IPFS ํ†ตํ•ฉ ์‚ฌ์šฉ ์˜ˆ์‹œ
class EncryptedDocumentStore:
    """์•”ํ˜ธํ™”๋œ ๋ฌธ์„œ๋ฅผ IPFS์— ์ €์žฅํ•˜๋Š” ํ†ตํ•ฉ ํด๋ž˜์Šค"""
    
    def __init__(self, ipfs_client, encryptor: DocumentEncryptor):
        self.ipfs = ipfs_client
        self.encryptor = encryptor
    
    def store_encrypted(self, content: str, password: str, 
                        metadata: dict = None) -> dict:
        """
        ๋ฌธ์„œ๋ฅผ ์•”ํ˜ธํ™”ํ•ด์„œ IPFS์— ์ €์žฅ
        """
        print("๐Ÿ” ์•”ํ˜ธํ™” ์ค‘...")
        encrypted_json = self.encryptor.encrypt_to_json(content, password)
        
        print("๐Ÿ“ค IPFS ์—…๋กœ๋“œ ์ค‘...")
        import ipfshttpclient
        result = self.ipfs.add_bytes(encrypted_json.encode('utf-8'))
        cid = result
        
        print(f"โœ… ์•”ํ˜ธํ™” ์ €์žฅ ์™„๋ฃŒ!")
        print(f"   CID: {cid}")
        print(f"   โš ๏ธ  ์ด CID์˜ ๋‚ด์šฉ์€ ํŒจ์Šค์›Œ๋“œ ์—†์ด ์ฝ์„ ์ˆ˜ ์—†์Œ")
        
        return {
            'cid': cid,
            'encrypted': True,
            'algorithm': 'AES-256-GCM',
            'metadata': metadata
        }
    
    def retrieve_encrypted(self, cid: str, password: str) -> str:
        """
        IPFS์—์„œ ์•”ํ˜ธํ™”๋œ ๋ฌธ์„œ ๊ฐ€์ ธ์™€์„œ ๋ณตํ˜ธํ™”
        """
        print(f"๐Ÿ“ฅ IPFS์—์„œ ๊ฐ€์ ธ์˜ค๋Š” ์ค‘: {cid}")
        encrypted_json = self.ipfs.cat(cid).decode('utf-8')
        
        print("๐Ÿ”“ ๋ณตํ˜ธํ™” ์ค‘...")
        content = self.encryptor.decrypt_from_json(encrypted_json, password)
        
        print("โœ… ๋ณตํ˜ธํ™” ์™„๋ฃŒ!")
        return content
โš ๏ธ ํŒจ์Šค์›Œ๋“œ ๊ด€๋ฆฌ ์ฃผ์˜์‚ฌํ•ญ! IPFS๋Š” ํƒˆ์ค‘์•™ํ™”๋ผ ํŒจ์Šค์›Œ๋“œ ๋ถ„์‹ค ์‹œ ๋ณต๊ตฌ ๋ฐฉ๋ฒ•์ด ์—†์–ด.
์‹ค์ œ ์„œ๋น„์Šค๋ผ๋ฉด ๋น„๋Œ€์นญ ์•”ํ˜ธํ™”(RSA/ECDH)๋‚˜ ์ž„๊ณ„๊ฐ’ ์•”ํ˜ธํ™”(Threshold Encryption)๋ฅผ ๊ณ ๋ คํ•ด๋ด.
๋˜๋Š” ์ด๋”๋ฆฌ์›€ ์ง€๊ฐ‘์˜ ๊ฐœ์ธํ‚ค๋กœ ์•”ํ˜ธํ™”ํ•˜๋Š” ๋ฐฉ๋ฒ•๋„ Web3 ์ƒํƒœ๊ณ„์—์„œ ๋งŽ์ด ์จ!

๐Ÿ“Œ Pinata๋กœ ์˜๊ตฌ ๋ณด์กด - ๋กœ์ปฌ ๋…ธ๋“œ๋งŒ ๋ฏฟ์œผ๋ฉด ์•ˆ ๋ผ

๋กœ์ปฌ IPFS ๋…ธ๋“œ์—๋งŒ ํŒŒ์ผ ์˜ฌ๋ฆฌ๋ฉด ๋‚ด ์ปดํ“จํ„ฐ ๊บผ์ง€๋ฉด ํŒŒ์ผ ์ ‘๊ทผ ๋ถˆ๊ฐ€์•ผ ใ…‹ใ…‹ใ…‹
๊ทธ๋ž˜์„œ Pinning ์„œ๋น„์Šค๋ฅผ ์จ์•ผ ํ•ด. ๋Œ€ํ‘œ์ ์ธ ๊ฒŒ Pinata์ธ๋ฐ, ๋ฌด๋ฃŒ ํ”Œ๋žœ๋„ ์žˆ์–ด์„œ ํ…Œ์ŠคํŠธํ•˜๊ธฐ ์ข‹์•„.
Pinata๊ฐ€ 24/7 ํŒŒ์ผ์„ ํ•€ํ•ด์ค˜์„œ ํ•ญ์ƒ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๊ฒŒ ํ•ด์คŒ ๐ŸŒ

Python - pinata_client.py
import requests
import json
import os
from typing import Optional

class PinataClient:
    """
    Pinata IPFS Pinning ์„œ๋น„์Šค ํด๋ผ์ด์–ธํŠธ
    https://pinata.cloud - ๋ฌด๋ฃŒ 1GB๊นŒ์ง€!
    """
    
    BASE_URL = "https://api.pinata.cloud"
    GATEWAY_URL = "https://gateway.pinata.cloud/ipfs"
    
    def __init__(self, api_key: str, api_secret: str):
        """
        Pinata API ํ‚ค ์„ค์ •
        pinata.cloud์—์„œ ๋ฐœ๊ธ‰๋ฐ›์•„์•ผ ํ•ด!
        """
        self.headers = {
            'pinata_api_key': api_key,
            'pinata_secret_api_key': api_secret
        }
        
        # ์—ฐ๊ฒฐ ํ…Œ์ŠคํŠธ
        self._test_connection()
    
    def _test_connection(self):
        """API ์—ฐ๊ฒฐ ํ…Œ์ŠคํŠธ"""
        response = requests.get(
            f"{self.BASE_URL}/data/testAuthentication",
            headers=self.headers
        )
        if response.status_code == 200:
            print("โœ… Pinata ์—ฐ๊ฒฐ ์„ฑ๊ณต!")
        else:
            raise ConnectionError(f"Pinata ์—ฐ๊ฒฐ ์‹คํŒจ: {response.text}")
    
    def pin_file(self, file_path: str, 
                 name: str = None,
                 metadata: dict = None) -> dict:
        """
        ํŒŒ์ผ์„ Pinata์— ์ง์ ‘ ์—…๋กœ๋“œ & ํ•€
        """
        if not os.path.exists(file_path):
            raise FileNotFoundError(f"ํŒŒ์ผ ์—†์Œ: {file_path}")
        
        filename = name or os.path.basename(file_path)
        
        # ํ•€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์„ค์ •
        pin_options = {
            'pinataMetadata': {
                'name': filename,
                'keyvalues': metadata or {}
            },
            'pinataOptions': {
                'cidVersion': 1  # CIDv1 ์‚ฌ์šฉ (์ตœ์‹  ๋ฒ„์ „)
            }
        }
        
        with open(file_path, 'rb') as f:
            files = {
                'file': (filename, f),
                'pinataOptions': (None, json.dumps(pin_options['pinataOptions'])),
                'pinataMetadata': (None, json.dumps(pin_options['pinataMetadata']))
            }
            
            response = requests.post(
                f"{self.BASE_URL}/pinning/pinFileToIPFS",
                files=files,
                headers={k: v for k, v in self.headers.items()}
            )
        
        if response.status_code == 200:
            result = response.json()
            return {
                'cid': result['IpfsHash'],
                'size': result['PinSize'],
                'timestamp': result['Timestamp'],
                'gateway_url': f"{self.GATEWAY_URL}/{result['IpfsHash']}",
                'public_url': f"https://ipfs.io/ipfs/{result['IpfsHash']}"
            }
        else:
            raise Exception(f"์—…๋กœ๋“œ ์‹คํŒจ: {response.text}")
    
    def pin_json(self, data: dict, name: str = "document") -> dict:
        """
        JSON ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ Pinata์— ํ•€
        ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ €์žฅ์— ์ตœ์ !
        """
        payload = {
            'pinataContent': data,
            'pinataMetadata': {'name': name},
            'pinataOptions': {'cidVersion': 1}
        }
        
        response = requests.post(
            f"{self.BASE_URL}/pinning/pinJSONToIPFS",
            json=payload,
            headers={**self.headers, 'Content-Type': 'application/json'}
        )
        
        if response.status_code == 200:
            result = response.json()
            return {
                'cid': result['IpfsHash'],
                'gateway_url': f"{self.GATEWAY_URL}/{result['IpfsHash']}"
            }
        else:
            raise Exception(f"JSON ํ•€ ์‹คํŒจ: {response.text}")
    
    def pin_by_cid(self, cid: str, name: str = "") -> dict:
        """
        ์ด๋ฏธ IPFS์— ์žˆ๋Š” CID๋ฅผ Pinata๋กœ ํ•€
        ๋กœ์ปฌ ๋…ธ๋“œ์—์„œ ์˜ฌ๋ฆฐ ํŒŒ์ผ์„ Pinata๋กœ ์˜๊ตฌ ๋ณด์กดํ•  ๋•Œ!
        """
        payload = {
            'hashToPin': cid,
            'pinataMetadata': {'name': name or cid}
        }
        
        response = requests.post(
            f"{self.BASE_URL}/pinning/pinByHash",
            json=payload,
            headers={**self.headers, 'Content-Type': 'application/json'}
        )
        
        if response.status_code == 200:
            print(f"๐Ÿ“Œ Pinata ํ•€ ์™„๋ฃŒ: {cid}")
            return response.json()
        else:
            raise Exception(f"ํ•€ ์‹คํŒจ: {response.text}")
    
    def list_pins(self, status: str = "pinned") -> list:
        """ํ•€๋œ ํŒŒ์ผ ๋ชฉ๋ก ์กฐํšŒ"""
        response = requests.get(
            f"{self.BASE_URL}/data/pinList?status={status}",
            headers=self.headers
        )
        
        if response.status_code == 200:
            data = response.json()
            return data.get('rows', [])
        return []
    
    def unpin(self, cid: str) -> bool:
        """ํ•€ ํ•ด์ œ (์ €์žฅ ๊ณต๊ฐ„ ์ ˆ์•ฝ)"""
        response = requests.delete(
            f"{self.BASE_URL}/pinning/unpin/{cid}",
            headers=self.headers
        )
        return response.status_code == 200
    
    def get_usage(self) -> dict:
        """์‚ฌ์šฉ๋Ÿ‰ ์กฐํšŒ"""
        response = requests.get(
            f"{self.BASE_URL}/data/userPinnedDataTotal",
            headers=self.headers
        )
        if response.status_code == 200:
            data = response.json()
            return {
                'pin_count': data['pin_count'],
                'pin_size_total': data['pin_size_total'],
                'pin_size_with_replications': data['pin_size_with_replications_total']
            }
        return {}


# ํ™˜๊ฒฝ๋ณ€์ˆ˜๋กœ API ํ‚ค ๊ด€๋ฆฌ (๋ณด์•ˆ!)
# .env ํŒŒ์ผ์— ์ €์žฅ:
# PINATA_API_KEY=your_api_key
# PINATA_API_SECRET=your_api_secret

from dotenv import load_dotenv
load_dotenv()

def create_pinata_client() -> PinataClient:
    api_key = os.getenv('PINATA_API_KEY')
    api_secret = os.getenv('PINATA_API_SECRET')
    
    if not api_key or not api_secret:
        raise ValueError("Pinata API ํ‚ค๊ฐ€ ์„ค์ •๋˜์ง€ ์•Š์Œ! .env ํŒŒ์ผ ํ™•์ธํ•ด")
    
    return PinataClient(api_key, api_secret)
โœ… Pinata ๋ฌด๋ฃŒ ํ”Œ๋žœ ์ŠคํŽ™ (2024 ๊ธฐ์ค€) ์ €์žฅ ์šฉ๋Ÿ‰: 1GB
์›”๊ฐ„ ๊ฒŒ์ดํŠธ์›จ์ด ์š”์ฒญ: 100GB
ํ•€ ๊ฐœ์ˆ˜: ๋ฌด์ œํ•œ
๊ฐœ์ธ ํ”„๋กœ์ ํŠธ๋‚˜ ํ…Œ์ŠคํŠธ์šฉ์œผ๋กœ๋Š” ์ถฉ๋ถ„ํ•ด! ์‹ค์ œ ์„œ๋น„์Šค๋Š” ์œ ๋ฃŒ ํ”Œ๋žœ ๊ณ ๋ คํ•ด๋ด.

๐Ÿš€ ์ „์ฒด ํ†ตํ•ฉ ์‹œ์Šคํ…œ - ๋‹ค ํ•ฉ์ณ๋ณด์ž

์ž ์ด์ œ ์ง€๊ธˆ๊นŒ์ง€ ๋งŒ๋“  ๋ชจ๋“  ์ปดํฌ๋„ŒํŠธ๋ฅผ ํ•˜๋‚˜๋กœ ํ•ฉ์ณ์„œ ์™„์„ฑ๋œ ์‹œ์Šคํ…œ์„ ๋งŒ๋“ค์–ด๋ณด์ž!
์žฌ๋Šฅ๋„ท ๊ฐ™์€ ํ”Œ๋žซํผ์—์„œ ์‚ฌ์šฉ์ž๋“ค์ด ์ž์‹ ์˜ ํฌํŠธํด๋ฆฌ์˜ค๋‚˜ ์ž‘์—…๋ฌผ์„ ํƒˆ์ค‘์•™ํ™” ๋ฐฉ์‹์œผ๋กœ ์ €์žฅํ•  ๋•Œ ์ด๋Ÿฐ ๊ตฌ์กฐ๋ฅผ ์“ธ ์ˆ˜ ์žˆ์–ด ๐Ÿ˜Š

Python - main_system.py
"""
ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ - ๋ฉ”์ธ ํ†ตํ•ฉ ๋ชจ๋“ˆ
Python + IPFS + Pinata + ์•”ํ˜ธํ™” ์™„์ „์ฒด
"""

import os
import json
from datetime import datetime
from typing import Optional, List

# ์•ž์„œ ๋งŒ๋“  ๋ชจ๋“ˆ๋“ค ์ž„ํฌํŠธ
# from ipfs_basic import IPFSClient
# from document_store import DecentralizedDocumentStore
# from document_schema import DocumentMetadata
# from encryption import DocumentEncryptor
# from pinata_client import PinataClient, create_pinata_client

class TotalDocumentSystem:
    """
    ์™„์ „ํ•œ ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ
    ๋กœ์ปฌ IPFS + Pinata ์ด์ค‘ ๋ณด์กด + ์„ ํƒ์  ์•”ํ˜ธํ™”
    """
    
    def __init__(self, use_pinata: bool = True, use_encryption: bool = False):
        print("๐Ÿš€ ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์‹œ์Šคํ…œ ์ดˆ๊ธฐํ™” ์ค‘...")
        
        # ๋กœ์ปฌ IPFS ์—ฐ๊ฒฐ
        import ipfshttpclient
        self.ipfs = ipfshttpclient.connect('/ip4/127.0.0.1/tcp/5001')
        
        # Pinata ํด๋ผ์ด์–ธํŠธ (์„ ํƒ)
        self.pinata = None
        if use_pinata:
            try:
                self.pinata = create_pinata_client()
                print("๐Ÿ“Œ Pinata ์—ฐ๊ฒฐ๋จ - ์ด์ค‘ ๋ณด์กด ํ™œ์„ฑํ™”")
            except Exception as e:
                print(f"โš ๏ธ Pinata ์—ฐ๊ฒฐ ์‹คํŒจ (๋กœ์ปฌ๋งŒ ์‚ฌ์šฉ): {e}")
        
        # ์•”ํ˜ธํ™” ๋ชจ๋“ˆ (์„ ํƒ)
        self.encryptor = None
        if use_encryption:
            self.encryptor = DocumentEncryptor()
            print("๐Ÿ” ์•”ํ˜ธํ™” ๋ชจ๋“ˆ ํ™œ์„ฑํ™”")
        
        # ๋กœ์ปฌ ์ธ๋ฑ์Šค DB
        self.db_path = "documents.db"
        self._init_db()
        
        print("โœ… ์‹œ์Šคํ…œ ์ค€๋น„ ์™„๋ฃŒ!\n")
    
    def _init_db(self):
        """SQLite ์ธ๋ฑ์Šค ์ดˆ๊ธฐํ™”"""
        import sqlite3
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        cursor.execute('''
            CREATE TABLE IF NOT EXISTS doc_registry (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                doc_id TEXT UNIQUE,
                title TEXT,
                author TEXT,
                content_cid TEXT,
                meta_cid TEXT,
                is_encrypted INTEGER DEFAULT 0,
                is_pinned INTEGER DEFAULT 0,
                created_at TEXT,
                tags TEXT
            )
        ''')
        conn.commit()
        conn.close()
    
    def save_document(self,
                      title: str,
                      content: str,
                      author: str,
                      tags: List[str] = None,
                      password: Optional[str] = None) -> dict:
        """
        ๋ฌธ์„œ ์ €์žฅ ๋ฉ”์ธ ํ•จ์ˆ˜
        
        Args:
            title: ๋ฌธ์„œ ์ œ๋ชฉ
            content: ๋ฌธ์„œ ๋‚ด์šฉ
            author: ์ž‘์„ฑ์ž
            tags: ํƒœ๊ทธ ๋ชฉ๋ก
            password: ์•”ํ˜ธํ™” ํŒจ์Šค์›Œ๋“œ (None์ด๋ฉด ํ‰๋ฌธ ์ €์žฅ)
        
        Returns:
            ์ €์žฅ ๊ฒฐ๊ณผ ๋”•์…”๋„ˆ๋ฆฌ
        """
        print(f"๐Ÿ“ ๋ฌธ์„œ ์ €์žฅ: '{title}'")
        print(f"   ์ž‘์„ฑ์ž: {author}")
        print(f"   ํฌ๊ธฐ: {len(content):,} ๊ธ€์ž")
        
        # 1. ์•”ํ˜ธํ™” (ํŒจ์Šค์›Œ๋“œ ์žˆ์„ ๋•Œ)
        if password and self.encryptor:
            print("๐Ÿ” ์•”ํ˜ธํ™” ์ ์šฉ ์ค‘...")
            upload_content = self.encryptor.encrypt_to_json(content, password)
            is_encrypted = True
        else:
            upload_content = content
            is_encrypted = False
        
        # 2. IPFS์— ๋‚ด์šฉ ์—…๋กœ๋“œ
        content_cid = self.ipfs.add_bytes(upload_content.encode('utf-8'))
        print(f"๐Ÿ“ค ๋‚ด์šฉ CID: {content_cid}")
        
        # 3. ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ƒ์„ฑ ๋ฐ ์—…๋กœ๋“œ
        doc_id = f"doc_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
        metadata = {
            'doc_id': doc_id,
            'title': title,
            'author': author,
            'tags': tags or [],
            'content_cid': content_cid,
            'is_encrypted': is_encrypted,
            'created_at': datetime.now().isoformat(),
            'system': 'TotalDocumentSystem v1.0'
        }
        
        meta_json = json.dumps(metadata, ensure_ascii=False)
        meta_cid = self.ipfs.add_bytes(meta_json.encode('utf-8'))
        print(f"๐Ÿ“‹ ๋ฉ”ํƒ€ CID: {meta_cid}")
        
        # 4. ๋กœ์ปฌ ํ•€
        self.ipfs.pin.add(content_cid)
        self.ipfs.pin.add(meta_cid)
        print("๐Ÿ“Œ ๋กœ์ปฌ ํ•€ ์™„๋ฃŒ")
        
        # 5. Pinata ์ด์ค‘ ๋ณด์กด
        is_pinned = False
        if self.pinata:
            try:
                self.pinata.pin_by_cid(content_cid, f"{title}_content")
                self.pinata.pin_by_cid(meta_cid, f"{title}_meta")
                is_pinned = True
                print("โ˜๏ธ Pinata ์ด์ค‘ ๋ณด์กด ์™„๋ฃŒ")
            except Exception as e:
                print(f"โš ๏ธ Pinata ํ•€ ์‹คํŒจ (๋กœ์ปฌ๋งŒ ๋ณด์กด): {e}")
        
        # 6. ๋กœ์ปฌ ์ธ๋ฑ์Šค ๋“ฑ๋ก
        import sqlite3
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        cursor.execute('''
            INSERT OR REPLACE INTO doc_registry
            (doc_id, title, author, content_cid, meta_cid, 
             is_encrypted, is_pinned, created_at, tags)
            VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
        ''', (
            doc_id, title, author, content_cid, meta_cid,
            1 if is_encrypted else 0,
            1 if is_pinned else 0,
            datetime.now().isoformat(),
            json.dumps(tags or [])
        ))
        conn.commit()
        conn.close()
        
        result = {
            'success': True,
            'doc_id': doc_id,
            'content_cid': content_cid,
            'meta_cid': meta_cid,
            'is_encrypted': is_encrypted,
            'is_pinned': is_pinned,
            'gateway_url': f"https://ipfs.io/ipfs/{content_cid}",
            'pinata_url': f"https://gateway.pinata.cloud/ipfs/{content_cid}"
        }
        
        print(f"\n๐ŸŽ‰ ์ €์žฅ ์™„๋ฃŒ!")
        print(f"   ๋ฌธ์„œ ID: {doc_id}")
        print(f"   ๊ฒŒ์ดํŠธ์›จ์ด: {result['gateway_url']}")
        
        return result
    
    def load_document(self, 
                      cid: str, 
                      password: Optional[str] = None) -> dict:
        """
        CID๋กœ ๋ฌธ์„œ ๋กœ๋“œ
        """
        print(f"๐Ÿ“ฅ ๋ฌธ์„œ ๋กœ๋“œ: {cid}")
        
        # IPFS์—์„œ ๋‚ด์šฉ ๊ฐ€์ ธ์˜ค๊ธฐ
        raw_content = self.ipfs.cat(cid).decode('utf-8')
        
        # ์•”ํ˜ธํ™” ์—ฌ๋ถ€ ํ™•์ธ ๋ฐ ๋ณตํ˜ธํ™”
        try:
            # JSON ํŒŒ์‹ฑ ์‹œ๋„ (์•”ํ˜ธํ™”๋œ ํŒจํ‚ค์ง€์ธ์ง€ ํ™•์ธ)
            parsed = json.loads(raw_content)
            if 'encrypted_data' in parsed and 'algorithm' in parsed:
                # ์•”ํ˜ธํ™”๋œ ๋ฌธ์„œ
                if not password:
                    raise ValueError("๐Ÿ” ์ด ๋ฌธ์„œ๋Š” ์•”ํ˜ธํ™”๋จ. ํŒจ์Šค์›Œ๋“œ ํ•„์š”!")
                if not self.encryptor:
                    self.encryptor = DocumentEncryptor()
                content = self.encryptor.decrypt_from_json(raw_content, password)
                print("๐Ÿ”“ ๋ณตํ˜ธํ™” ์™„๋ฃŒ")
            else:
                content = raw_content
        except json.JSONDecodeError:
            # ์ผ๋ฐ˜ ํ…์ŠคํŠธ
            content = raw_content
        
        return {
            'cid': cid,
            'content': content,
            'length': len(content)
        }
    
    def list_documents(self, author: str = None) -> list:
        """์ €์žฅ๋œ ๋ฌธ์„œ ๋ชฉ๋ก"""
        import sqlite3
        conn = sqlite3.connect(self.db_path)
        cursor = conn.cursor()
        
        if author:
            cursor.execute(
                'SELECT doc_id, title, author, content_cid, created_at, tags FROM doc_registry WHERE author = ? ORDER BY created_at DESC',
                (author,)
            )
        else:
            cursor.execute(
                'SELECT doc_id, title, author, content_cid, created_at, tags FROM doc_registry ORDER BY created_at DESC'
            )
        
        rows = cursor.fetchall()
        conn.close()
        
        return [{
            'doc_id': r[0],
            'title': r[1],
            'author': r[2],
            'content_cid': r[3],
            'created_at': r[4],
            'tags': json.loads(r[5]),
            'gateway_url': f"https://ipfs.io/ipfs/{r[3]}"
        } for r in rows]


# ===== ์‹ค์ œ ์‚ฌ์šฉ ์˜ˆ์‹œ =====
if __name__ == "__main__":
    
    # ์‹œ์Šคํ…œ ์ดˆ๊ธฐํ™”
    system = TotalDocumentSystem(
        use_pinata=False,      # Pinata API ํ‚ค ์—†์œผ๋ฉด False
        use_encryption=True    # ์•”ํ˜ธํ™” ํ™œ์„ฑํ™”
    )
    
    # 1. ์ผ๋ฐ˜ ๋ฌธ์„œ ์ €์žฅ
    result1 = system.save_document(
        title="ํŒŒ์ด์ฌ IPFS ํŠœํ† ๋ฆฌ์–ผ",
        content="์ด ๋ฌธ์„œ๋Š” ํƒˆ์ค‘์•™ํ™” ์ €์žฅ ์‹œ์Šคํ…œ์— ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ๊ฒ€์—ด ๋ถˆ๊ฐ€!",
        author="๊ฐœ๋ฐœ์ž๊น€์”จ",
        tags=["python", "ipfs", "web3", "tutorial"]
    )
    
    print("\n" + "="*50)
    
    # 2. ์•”ํ˜ธํ™” ๋ฌธ์„œ ์ €์žฅ
    result2 = system.save_document(
        title="๋น„๋ฐ€ ํ”„๋กœ์ ํŠธ ๊ณ„ํš์„œ",
        content="์ด ๋‚ด์šฉ์€ ์•”ํ˜ธํ™”๋˜์–ด ์ €์žฅ๋ฉ๋‹ˆ๋‹ค. ํŒจ์Šค์›Œ๋“œ ์—†์ด๋Š” ์ฝ๊ธฐ ๋ถˆ๊ฐ€!",
        author="๊ฐœ๋ฐœ์ž๊น€์”จ",
        tags=["secret", "encrypted"],
        password="my_super_secret_password_123"
    )
    
    print("\n" + "="*50)
    
    # 3. ๋ฌธ์„œ ๋ชฉ๋ก ์กฐํšŒ
    docs = system.list_documents()
    print(f"\n๐Ÿ“š ์ €์žฅ๋œ ๋ฌธ์„œ ์ˆ˜: {len(docs)}๊ฐœ")
    for doc in docs:
        print(f"   - [{doc['doc_id']}] {doc['title']} by {doc['author']}")
    
    print("\n" + "="*50)
    
    # 4. ๋ฌธ์„œ ๋กœ๋“œ
    loaded = system.load_document(result1['content_cid'])
    print(f"\n๐Ÿ“– ๋กœ๋“œ๋œ ๋‚ด์šฉ: {loaded['content']}")
    
    # 5. ์•”ํ˜ธํ™” ๋ฌธ์„œ ๋กœ๋“œ
    loaded_enc = system.load_document(
        result2['content_cid'],
        password="my_super_secret_password_123"
    )
    print(f"๐Ÿ”“ ๋ณตํ˜ธํ™”๋œ ๋‚ด์šฉ: {loaded_enc['content']}")

๐Ÿ“Š ๋ฌธ์„œ ์ €์žฅ ๋ฐ์ดํ„ฐ ํ๋ฆ„ ์‚ฌ์šฉ์ž ๋ฌธ์„œ ์ž…๋ ฅ โœ๏ธ ์•”ํ˜ธํ™” AES-256-GCM ๐Ÿ” IPFS ์—…๋กœ๋“œ CID ์ƒ์„ฑ ๐Ÿ“ค ํ•€(Pin) ์˜๊ตฌ ๋ณด์กด ๐Ÿ“Œ ์ธ๋ฑ์Šค ๋“ฑ๋ก SQLite DB ๐Ÿ“Š ๋กœ์ปฌ IPFS ๋…ธ๋“œ 127.0.0.1:5001 Pinata ํด๋ผ์šฐ๋“œ 24/7 ์˜๊ตฌ ๋ณด์กด Web3.Storage Filecoin ๋ฐฑ์—… ๐ŸŒ ์ ‘๊ทผ ๋ฐฉ๋ฒ• ipfs://QmXxx... https://ipfs.io/ipfs/QmXxx... https://gateway.pinata.cloud/ipfs/QmXxx...

๐Ÿ’ป CLI ๋„๊ตฌ ๋งŒ๋“ค๊ธฐ - ํ„ฐ๋ฏธ๋„์—์„œ ๋ฐ”๋กœ ์“ฐ์ž

๋งค๋ฒˆ ํŒŒ์ด์ฌ ์Šคํฌ๋ฆฝํŠธ ์ง์ ‘ ์‹คํ–‰ํ•˜๊ธฐ ๊ท€์ฐฎ์ž–์•„? CLI ๋„๊ตฌ๋กœ ๋งŒ๋“ค๋ฉด ํ„ฐ๋ฏธ๋„์—์„œ ๋ช…๋ น์–ด ํ•˜๋‚˜๋กœ ๋๋‚ผ ์ˆ˜ ์žˆ์–ด ใ…‹ใ…‹
argparse ์จ์„œ ๊น”๋”ํ•˜๊ฒŒ ๋งŒ๋“ค์–ด๋ณด์ž!

Python - cli.py
#!/usr/bin/env python3
"""
ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ CLI ๋„๊ตฌ
์‚ฌ์šฉ๋ฒ•: python cli.py [command] [options]
"""

import argparse
import sys
import json
import os

def cmd_save(args):
    """๋ฌธ์„œ ์ €์žฅ ๋ช…๋ น"""
    system = TotalDocumentSystem(
        use_pinata=args.pinata,
        use_encryption=bool(args.password)
    )
    
    # ํŒŒ์ผ์—์„œ ์ฝ๊ธฐ ๋˜๋Š” ์ง์ ‘ ์ž…๋ ฅ
    if args.file:
        with open(args.file, 'r', encoding='utf-8') as f:
            content = f.read()
    else:
        print("๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜์„ธ์š” (Ctrl+D๋กœ ์™„๋ฃŒ):")
        content = sys.stdin.read()
    
    tags = args.tags.split(',') if args.tags else []
    
    result = system.save_document(
        title=args.title,
        content=content,
        author=args.author,
        tags=tags,
        password=args.password
    )
    
    print(f"\nโœ… ์ €์žฅ ์™„๋ฃŒ!")
    print(f"CID: {result['content_cid']}")
    print(f"URL: {result['gateway_url']}")

def cmd_load(args):
    """๋ฌธ์„œ ๋กœ๋“œ ๋ช…๋ น"""
    system = TotalDocumentSystem(use_encryption=True)
    result = system.load_document(args.cid, password=args.password)
    
    if args.output:
        with open(args.output, 'w', encoding='utf-8') as f:
            f.write(result['content'])
        print(f"โœ… ์ €์žฅ๋จ: {args.output}")
    else:
        print(result['content'])

def cmd_list(args):
    """๋ฌธ์„œ ๋ชฉ๋ก ๋ช…๋ น"""
    system = TotalDocumentSystem()
    docs = system.list_documents(author=args.author)
    
    if not docs:
        print("๐Ÿ“ญ ์ €์žฅ๋œ ๋ฌธ์„œ ์—†์Œ")
        return
    
    print(f"\n๐Ÿ“š ๋ฌธ์„œ ๋ชฉ๋ก ({len(docs)}๊ฐœ):")
    print("-" * 60)
    for doc in docs:
        print(f"์ œ๋ชฉ: {doc['title']}")
        print(f"์ž‘์„ฑ์ž: {doc['author']}")
        print(f"CID: {doc['content_cid'][:20]}...")
        print(f"๋‚ ์งœ: {doc['created_at'][:10]}")
        print(f"ํƒœ๊ทธ: {', '.join(doc['tags'])}")
        print("-" * 60)

def main():
    parser = argparse.ArgumentParser(
        description='๐Ÿ”— ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ',
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
์‚ฌ์šฉ ์˜ˆ์‹œ:
  python cli.py save -t "์ œ๋ชฉ" -a "์ž‘์„ฑ์ž" -f document.txt
  python cli.py save -t "์ œ๋ชฉ" -a "์ž‘์„ฑ์ž" -f doc.txt -p "ํŒจ์Šค์›Œ๋“œ"
  python cli.py load QmXxx...
  python cli.py load QmXxx... -p "ํŒจ์Šค์›Œ๋“œ" -o output.txt
  python cli.py list
  python cli.py list -a "์ž‘์„ฑ์ž๋ช…"
        """
    )
    
    subparsers = parser.add_subparsers(dest='command')
    
    # save ๋ช…๋ น
    save_parser = subparsers.add_parser('save', help='๋ฌธ์„œ ์ €์žฅ')
    save_parser.add_argument('-t', '--title', required=True, help='๋ฌธ์„œ ์ œ๋ชฉ')
    save_parser.add_argument('-a', '--author', required=True, help='์ž‘์„ฑ์ž')
    save_parser.add_argument('-f', '--file', help='ํŒŒ์ผ ๊ฒฝ๋กœ')
    save_parser.add_argument('--tags', help='ํƒœ๊ทธ (์‰ผํ‘œ ๊ตฌ๋ถ„)')
    save_parser.add_argument('-p', '--password', help='์•”ํ˜ธํ™” ํŒจ์Šค์›Œ๋“œ')
    save_parser.add_argument('--pinata', action='store_true', help='Pinata ์ด์ค‘ ๋ณด์กด')
    
    # load ๋ช…๋ น
    load_parser = subparsers.add_parser('load', help='๋ฌธ์„œ ๋กœ๋“œ')
    load_parser.add_argument('cid', help='IPFS CID')
    load_parser.add_argument('-p', '--password', help='๋ณตํ˜ธํ™” ํŒจ์Šค์›Œ๋“œ')
    load_parser.add_argument('-o', '--output', help='์ถœ๋ ฅ ํŒŒ์ผ ๊ฒฝ๋กœ')
    
    # list ๋ช…๋ น
    list_parser = subparsers.add_parser('list', help='๋ฌธ์„œ ๋ชฉ๋ก')
    list_parser.add_argument('-a', '--author', help='์ž‘์„ฑ์ž ํ•„ํ„ฐ')
    
    args = parser.parse_args()
    
    if args.command == 'save':
        cmd_save(args)
    elif args.command == 'load':
        cmd_load(args)
    elif args.command == 'list':
        cmd_list(args)
    else:
        parser.print_help()

if __name__ == '__main__':
    main()
๐Ÿ’ก CLI ์‚ฌ์šฉ ์˜ˆ์‹œ
python cli.py save -t "๋‚ด ํฌํŠธํด๋ฆฌ์˜ค" -a "ํ™๊ธธ๋™" -f portfolio.pdf --pinata

python cli.py load QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG

python cli.py list -a "ํ™๊ธธ๋™"

โšก ์‹ค์ „ ํŒ & ์ž์ฃผ ํ•˜๋Š” ์‹ค์ˆ˜๋“ค

์ฝ”๋“œ ์งœ๋‹ค ๋ณด๋ฉด ๋ฐ˜๋“œ์‹œ ๋งˆ์ฃผ์น˜๋Š” ๋ฌธ์ œ๋“ค์ด ์žˆ์–ด. ๋ฏธ๋ฆฌ ์•Œ๊ณ  ๊ฐ€๋ฉด ์‹œ๊ฐ„ ์ ˆ์•ฝ ์—„์ฒญ ๋ผ ๐Ÿ˜ค

๐Ÿšจ ์ž์ฃผ ํ•˜๋Š” ์‹ค์ˆ˜ TOP 5
1
ํ•€(Pin) ์•ˆ ํ•˜๊ณ  ํŒŒ์ผ ์˜ฌ๋ฆฌ๊ธฐ
IPFS ๊ฐ€๋น„์ง€ ์ปฌ๋ ‰์…˜์ด ๋Œ๋ฉด ํ•€ ์•ˆ ๋œ ํŒŒ์ผ์€ ์‚ญ์ œ๋ผ. ์—…๋กœ๋“œ ํ›„ ๋ฐ˜๋“œ์‹œ ipfs.pin.add(cid) ํ˜ธ์ถœํ•ด์•ผ ํ•ด!
Pinata ๊ฐ™์€ ์™ธ๋ถ€ ํ•€๋‹ ์„œ๋น„์Šค๋„ ๊ฐ™์ด ์“ฐ๋ฉด ๋” ์•ˆ์ „.
2
CID๋ฅผ ์žƒ์–ด๋ฒ„๋ฆฌ๊ธฐ
IPFS๋Š” CID๊ฐ€ ์—†์œผ๋ฉด ํŒŒ์ผ ์ฐพ์„ ๋ฐฉ๋ฒ•์ด ์—†์–ด. ๋ฐ˜๋“œ์‹œ ๋กœ์ปฌ DB๋‚˜ ๋ธ”๋ก์ฒด์ธ์— CID๋ฅผ ๊ธฐ๋กํ•ด๋‘ฌ์•ผ ํ•ด.
SQLite ์ธ๋ฑ์Šค + ๋ธ”๋ก์ฒด์ธ ์˜จ์ฒด์ธ ๊ธฐ๋ก ์ด์ค‘์œผ๋กœ ํ•˜๋Š” ๊ฒŒ ๋ฒ ์ŠคํŠธ.
3
๋Œ€์šฉ๋Ÿ‰ ํŒŒ์ผ ํ•œ ๋ฒˆ์— ์˜ฌ๋ฆฌ๊ธฐ
IPFS๋Š” 256KB ์ฒญํฌ๋กœ ์ž๋™ ๋ถ„ํ• ํ•˜์ง€๋งŒ, ๋„ˆ๋ฌด ํฐ ํŒŒ์ผ์€ ์—…๋กœ๋“œ ์‹œ๊ฐ„์ด ๊ธธ์–ด.
100MB ์ด์ƒ์ด๋ฉด ipfs.add(file, chunker='size-1048576')๋กœ ์ฒญํฌ ํฌ๊ธฐ ์กฐ์ ˆํ•˜๊ฑฐ๋‚˜ ์ŠคํŠธ๋ฆฌ๋ฐ ์—…๋กœ๋“œ ์จ.
4
IPFS ๋ฐ๋ชฌ ์•ˆ ์ผœ๊ณ  ์—ฐ๊ฒฐ ์‹œ๋„
๋‹น์—ฐํ•œ ๋ง์ด์ง€๋งŒ ipfs daemon์ด ์‹คํ–‰ ์ค‘์ด์–ด์•ผ ํ•ด.
ํ”„๋กœ๋•์…˜ ํ™˜๊ฒฝ์—์„œ๋Š” systemd ์„œ๋น„์Šค๋กœ ๋“ฑ๋กํ•ด์„œ ์ž๋™ ์‹œ์ž‘๋˜๊ฒŒ ํ•ด์•ผ ํ•จ.
5
ํŒจ์Šค์›Œ๋“œ ํ‰๋ฌธ ์ €์žฅ
์•”ํ˜ธํ™” ํŒจ์Šค์›Œ๋“œ๋ฅผ ์ฝ”๋“œ์— ํ•˜๋“œ์ฝ”๋”ฉํ•˜๊ฑฐ๋‚˜ ํ‰๋ฌธ ํŒŒ์ผ์— ์ €์žฅํ•˜๋ฉด ์•ˆ ๋ผ.
๋ฐ˜๋“œ์‹œ ํ™˜๊ฒฝ๋ณ€์ˆ˜(.env)๋‚˜ ์‹œํฌ๋ฆฟ ๋งค๋‹ˆ์ €(AWS Secrets Manager, HashiCorp Vault ๋“ฑ) ์‚ฌ์šฉ!
๐Ÿš€ ์„ฑ๋Šฅ ์ตœ์ ํ™” ํŒ

1. ๋ฐฐ์น˜ ์—…๋กœ๋“œ ์—ฌ๋Ÿฌ ํŒŒ์ผ ์˜ฌ๋ฆด ๋•Œ ํ•˜๋‚˜์”ฉ ์˜ฌ๋ฆฌ์ง€ ๋ง๊ณ  ipfs.add_multiple() ์จ์„œ ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•ด.

2. ๋กœ์ปฌ ์บ์‹ฑ
์ž์ฃผ ์ฝ๋Š” ํŒŒ์ผ์€ ๋กœ์ปฌ์— ์บ์‹œํ•ด๋‘๋ฉด IPFS ๋„คํŠธ์›Œํฌ ์š”์ฒญ ์ค„์ผ ์ˆ˜ ์žˆ์–ด.
functools.lru_cache๋‚˜ Redis ์จ๋ด.

3. ๊ฒŒ์ดํŠธ์›จ์ด ์„ ํƒ
๊ณต๊ฐœ ๊ฒŒ์ดํŠธ์›จ์ด(ipfs.io, cloudflare-ipfs.com)๋Š” ๋А๋ฆด ์ˆ˜ ์žˆ์–ด.
Pinata ์ „์šฉ ๊ฒŒ์ดํŠธ์›จ์ด๋‚˜ ์ž์ฒด ๊ฒŒ์ดํŠธ์›จ์ด ์šด์˜ํ•˜๋ฉด ํ›จ์”ฌ ๋น ๋ฆ„.

4. CIDv1 ์‚ฌ์šฉ
ipfs.add(file, cid_version=1)๋กœ CIDv1 ์“ฐ๋ฉด base32 ์ธ์ฝ”๋”ฉ์ด๋ผ URL์—์„œ ๋” ์•ˆ์ „ํ•˜๊ณ  ๋Œ€์†Œ๋ฌธ์ž ๊ตฌ๋ถ„ ์—†์–ด.


โ›“๏ธ ๋ธ”๋ก์ฒด์ธ ์—ฐ๋™ - CID๋ฅผ ์˜จ์ฒด์ธ์— ๊ธฐ๋กํ•˜๊ธฐ

IPFS๋งŒ์œผ๋กœ๋„ ์ถฉ๋ถ„ํ•˜์ง€๋งŒ, CID๋ฅผ ์ด๋”๋ฆฌ์›€ ๋ธ”๋ก์ฒด์ธ์— ๊ธฐ๋กํ•˜๋ฉด ์†Œ์œ ๊ถŒ ์ฆ๋ช… + ํƒ€์ž„์Šคํƒฌํ”„ + ๋ถˆ๋ณ€์„ฑ๊นŒ์ง€ ์™„๋ฒฝํ•˜๊ฒŒ ๋ณด์žฅ๋ผ.
์ด๊ฒŒ ์ง„์งœ Web3 ์Šคํƒ์ด์•ผ! ๐Ÿ”ฅ

Python - blockchain_anchor.py
from web3 import Web3
import json
import os

# ๊ฐ„๋‹จํ•œ ์Šค๋งˆํŠธ ์ปจํŠธ๋ž™ํŠธ ABI (Solidity๋กœ ๋ฐฐํฌํ•œ ๊ฒƒ)
DOCUMENT_REGISTRY_ABI = [
    {
        "inputs": [
            {"name": "cid", "type": "string"},
            {"name": "title", "type": "string"},
            {"name": "docHash", "type": "bytes32"}
        ],
        "name": "registerDocument",
        "outputs": [],
        "stateMutability": "nonpayable",
        "type": "function"
    },
    {
        "inputs": [{"name": "cid", "type": "string"}],
        "name": "getDocument",
        "outputs": [
            {"name": "owner", "type": "address"},
            {"name": "title", "type": "string"},
            {"name": "timestamp", "type": "uint256"},
            {"name": "docHash", "type": "bytes32"}
        ],
        "stateMutability": "view",
        "type": "function"
    }
]

class BlockchainAnchor:
    """
    IPFS CID๋ฅผ ์ด๋”๋ฆฌ์›€ ๋ธ”๋ก์ฒด์ธ์— ์•ต์ปค๋ง
    ์†Œ์œ ๊ถŒ ์ฆ๋ช… + ํƒ€์ž„์Šคํƒฌํ”„ ๋ถˆ๋ณ€ ๊ธฐ๋ก
    """
    
    def __init__(self, 
                 rpc_url: str,           # ์ด๋”๋ฆฌ์›€ RPC (Infura, Alchemy ๋“ฑ)
                 contract_address: str,   # ๋ฐฐํฌ๋œ ์ปจํŠธ๋ž™ํŠธ ์ฃผ์†Œ
                 private_key: str):       # ์„œ๋ช…์šฉ ๊ฐœ์ธํ‚ค
        
        self.w3 = Web3(Web3.HTTPProvider(rpc_url))
        
        if not self.w3.is_connected():
            raise ConnectionError("์ด๋”๋ฆฌ์›€ ๋…ธ๋“œ ์—ฐ๊ฒฐ ์‹คํŒจ!")
        
        print(f"โœ… ์ด๋”๋ฆฌ์›€ ์—ฐ๊ฒฐ: ์ฒด์ธ ID {self.w3.eth.chain_id}")
        
        # ์ปจํŠธ๋ž™ํŠธ ์ธ์Šคํ„ด์Šค
        self.contract = self.w3.eth.contract(
            address=Web3.to_checksum_address(contract_address),
            abi=DOCUMENT_REGISTRY_ABI
        )
        
        # ๊ณ„์ • ์„ค์ •
        self.account = self.w3.eth.account.from_key(private_key)
        print(f"๐Ÿ“ ๊ณ„์ •: {self.account.address}")
    
    def anchor_cid(self, cid: str, title: str, content: str) -> dict:
        """
        CID๋ฅผ ๋ธ”๋ก์ฒด์ธ์— ๊ธฐ๋ก
        """
        import hashlib
        
        # ๋ฌธ์„œ ํ•ด์‹œ (bytes32)
        doc_hash = hashlib.sha256(content.encode()).digest()
        doc_hash_bytes32 = doc_hash[:32]
        
        print(f"โ›“๏ธ ๋ธ”๋ก์ฒด์ธ ์•ต์ปค๋ง: {cid[:20]}...")
        
        # ํŠธ๋žœ์žญ์…˜ ๋นŒ๋“œ
        nonce = self.w3.eth.get_transaction_count(self.account.address)
        
        tx = self.contract.functions.registerDocument(
            cid, title, doc_hash_bytes32
        ).build_transaction({
            'from': self.account.address,
            'nonce': nonce,
            'gas': 200000,
            'gasPrice': self.w3.eth.gas_price
        })
        
        # ์„œ๋ช… ๋ฐ ์ „์†ก
        signed_tx = self.w3.eth.account.sign_transaction(tx, self.account.key)
        tx_hash = self.w3.eth.send_raw_transaction(signed_tx.rawTransaction)
        
        print(f"๐Ÿ“จ ํŠธ๋žœ์žญ์…˜ ์ „์†ก: {tx_hash.hex()}")
        
        # ์˜์ˆ˜์ฆ ๋Œ€๊ธฐ
        receipt = self.w3.eth.wait_for_transaction_receipt(tx_hash, timeout=120)
        
        result = {
            'tx_hash': tx_hash.hex(),
            'block_number': receipt['blockNumber'],
            'gas_used': receipt['gasUsed'],
            'cid': cid,
            'anchored': receipt['status'] == 1
        }
        
        if result['anchored']:
            print(f"โœ… ์•ต์ปค๋ง ์™„๋ฃŒ! ๋ธ”๋ก #{result['block_number']}")
        else:
            print("โŒ ํŠธ๋žœ์žญ์…˜ ์‹คํŒจ!")
        
        return result
    
    def verify_cid(self, cid: str) -> dict:
        """
        ๋ธ”๋ก์ฒด์ธ์—์„œ CID ์†Œ์œ ๊ถŒ ๋ฐ ํƒ€์ž„์Šคํƒฌํ”„ ๊ฒ€์ฆ
        """
        try:
            owner, title, timestamp, doc_hash = self.contract.functions.getDocument(cid).call()
            
            from datetime import datetime
            registered_time = datetime.fromtimestamp(timestamp)
            
            return {
                'verified': True,
                'owner': owner,
                'title': title,
                'registered_at': registered_time.isoformat(),
                'doc_hash': doc_hash.hex()
            }
        except Exception as e:
            return {'verified': False, 'error': str(e)}
๐Ÿ’ก ํ…Œ์ŠคํŠธ๋„ท ๋จผ์ €! ์‹ค์ œ ์ด๋”๋ฆฌ์›€ ๋ฉ”์ธ๋„ท์€ ๊ฐ€์Šค๋น„๊ฐ€ ๋น„์‹ธ.
๊ฐœ๋ฐœ ์ค‘์—๋Š” Sepolia๋‚˜ Mumbai(Polygon ํ…Œ์ŠคํŠธ๋„ท) ์จ์„œ ๋ฌด๋ฃŒ๋กœ ํ…Œ์ŠคํŠธํ•ด.
Infura๋‚˜ Alchemy์—์„œ ๋ฌด๋ฃŒ RPC ์—”๋“œํฌ์ธํŠธ ๋ฐ›์„ ์ˆ˜ ์žˆ์–ด!

๐ŸŽฏ ๋งˆ๋ฌด๋ฆฌ - ์ด๊ฑธ๋กœ ๋ญ˜ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด?

์ž ์˜ค๋Š˜ ๋ฐฐ์šด ๋‚ด์šฉ ์ •๋ฆฌํ•ด๋ณด์ž. ํŒŒ์ด์ฌ + IPFS ์กฐํ•ฉ์œผ๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๊ฒƒ๋“ค์ด ์ง„์งœ ๋งŽ์•„ ๐Ÿคฉ

๐ŸŒŸ ํ™œ์šฉ ์‚ฌ๋ก€๋“ค

๐Ÿ“„ ํƒˆ์ค‘์•™ ํฌํŠธํด๋ฆฌ์˜ค ํ”Œ๋žซํผ
์žฌ๋Šฅ๋„ท ๊ฐ™์€ ํ”Œ๋žซํผ์—์„œ ํ”„๋ฆฌ๋žœ์„œ๋“ค์ด ์ž์‹ ์˜ ์ž‘์—…๋ฌผ์„ IPFS์— ์˜ฌ๋ฆฌ๋ฉด, ํ”Œ๋žซํผ์ด ๋งํ•ด๋„ ํฌํŠธํด๋ฆฌ์˜ค๋Š” ์˜๊ตฌ ๋ณด์กด๋ผ. ์ง„์งœ ๋‚ด ๊ฒƒ์ด ๋˜๋Š” ๊ฑฐ์•ผ!

๐Ÿ“ฐ ๊ฒ€์—ด ์ €ํ•ญ ๋ฏธ๋””์–ด
์–ธ๋ก  ์ž์œ ๊ฐ€ ์ œํ•œ๋œ ํ™˜๊ฒฝ์—์„œ ๊ธฐ์‚ฌ๋‚˜ ๋ฌธ์„œ๋ฅผ IPFS์— ์˜ฌ๋ฆฌ๋ฉด ์‚ญ์ œ ๋ถˆ๊ฐ€. ์‹ค์ œ๋กœ ํ™์ฝฉ ์‹œ์œ„ ๋•Œ IPFS ๋งŽ์ด ์ผ์–ด.

๐ŸŽ“ ํ•™์ˆ  ๋…ผ๋ฌธ ์•„์นด์ด๋ธŒ
๋…ผ๋ฌธ์„ IPFS์— ์˜ฌ๋ฆฌ๊ณ  DOI ๋Œ€์‹  CID ์‚ฌ์šฉ. ์˜๊ตฌ ์ ‘๊ทผ ๊ฐ€๋Šฅํ•˜๊ณ  ์œ„๋ณ€์กฐ ๋ถˆ๊ฐ€.

โš–๏ธ ๋ฒ•์  ๋ฌธ์„œ ํƒ€์ž„์Šคํƒฌํ”„
๊ณ„์•ฝ์„œ, ํŠนํ—ˆ ์•„์ด๋””์–ด ๋“ฑ์„ IPFS + ๋ธ”๋ก์ฒด์ธ์— ๊ธฐ๋กํ•˜๋ฉด ๋ฒ•์  ์ฆ๊ฑฐ๋ ฅ ์žˆ๋Š” ํƒ€์ž„์Šคํƒฌํ”„ ์ƒ์„ฑ.

๐ŸŽฎ NFT ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ ์ €์žฅ
NFT์˜ ์ด๋ฏธ์ง€์™€ ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋ฅผ IPFS์— ์ €์žฅํ•˜๋ฉด ์ค‘์•™ ์„œ๋ฒ„ ์˜์กด ์—†์ด ์˜๊ตฌ ๋ณด์กด. ์ง„์งœ ํƒˆ์ค‘์•™ NFT!

๐Ÿ“Š ๊ธฐ์ˆ  ์Šคํƒ ์š”์•ฝ

Python 3.8+ โ†’ ๋ฉ”์ธ ๊ฐœ๋ฐœ ์–ธ์–ด
ipfshttpclient โ†’ IPFS API ํ†ต์‹ 
Kubo (go-ipfs) โ†’ ๋กœ์ปฌ IPFS ๋…ธ๋“œ
Pinata โ†’ ํด๋ผ์šฐ๋“œ ํ•€๋‹ ์„œ๋น„์Šค
cryptography โ†’ AES-256-GCM ์•”ํ˜ธํ™”
SQLite โ†’ ๋กœ์ปฌ CID ์ธ๋ฑ์Šค
web3.py โ†’ ๋ธ”๋ก์ฒด์ธ ์—ฐ๋™ (์„ ํƒ)
argparse โ†’ CLI ์ธํ„ฐํŽ˜์ด์Šค

์ด ์‹œ์Šคํ…œ ๊ตฌ์ถ•ํ•˜๋Š” ๋ฐ ๊ด€์‹ฌ ์žˆ๋Š” ๋ถ„๋“ค, ํ˜น์‹œ ํŒŒ์ด์ฌ์ด๋‚˜ Web3 ๊ฐœ๋ฐœ ๋„์›€์ด ํ•„์š”ํ•˜๋‹ค๋ฉด ์žฌ๋Šฅ๋„ท์—์„œ ๊ด€๋ จ ์ „๋ฌธ๊ฐ€๋ฅผ ์ฐพ์•„๋ณผ ์ˆ˜ ์žˆ์–ด. ๋ธ”๋ก์ฒด์ธ ๊ฐœ๋ฐœ์ž๋“ค์ด ๊ฝค ๋งŽ์ด ํ™œ๋™ํ•˜๊ณ  ์žˆ๊ฑฐ๋“  ๐Ÿ˜Š

ํƒˆ์ค‘์•™ํ™” ๊ธฐ์ˆ ์€ ์•„์ง ๋ฐœ์ „ ์ค‘์ด๊ณ , IPFS๋„ ๊ณ„์† ์—…๋ฐ์ดํŠธ๋˜๊ณ  ์žˆ์–ด. ํŠนํžˆ Filecoin๊ณผ์˜ ํ†ตํ•ฉ์œผ๋กœ ๊ฒฝ์ œ์  ์ธ์„ผํ‹ฐ๋ธŒ ๊ธฐ๋ฐ˜ ์˜๊ตฌ ์ €์žฅ์ด ๊ฐ€๋Šฅํ•ด์ง€๊ณ  ์žˆ๊ณ , IPFS Cluster๋กœ ์—ฌ๋Ÿฌ ๋…ธ๋“œ๋ฅผ ๋ฌถ์–ด์„œ ๋” ์•ˆ์ •์ ์ธ ์„œ๋น„์Šค๋„ ๋งŒ๋“ค ์ˆ˜ ์žˆ์–ด.

Web3 ์„ธ๊ณ„์—์„œ "๋‚ด ๋ฐ์ดํ„ฐ๋Š” ๋‚ด๊ฐ€ ์†Œ์œ ํ•œ๋‹ค"๋Š” ์ฒ ํ•™, ์ด์ œ ์ฝ”๋“œ๋กœ ์ง์ ‘ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋์ง€? ๐Ÿš€

๐Ÿ”‘ ํ•ต์‹ฌ ์š”์•ฝ ๐ŸŒ IPFS ์ฝ˜ํ…์ธ  ์ฃผ์†Œ ๊ธฐ๋ฐ˜ ๋ถ„์‚ฐ ์ €์žฅ ๋„คํŠธ์›Œํฌ ๐Ÿ Python ํด๋ผ์ด์–ธํŠธ ipfshttpclient๋กœ API ํ†ต์‹  ๐Ÿ“Œ Pinata ํ•€๋‹ 24/7 ์˜๊ตฌ ๋ณด์กด ์ด์ค‘ ๋ฐฑ์—… โ›“๏ธ ๋ธ”๋ก์ฒด์ธ ์•ต์ปค ์†Œ์œ ๊ถŒ ์ฆ๋ช… ํƒ€์ž„์Šคํƒฌํ”„ ๋ถˆ๋ณ€ ๊ธฐ๋ก ๐ŸŽฏ ํƒˆ์ค‘์•™ํ™” = ๊ฒ€์—ด ๋ถˆ๊ฐ€ + ์˜๊ตฌ ๋ณด์กด + ๋‚ด ๋ฐ์ดํ„ฐ ์ฃผ๊ถŒ CID๋Š” ๋‚ด์šฉ์˜ ์ง€๋ฌธ | ํ•€์€ ์˜๊ตฌ ๋ณด์กด์˜ ์—ด์‡  | ๋ธ”๋ก์ฒด์ธ์€ ์†Œ์œ ๊ถŒ์˜ ์ฆ๊ฑฐ Python + IPFS + Pinata + Web3.py = ์™„์ „ํ•œ ํƒˆ์ค‘์•™ ๋ฌธ์„œ ์ €์žฅ ์‹œ์Šคํ…œ
๋Œ“๊ธ€ ์ž‘์„ฑ

์ด ๊ธ€์— ๋Œ€ํ•œ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒ๊ฐ์„ ๋“ค๋ ค์ฃผ์„ธ์š”

๋Œ“๊ธ€ 0
๋Œ“๊ธ€์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ์ค‘์ž…๋‹ˆ๋‹ค.

์•„์ง ๋Œ“๊ธ€์ด ์—†์Šต๋‹ˆ๋‹ค.