Skip to content

Commit

Permalink
Namecoin: Add name_op to TxOutput
Browse files Browse the repository at this point in the history
  • Loading branch information
JeremyRand committed Feb 6, 2020
1 parent 1aedb70 commit d4bf684
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions electrum_nmc/electrum/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,18 @@ def from_address_and_value(cls, address: str, value: Union[int, str]) -> Union['
return cls(scriptpubkey=bfh(bitcoin.address_to_script(address)),
value=value)

def add_name_op(self, name_op: dict) -> None:
if self.name_op is not None:
raise Exception("TxOutput already has a name operation")

# Serialize the name script, and prepend it to the scriptpubkey
name_script = bfh(name_op_to_script(name_op))
self.scriptpubkey = name_script + self.scriptpubkey

# Name ops include an extra 0.01 NMC locked inside the output
if type(self.value) is int:
self.value += COIN // 100

def serialize_to_network(self) -> bytes:
buf = int.to_bytes(self.value, 8, byteorder="little", signed=False)
script = self.scriptpubkey
Expand Down Expand Up @@ -137,6 +149,10 @@ def from_legacy_tuple(cls, _type: int, addr: str, val: Union[int, str]) -> Union
def address(self) -> Optional[str]:
return get_address_from_output_script(self.scriptpubkey) # TODO cache this?

@property
def name_op(self) -> Optional[dict]:
return get_name_op_from_output_script(self.scriptpubkey) # TODO cache this?

def get_ui_address_str(self) -> str:
addr = self.address
if addr is not None:
Expand Down

0 comments on commit d4bf684

Please sign in to comment.