Skip to content

Commit

Permalink
Fix cashlink signing and claiming
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Nov 21, 2024
1 parent 4fd6de2 commit 25c1466
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 11 deletions.
13 changes: 6 additions & 7 deletions src/components/Network.vue
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class Network extends Vue {
fee = 0,
validityStartHeight,
flags = 0 /* Nimiq.Transaction.Flag.NONE */,
data,
recipientData,
signerPubKey,
signature,
proofPrefix = new Uint8Array(0),
Expand All @@ -37,7 +37,7 @@ class Network extends Vue {
fee?: number,
validityStartHeight: number,
flags?: number,
data?: Uint8Array,
recipientData?: Uint8Array,
signerPubKey: Nimiq.PublicKey | Uint8Array,
signature?: Nimiq.Signature | Uint8Array,
proofPrefix?: Uint8Array,
Expand All @@ -48,27 +48,26 @@ class Network extends Vue {
if (signature && !(signature instanceof Nimiq.Signature)) signature = Nimiq.Signature.deserialize(signature);
if (
(data && data.length > 0)
(recipientData && recipientData.length > 0)
|| senderType !== Nimiq.AccountType.Basic
|| recipientType !== Nimiq.AccountType.Basic
|| flags !== 0 /* Nimiq.Transaction.Flag.NONE */
) {
let proof: Nimiq.SerialBuffer | undefined;
if (signature) {
// 32 publicKey + 1 empty merkle path + 64 signature
proof = new Nimiq.SerialBuffer(proofPrefix.length + 32 + 1 + 64);
// 1 type + 32 publicKey + 1 empty merkle path + 64 signature
proof = new Nimiq.SerialBuffer(proofPrefix.length + 1 + 32 + 1 + 64);
proof.write(proofPrefix);
proof.write(Nimiq.SignatureProof.singleSig(signerPubKey, signature).serialize());
}
const tx = new Nimiq.Transaction(
sender,
// @ts-ignore Staking type not yet supported
senderType,
senderData,
recipient,
recipientType,
data || new Uint8Array(0),
recipientData || new Uint8Array(0),
BigInt(value),
BigInt(fee),
flags,
Expand Down
7 changes: 4 additions & 3 deletions src/lib/Cashlink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ class Cashlink {
try {
str = str.replace(/~/g, '').replace(/=*$/, (match) => new Array(match.length).fill('.').join(''));
const buf = Nimiq.BufferUtils.fromBase64Url(str);
const keyPair = Nimiq.KeyPair.derive(Nimiq.PrivateKey.deserialize(buf));
const privateKeyBytes = buf.read(Nimiq.PrivateKey.SIZE);
const keyPair = Nimiq.KeyPair.derive(Nimiq.PrivateKey.deserialize(privateKeyBytes));
const value = buf.readUint64();
let message: string;
if (buf.readPos === buf.byteLength) {
Expand Down Expand Up @@ -304,15 +305,15 @@ class Cashlink {
recipient: Nimiq.Address,
value: number,
fee: number,
data: Uint8Array,
recipientData: Uint8Array,
cashlinkMessage: string,
} {
return {
layout: 'cashlink',
recipient: this.address,
value: this.value,
fee: this.fee,
data: CashlinkExtraData.FUNDING,
recipientData: CashlinkExtraData.FUNDING,
cashlinkMessage: this.message,
};
}
Expand Down
1 change: 1 addition & 0 deletions src/views/RefundSwapSuccess.vue
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ export default class SignBtcTransactionSuccess extends Vue {
signerPubKey: this.keyguardResult.publicKey,
}, this.keyguardResult, this.request.refund, {
senderType: Nimiq.AccountType.HTLC,
recipientData: this.request.refund.extraData,
}));
const proof = new Nimiq.SerialBuffer(1 + tx.proof.length);
Expand Down
3 changes: 2 additions & 1 deletion src/views/SetupSwapSuccess.vue
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ export default class SetupSwapSuccess extends BitcoinSyncBaseView {
...this.request.fund,
recipient: new Nimiq.Address(new Uint8Array(20)),
recipientType: Nimiq.AccountType.HTLC,
data: htlcInfo.fund.htlcData,
recipientData: htlcInfo.fund.htlcData,
flags: 1 /* Nimiq.Transaction.Flag.CONTRACT_CREATION */,
signerPubKey: nimiqSignatureResult.publicKey,
signature: nimiqSignatureResult.signature,
Expand All @@ -613,6 +613,7 @@ export default class SetupSwapSuccess extends BitcoinSyncBaseView {
senderType: Nimiq.AccountType.HTLC,
signerPubKey: nimiqSignatureResult.publicKey,
signature: nimiqSignatureResult.signature,
recipientData: this.request.redeem.extraData,
});
}
Expand Down

0 comments on commit 25c1466

Please sign in to comment.