This commit is contained in:
callebtc
2022-12-26 21:14:00 +01:00
parent 9d5dbc97d5
commit 3d18e53cb7

View File

@@ -286,29 +286,40 @@ async def send(ctx, amount: int, lock: str, legacy: bool):
async def receive(ctx, token: str, lock: str): async def receive(ctx, token: str, lock: str):
wallet: Wallet = ctx.obj["WALLET"] wallet: Wallet = ctx.obj["WALLET"]
await wallet.load_mint() await wallet.load_mint()
# check for P2SH locks
if lock: if lock:
# load the script and signature of this address from the database # load the script and signature of this address from the database
assert len(lock.split("P2SH:")) == 2, Exception( assert len(lock.split("P2SH:")) == 2, Exception(
"lock has wrong format. Expected P2SH:<address>." "lock has wrong format. Expected P2SH:<address>."
) )
address_split = lock.split("P2SH:")[1] address_split = lock.split("P2SH:")[1]
p2shscripts = await get_unused_locks(address_split, db=wallet.db) p2shscripts = await get_unused_locks(address_split, db=wallet.db)
assert len(p2shscripts) == 1, Exception("lock not found.") assert len(p2shscripts) == 1, Exception("lock not found.")
script = p2shscripts[0].script script, signature = p2shscripts[0].script, p2shscripts[0].signature
signature = p2shscripts[0].signature
else: else:
script, signature = None, None script, signature = None, None
# deserialize token
# we support old tokens (< 0.7) without mint information and (W3siaWQ...) # we support old tokens (< 0.7) without mint information and (W3siaWQ...)
# new tokens (>= 0.7) with multiple mint support (eyJ0b2...) # new tokens (>= 0.7) with multiple mint support (eyJ0b2...)
try: try:
# backwards compatibility: tokens without mint information # backwards compatibility: tokens without mint information
# trows an error if the desirialization with the old format doesn't work
proofs = [Proof(**p) for p in json.loads(base64.urlsafe_b64decode(token))] proofs = [Proof(**p) for p in json.loads(base64.urlsafe_b64decode(token))]
_, _ = await wallet.redeem(proofs, scnd_script=script, scnd_siganture=signature) token = await wallet.serialize_proofs(
except: proofs,
# assume token with mint information include_mints=False,
)
# _, _ = await wallet.redeem(proofs, scnd_script=script, scnd_siganture=signature)
except Exception as e:
print(e)
print(token)
# deserialize token
dtoken = json.loads(base64.urlsafe_b64decode(token)) dtoken = json.loads(base64.urlsafe_b64decode(token))
assert "tokens" in dtoken, Exception("no proofs in token") assert "tokens" in dtoken, Exception("no proofs in token")
includes_mint_info: bool = "mints" in dtoken and dtoken.get("mints") is not None includes_mint_info: bool = "mints" in dtoken and dtoken.get("mints") is not None
@@ -318,17 +329,15 @@ async def receive(ctx, token: str, lock: str):
if includes_mint_info: if includes_mint_info:
# we ask the user to confirm any new mints the tokens may include # we ask the user to confirm any new mints the tokens may include
await verify_mints(ctx, dtoken) await verify_mints(ctx, dtoken)
# proceed with redemption # redeem tokens with new wallet instances
await redeem_multimint(ctx, dtoken, script, signature) await redeem_multimint(ctx, dtoken, script, signature)
# reload main wallet so the balance updates # reload main wallet so the balance updates
await wallet.load_proofs() await wallet.load_proofs()
else: else:
# no mint information present, use wallet's default mint # no mint information present, we extract the proofs and use wallet's default mint
proofs = [Proof(**p) for p in dtoken["tokens"]] proofs = [Proof(**p) for p in dtoken["tokens"]]
_, _ = await wallet.redeem( _, _ = await wallet.redeem(proofs, scnd_script=script, scnd_siganture=signature)
proofs, scnd_script=script, scnd_siganture=signature
)
wallet.status() wallet.status()