Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests for Rawtransactions #88

Merged
merged 9 commits into from
Jul 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion defichain/hdwallet/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def get_wif(self):
return self._wallet.wif()

def get_p2sh(self):
return self._wallet.p2wpkh_in_p2sh_address()
return self._wallet.p2sh_address()

def get_p2pkh(self):
return self._wallet.p2pkh_address()
Expand Down
8 changes: 4 additions & 4 deletions defichain/hdwallet/wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ def default_address(self) -> str:
>>> wallet.default_address()
"dKYzYQDmN9TFEdZy46mFtStbqYLnougqzY"
"""
return self.p2wpkh_in_p2sh_address()
return self.p2sh_address()

def legacy_address(self) -> str:
"""
Expand Down Expand Up @@ -1225,7 +1225,7 @@ def p2wpkh_address(self) -> Optional[str]:
return None
return ensure_string(encode(self._cryptocurrency.SEGWIT_ADDRESS.HRP, 0, public_key_hash))

def p2wpkh_in_p2sh_address(self) -> Optional[str]:
def p2sh_address(self) -> Optional[str]:
"""
Get P2WPKH nested in P2SH address.

Expand All @@ -1236,7 +1236,7 @@ def p2wpkh_in_p2sh_address(self) -> Optional[str]:
>>> wallet = Wallet(network=DefichainMainnet)
>>> wallet.from_mnemonic(mnemonic="venture fitness paper little blush april rigid where find volcano fetch crack label polar dash", passphrase="password")
>>> wallet.from_path(path="m/1129/0/0/0")
>>> wallet.p2wpkh_in_p2sh_address()
>>> wallet.p2sh_address()
"dKYzYQDmN9TFEdZy46mFtStbqYLnougqzY"
"""

Expand Down Expand Up @@ -1411,6 +1411,6 @@ def dumps(self) -> dict:
addresses=dict(
legacy=self.p2pkh_address(),
bech32=self.p2wpkh_address(),
default=self.p2wpkh_in_p2sh_address()
default=self.p2sh_address()
)
)
25 changes: 15 additions & 10 deletions defichain/transactions/rawtransactions/tx.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,27 @@ def get_outputsValue(self) -> int:
def get_fee(self) -> "int | None":
return self.get_inputsValue() - self.get_outputsValue() if self.get_inputsValue() is not None else None

def get_unspent(self) -> [TxInput]:
def get_unspent(self, p2wpkh_for_p2sh_input: [] = None) -> [TxInput]:
outputs = self.get_outputs()
unspent = []
vout = 0
for output in outputs:
if isinstance(output, TxAddressOutput):
addressType = Address.from_address(output.get_address()).get_addressType()
for vout in range(len(outputs)):
if isinstance(outputs[vout], TxAddressOutput):
addressType = Address.from_address(outputs[vout].get_address()).get_addressType()

if addressType == AddressTypes.P2PKH:
unspent.append(TxP2PKHInput(self.get_txid(), vout))
unspent.append(TxP2PKHInput(self.get_txid(), vout, outputs[vout].get_address(),
outputs[vout].get_value()))
elif addressType == AddressTypes.P2SH:
unspent.append(TxP2SHInput(self.get_txid(), vout, output.get_address(), output.get_value()))
if not isinstance(p2wpkh_for_p2sh_input, list):
raise TypeError("The addresses must be passed via a list")
if not p2wpkh_for_p2sh_input:
raise RawTransactionError("To create the unspents, you need to provide the corresponding P2WPKH"
" address for the P2SH address.")
unspent.append(TxP2SHInput(self.get_txid(), vout, p2wpkh_for_p2sh_input.pop(),
outputs[vout].get_value()))
elif addressType == AddressTypes.P2WPKH:
unspent.append(TxP2WPKHInput(self.get_txid(), vout, output.get_address(), output.get_value()))

vout += 1
unspent.append(TxP2WPKHInput(self.get_txid(), vout, outputs[vout].get_address(),
outputs[vout].get_value()))
return unspent

# Get Information
Expand Down
14 changes: 10 additions & 4 deletions defichain/transactions/rawtransactions/txinput.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,20 +213,26 @@ class TxP2PKHInput(TxInput):
:type txid: str
:param vout: (required) previous transaction output number
:type vout: int
:param scriptSig: (optional) signature of P2PKH input
:type scriptSig: str
:param address: (optional) address from which the UTXO are spend - (required for signing of the input)
:type address: str
:param value: (optional) previous transaction output value
:type value: int
:param sequence: (optional) makes input replaceable when not "ffffffff"
:type sequence: str
"""

@staticmethod
def deserialize(network: Any, hex: str) -> "TxP2PKHInput":
input = TxBaseInput.deserialize(network, hex)
address = ""

# Checks if inputs has a script signature and extracts address
if input.get_scriptSig() != "":
if len(input.get_scriptSig()) < 214:
raise DeserializeError("The given input to decode is not an p2pkh input")
publicKey = input.get_scriptSig()[-66:]
address = PublicKey(network, publicKey).p2pkh_address()
publicKey = input.get_scriptSig()[-66:]
address = PublicKey(network, publicKey).p2pkh_address()

resultInput = TxP2PKHInput(input.get_txid(), input.get_vout(), address, input.get_value(), input.get_sequence())
resultInput.set_scriptSig(input.get_scriptSig())
return resultInput
Expand Down
16 changes: 8 additions & 8 deletions defichain/transactions/rawtransactions/txoutput.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@ def set_bytes_address(self, address: bytes) -> None:


class TxDataOutput(TxOutput):
"""
An output witch includes a data.

:param data: (required) your data in hex encoding
:type data: str
:param tokenId: (optional) which token you want to send (almost always 0 -> stands for DFI)
:type tokenId: int
"""

@staticmethod
def deserialize(network: Any, hex: str) -> "TxDataOutput":
Expand All @@ -202,14 +210,6 @@ def deserialize(network: Any, hex: str) -> "TxDataOutput":
return txDataOutput

def __init__(self, data: str, tokenId: int = 0):
"""
An output witch includes a data.

:param data: (required) your data in hex encoding
:type data: str
:param tokenId: (optional) which token you want to send (almost always 0 -> stands for DFI)
:type tokenId: int
"""
self._data = None
self.set_data(data)
super().__init__(0, self.get_script(), tokenId)
Expand Down
Loading
Loading