์ด๊ฒ์ ์จํจ๋ก์ง ๋ธ๋ก์ฒด์ธ ๋คํธ์ํฌ์์ Python์ผ๋ก ์ค๋งํธ ๊ณ์ฝ์ ์์ฑํ๋ ๋ฐฉ๋ฒ์ ๋ํ ๊ต์ก ๊ธฐ์ฌ ์๋ฆฌ์ฆ์ ์ธ ๋ฒ์งธ ๋ถ๋ถ์
๋๋ค. ์ด์ ๊ธฐ์ฌ์์ ์ฐ๋ฆฌ๋
์ด์ ์จํจ๋ก์ง ๋คํธ์ํฌ์์ Python์ ์ฌ์ฉํ์ฌ ์ค๋งํธ ๊ณ์ฝ์ ๊ฐ๋ฐํ ๋ ์ ์ ํ ์๊ตฌ ์ ์ฅ์ API๋ฅผ ํธ์ถํ๋ ๋ฐฉ๋ฒ์ ๋ํ ์์ด๋์ด๋ฅผ ์ป์์ผ๋ฏ๋ก ์ฌ์ฉ ๋ฐฉ๋ฒ์ ์์๋ณด๊ฒ ์ต๋๋ค. ๋ฐํ์ API (๊ณ์ฝ ์คํ API). ๋ฐํ์ API์๋ ๊ณ์ฝ ์คํ์ ์ํ ๊ณตํต ์ธํฐํ์ด์ค๋ฅผ ์ ๊ณตํ๊ณ ๊ฐ๋ฐ์๊ฐ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์, ๋ณํ ๋ฐ ๊ฒ์ฆํ๋ ๋ฐ ๋์์ด ๋๋ 8๊ฐ์ ๊ด๋ จ API๊ฐ ์์ต๋๋ค.
๋ค์์ 8๊ฐ์ API ๋ฐ์ดํฐ์ ๋ํ ๊ฐ๋ตํ ์ค๋ช
์
๋๋ค.
8๊ฐ์ API ๋ฐ์ดํฐ๋ฅผ ํ์ฉํ๋ ๋ฐฉ๋ฒ์ ์์ธํ ์ดํด๋ณด๊ฒ ์ต๋๋ค. ๊ทธ ์ ์ ์จํจ๋ก์ง ์ค๋งํธ ๊ณ์ฝ ๊ฐ๋ฐ ๋๊ตฌ์์ ์ ๊ณ์ฝ์ ์์ฑํ ์ ์์ต๋๋ค.
๋ฐํ์ API๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
์์ ํ๋ ๋ฐฉ๋ฒ์ XNUMX๊ฐ์ง๊ฐ ์์ต๋๋ค ๋ฐํ์ API: ontology.interop.System.Runtime ะธ ์จํจ๋ก์ง.interop.์จํจ๋ก์ง.๋ฐํ์. ์จํจ๋ก์ง ๊ฒฝ๋ก์๋ ์๋ก ์ถ๊ฐ๋ API๊ฐ ํฌํจ๋์ด ์์ต๋๋ค. ์๋ ์ค์ API ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ต๋๋ค.
from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58, GetCurrentBlockHash
์๋ฆผ API
์๋ฆผ ๊ธฐ๋ฅ์ ๋คํธ์ํฌ ์ ์ฒด์ ์ด๋ฒคํธ๋ฅผ ๋ฐฉ์กํฉ๋๋ค. ์๋ ์์์ Notify ํจ์๋ XNUMX์ง์ ๋ฌธ์์ด "hello word"๋ฅผ ๋ฐํํ๊ณ ์ด๋ฅผ ๋คํธ์ํฌ ์ ์ฒด์ ๋ธ๋ก๋์บ์คํธํฉ๋๋ค.
from ontology.interop.System.Runtime import Notify
def demo():
Notify("hello world")
๋ก๊ทธ์์ ์ด๋ฅผ ํ์ธํ ์ ์์ต๋๋ค.
GetTime API
GetTime ํจ์๋ ํจ์๊ฐ ํธ์ถ๋ Unix ์๊ฐ์ ๋ฐํํ๋ ํ์ฌ ํ์์คํฌํ๋ฅผ ๋ฐํํฉ๋๋ค. ์ธก์ ๋จ์๋ ์ด์ ๋๋ค.
from ontology.interop.System.Runtime import GetTime
def demo():
time=GetTime()
return time # return a uint num
GetCurrentBlockHash API
GetCurrentBlockHash ํจ์๋ ํ์ฌ ๋ธ๋ก์ ํด์๋ฅผ ๋ฐํํฉ๋๋ค.
from ontology.interop.Ontology.Runtime import GetCurrentBlockHash
def demo():
block_hash = GetCurrentBlockHash()
return block_hash
์ง๋ ฌํ ๋ฐ ์ญ์ง๋ ฌํ
์ด๋ ์ง๋ ฌํ ๋ฐ ์ญ์ง๋ ฌํ ํจ์ ์์ ๋๋ค. Serialize ํจ์๋ ๊ฐ์ฒด๋ฅผ bytearray ๊ฐ์ฒด๋ก ๋ณํํ๊ณ Deserialize ํจ์๋ bytearray๋ฅผ ์๋ณธ ๊ฐ์ฒด๋ก ๋ณํํฉ๋๋ค. ์๋ ์ฝ๋ ์ํ์ ๋ค์ด์ค๋ ๋งค๊ฐ๋ณ์๋ฅผ ๋ณํํ์ฌ ๊ณ์ฝ์ ์๊ตฌ ์ ์ฅ์์ ์ ์ฅํฉ๋๋ค. ๋ํ ๊ณ์ฝ์ ์๊ตฌ ์ ์ฅ์์์ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ํ์ฌ ์๋ณธ ๊ฐ์ฒด๋ก ๋ณํํฉ๋๋ค.
from ontology.interop.System.Runtime import GetTime, CheckWitness, Log, Notify, Serialize, Deserialize
from ontology.interop.System.Storage import Put, Get, GetContext
def Main(operation, args):
if operation == 'serialize_to_bytearray':
data = args[0]
return serialize_to_bytearray(data)
if operation == 'deserialize_from_bytearray':
key = args[0]
return deserialize_from_bytearray(key)
return False
def serialize_to_bytearray(data):
sc = GetContext()
key = "1"
byte_data = Serialize(data)
Put(sc, key, byte_data)
def deserialize_from_bytearray(key):
sc = GetContext()
byte_data = Get(sc, key)
data = Deserialize(byte_data)
return data
Base58ToAddress ๋ฐ AddressToBase58
์ด ์ฃผ์ ๋ณํ ๊ธฐ๋ฅ ์. Base58ToAddress ํจ์๋ base58๋ก ์ธ์ฝ๋ฉ๋ ์ฃผ์๋ฅผ ๋ฐ์ดํธ ๋ฐฐ์ด ์ฃผ์๋ก ๋ณํํ๊ณ , AddressToBase58์ ๋ฐ์ดํธ ๋ฐฐ์ด ์ฃผ์๋ฅผ base58๋ก ์ธ์ฝ๋ฉ๋ ์ฃผ์๋ก ๋ณํํฉ๋๋ค.
from ontology.interop.Ontology.Runtime import Base58ToAddress, AddressToBase58
def demo():
base58_addr="AV1GLfVzw28vtK3d1kVGxv5xuWU59P6Sgn"
addr=Base58ToAddress(base58_addr)
Log(addr)
base58_addr=AddressToBase58(addr)
Log(base58_addr)
์ฆ์ธ ํ์ธ
CheckWitness(fromAcct) ํจ์์๋ ๋ ๊ฐ์ง ๊ธฐ๋ฅ์ด ์์ต๋๋ค.
- ํ์ฌ ํจ์์ ํธ์ถ์๊ฐ fromAcct์ธ์ง ํ์ธํฉ๋๋ค. ๊ทธ๋ ๋ค๋ฉด(์ฆ, ์๋ช ํ์ธ์ด ํต๊ณผ๋ ๊ฒฝ์ฐ) ํจ์๊ฐ ๋ฐํ๋ฉ๋๋ค.
- ํ์ฌ ํจ์๋ฅผ ํธ์ถํ๋ ๊ฐ์ฒด๊ฐ ์ปจํธ๋ํธ์ธ์ง ํ์ธํ์ธ์. ๊ณ์ฝ์ด๊ณ โโ๊ณ์ฝ์์ ๊ธฐ๋ฅ์ด ์คํ๋๋ฉด ๊ฒ์ฆ์ด ํต๊ณผ๋ฉ๋๋ค. ์ฆ, fromAcct๊ฐ GetCallingScriptHash()์ ๋ฐํ ๊ฐ์ธ์ง ํ์ธํฉ๋๋ค. GetCallingScriptHash() ํจ์๋ ํ์ฌ ์ค๋งํธ ๊ณ์ฝ์ ๊ณ์ฝ ํด์ ๊ฐ์ ๊ฐ์ ธ์ฌ ์ ์์ต๋๋ค.
GetCallingScriptHash()๏ผ
๋์
from ontology.interop.System.Runtime import CheckWitness
from ontology.interop.Ontology.Runtime import Base58ToAddress
def demo():
addr=Base58ToAddress("AW8hN1KhHE3fLDoPAwrhtjD1P7vfad3v8z")
res=CheckWitness(addr)
return res
์์ธํ ๋ด์ฉ์
๊ธฐ์ฌ๋ ํธ์ง์๋ค์ ์ํด ๋ฒ์ญ๋์์ต๋๋ค.
ํด์์จ ๋ฐ ๊ณต์ ํนํ OntologyRussia์ ๊ฒฝ์ฐ.
๋น์ ์ ๊ฐ๋ฐ์์
๋๊น? ๊ธฐ์ ์ปค๋ฎค๋ํฐ์ ๊ฐ์
ํ์ธ์.
์จํจ๋ก์ง
์ถ์ฒ : habr.com