Skip to content

Commit

Permalink
Actually fixed receiving from Grin++
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasFlamel1 committed Oct 30, 2022
1 parent e60cda7 commit 3c180d6
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -502,9 +502,9 @@ export default class Slate {
}

public async combineOffsets(
slate: Slate
offset: Buffer
): Promise<boolean> {
const combinedOffset = await Common.resolveIfPromise(Secp256k1Zkp.blindSum([this.offset, slate.offset], []));
const combinedOffset = await Common.resolveIfPromise(Secp256k1Zkp.blindSum([this.offset, offset], []));
if(combinedOffset === Secp256k1Zkp.OPERATION_FAILED) {
return false;
}
Expand Down Expand Up @@ -602,7 +602,12 @@ export default class Slate {
}
if(!this.isCompact()) {
if(!this.offset.equals(slate.offset)) {
return false;
return false;
}
}
else {
if(this.offset.equals(slate.offset)) {
return false;
}
}
if(this.headerVersion !== slate.headerVersion) {
Expand Down Expand Up @@ -1211,7 +1216,7 @@ export default class Slate {
if((purpose === Slate.Purpose.SEND_INITIAL && !offset.equals(Buffer.alloc(Crypto.SECP256K1_PRIVATE_KEY_LENGTH)) && !await Common.resolveIfPromise(Secp256k1Zkp.isValidSecretKey(offset))) || (purpose === Slate.Purpose.SEND_RESPONSE && !await Common.resolveIfPromise(Secp256k1Zkp.isValidSecretKey(offset)))) {
throw new MimbleWimbleCoinInvalidParameters("Invalid serialized slate offset");
}
slate.offset = (purpose === Slate.Purpose.SEND_INITIAL) ? Buffer.alloc(Crypto.SECP256K1_PRIVATE_KEY_LENGTH) : offset;
slate.offset = offset;
const optionalFields = SlateUtils.readUint8(bitReader);
let numberOfParticipants: BigNumber;
if(optionalFields & 0b00000001) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { Observable, Subscriber } from "rxjs";
const buildOptimisticOperation = async (
account: Account,
slate: Slate,
kernelOffset: Buffer,
commitment: Buffer,
identifier: Identifier,
switchType: number
Expand All @@ -37,15 +38,6 @@ const buildOptimisticOperation = async (
) {
throw new MimbleWimbleCoinAddingToSlateFailed("Failed getting slate's kernel excess");
}
let kernelOffset: Buffer;
try {
kernelOffset = await slate.getOffsetExcess();
}
catch(
error: any
) {
throw new MimbleWimbleCoinAddingToSlateFailed("Failed getting slate's kernel offset");
}
return {
id: encodeOperationId(account.id, commitment.toString("hex"), "IN"),
hash: slate.id,
Expand Down Expand Up @@ -187,12 +179,13 @@ export default (
throw new MimbleWimbleCoinUnsupportedSlate("Invalid slate ID");
}
}
const offset = slate.offset;
let kernelOffset: Buffer;
for(let uniqueKernelOffset: boolean = false; !uniqueKernelOffset;) {
uniqueKernelOffset = true;
if(slate.isCompact()) {
await slate.createOffset();
}
let kernelOffset: Buffer;
try {
kernelOffset = await slate.getOffsetExcess();
}
Expand Down Expand Up @@ -254,6 +247,11 @@ export default (
const publicBlindExcess = await mimbleWimbleCoin.getTransactionPublicKey();
const publicNonce = await mimbleWimbleCoin.getTransactionPublicNonce();
slate.addParticipant(new SlateParticipant(SlateParticipant.SENDER_ID.plus(1), publicBlindExcess, publicNonce));
if(slate.isCompact()) {
if(!await slate.combineOffsets(offset)) {
throw new MimbleWimbleCoinAddingToSlateFailed("Failed combining offset with the slate's offset");
}
}
let publicNonceSum: Buffer;
try {
publicNonceSum = await slate.getPublicNonceSum();
Expand Down Expand Up @@ -285,7 +283,7 @@ export default (
}
subscriber.next({
type: "device-signature-requested",
operation: toOperationRaw(await buildOptimisticOperation(account, slate, commitment, (account as MimbleWimbleCoinAccount).mimbleWimbleCoinResources.nextIdentifier.withHeight(account.currency, tipHeight.plus(1)), Crypto.SwitchType.REGULAR))
operation: toOperationRaw(await buildOptimisticOperation(account, slate, kernelOffset!, commitment, (account as MimbleWimbleCoinAccount).mimbleWimbleCoinResources.nextIdentifier.withHeight(account.currency, tipHeight.plus(1)), Crypto.SwitchType.REGULAR))
});
const {
partialSignature,
Expand Down Expand Up @@ -316,7 +314,7 @@ export default (
derivationPath: newDerivationPath
},
nextIdentifier: (account as MimbleWimbleCoinAccount).mimbleWimbleCoinResources.nextIdentifier.getNext().serialize().toString("hex"),
operation: toOperationRaw(await buildOptimisticOperation(account, slate, commitment, (account as MimbleWimbleCoinAccount).mimbleWimbleCoinResources.nextIdentifier.withHeight(account.currency, tipHeight.plus(1)), Crypto.SwitchType.REGULAR))
operation: toOperationRaw(await buildOptimisticOperation(account, slate, kernelOffset!, commitment, (account as MimbleWimbleCoinAccount).mimbleWimbleCoinResources.nextIdentifier.withHeight(account.currency, tipHeight.plus(1)), Crypto.SwitchType.REGULAR))
});
subscriber.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const buildOptimisticOperation = async (
const buildChangeOperation = async (
account: Account,
slate: Slate,
kernelOffset: Buffer,
amount: BigNumber,
commitment: Buffer | undefined,
identifier: Identifier,
Expand All @@ -78,15 +79,6 @@ const buildChangeOperation = async (
) {
throw new MimbleWimbleCoinFinalizingSlateFailed("Failed getting finalized slate's kernel excess");
}
let kernelOffset: Buffer;
try {
kernelOffset = await slate.getOffsetExcess();
}
catch(
error: any
) {
throw new MimbleWimbleCoinFinalizingSlateFailed("Failed getting finalized slate's kernel offset");
}
return amount.isZero() ? null : {
id: encodeOperationId(account.id, commitment!.toString("hex"), "IN"),
hash: "",
Expand Down Expand Up @@ -280,14 +272,22 @@ export default (
throw new MimbleWimbleCoinCreatingSlateFailed("Failed adding output to slate");
}
}
let kernelOffset: Buffer;
if(transactionAlreadyPrepared) {
slate.offset = transaction.offset!;
try {
kernelOffset = await slate.getOffsetExcess();
}
catch(
error: any
) {
throw new MimbleWimbleCoinFinalizingSlateFailed("Failed getting slate's kernel offset");
}
}
else {
await slate.createOffset();
for(let uniqueKernelOffset: boolean = false; !uniqueKernelOffset;) {
uniqueKernelOffset = true;
let kernelOffset: Buffer;
try {
kernelOffset = await slate.getOffsetExcess();
}
Expand Down Expand Up @@ -425,7 +425,7 @@ export default (
throw new MimbleWimbleCoinUnsupportedResponseFromRecipient("Invalid slate response outputs");
}
if(slate.isCompact()) {
if(!await slateResponse.combineOffsets(slate)) {
if(!await slateResponse.combineOffsets(slate.offset)) {
throw new MimbleWimbleCoinFinalizingSlateFailed("Failed combining slate response's offset with the slate's offset");
}
}
Expand Down Expand Up @@ -493,7 +493,7 @@ export default (
}
const timestamp = new Date();
const operation = await buildOptimisticOperation(account, transaction, slateResponse, timestamp);
const changeOperation = await buildChangeOperation(account, slateResponse, change, commitment, currentIdentifier.withHeight(account.currency, slateResponse.height!), Crypto.SwitchType.REGULAR, timestamp);
const changeOperation = await buildChangeOperation(account, slateResponse, kernelOffset!, change, commitment, currentIdentifier.withHeight(account.currency, slateResponse.height!), Crypto.SwitchType.REGULAR, timestamp);
const bipPath = BIPPath.fromString(account.freshAddresses[0].derivationPath).toPathArray();
++bipPath[Crypto.BIP44_PATH_INDEX_INDEX];
const newDerivationPath = BIPPath.fromPathArray(bipPath).toString(true);
Expand Down

0 comments on commit 3c180d6

Please sign in to comment.