Skip to content
Draft
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
111 changes: 70 additions & 41 deletions modules/sdk-coin-near/src/near.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
EDDSAMethods,
EDDSAMethodTypes,
Environments,
getEddsaSigningMaterial,
KeyPair,
MPCAlgorithm,
MPCRecoveryOptions,
Expand All @@ -32,6 +33,7 @@ import {
ParseTransactionOptions as BaseParseTransactionOptions,
PublicKey,
RecoveryTxRequest,
signEddsaMpcV2RecoveryTx,
SignedTransaction,
SignTransactionOptions as BaseSignTransactionOptions,
TokenEnablementConfig,
Expand Down Expand Up @@ -365,6 +367,7 @@ export class Near extends BaseCoin {
}
const bitgoKey = params.bitgoKey.replace(/\s/g, '');
const isUnsignedSweep = !params.userKey && !params.backupKey && !params.walletPassphrase;
const isMpcV2 = await this.isMpcv2SigningMaterial(params.userKey, params.backupKey, params.walletPassphrase);
const MPC = await EDDSAMethods.getInitializedMpcInstance();
const { storageAmountPerByte, transferCost, receiptConfig } = await this.getProtocolConfig();
let isStorageDepositEnabled = false;
Expand Down Expand Up @@ -440,7 +443,8 @@ export class Near extends BaseCoin {
bitgoKey,
isStorageDepositEnabled,
availableTokenBalance,
isUnsignedSweep
isUnsignedSweep,
isMpcV2
);
}

Expand Down Expand Up @@ -474,7 +478,7 @@ export class Near extends BaseCoin {
const unsignedTransaction = (await txBuilder.build()) as Transaction;
let serializedTx = unsignedTransaction.toBroadcastFormat();
if (!isUnsignedSweep) {
serializedTx = await this.signRecoveryTransaction(txBuilder, params, currPath, accountId);
serializedTx = await this.signRecoveryTransaction(txBuilder, params, currPath, accountId, isMpcV2);
} else {
return this.buildUnsignedSweepTransaction(
txBuilder,
Expand Down Expand Up @@ -514,7 +518,8 @@ export class Near extends BaseCoin {
bitgoKey: string,
isStorageDepositEnabled: boolean,
availableTokenBalance: BigNumber,
isUnsignedSweep: boolean
isUnsignedSweep: boolean,
isMpcV2 = false
): Promise<MPCTx | MPCSweepTxs> {
const factory = new TransactionBuilderFactory(token);
const bs58EncodedPublicKey = nearAPI.utils.serialize.base_encode(new Uint8Array(Buffer.from(senderAddress, 'hex')));
Expand Down Expand Up @@ -549,7 +554,13 @@ export class Near extends BaseCoin {
token
);
} else {
const serializedTx = await this.signRecoveryTransaction(txBuilder, params, derivationPath, senderAddress);
const serializedTx = await this.signRecoveryTransaction(
txBuilder,
params,
derivationPath,
senderAddress,
isMpcV2
);
return { serializedTx: serializedTx, scanIndex: idx };
}
}
Expand Down Expand Up @@ -631,12 +642,11 @@ export class Near extends BaseCoin {
txBuilder: TransactionBuilder,
params: MPCRecoveryOptions,
derivationPath: string,
senderAddress: string
senderAddress: string,
isMpcV2 = false
): Promise<string> {
const unsignedTransaction = (await txBuilder.build()) as Transaction;
// Sign the txn
/* ***************** START **************************************/
// TODO(BG-51092): This looks like a common part which can be extracted out too

if (!params.userKey) {
throw new Error('missing userKey');
}
Expand All @@ -647,49 +657,68 @@ export class Near extends BaseCoin {
throw new Error('missing wallet passphrase');
}

// Clean up whitespace from entered values
const userKey = params.userKey.replace(/\s/g, '');
const backupKey = params.backupKey.replace(/\s/g, '');

// Decrypt private keys from KeyCard values
let userPrv;
try {
userPrv = await this.bitgo.decrypt({
input: userKey,
password: params.walletPassphrase,
let signatureHex: Buffer;
if (isMpcV2) {
signatureHex = await signEddsaMpcV2RecoveryTx({
message: unsignedTransaction.signablePayload,
userKey,
backupKey,
walletPassphrase: params.walletPassphrase,
bitgoKey: params.bitgoKey.replace(/\s/g, ''),
derivationPath,
bitgo: this.bitgo,
});
} catch (e) {
throw new Error(`Error decrypting user keychain: ${e.message}`);
}
/** TODO BG-52419 Implement Codec for parsing */
const userSigningMaterial = JSON.parse(userPrv) as EDDSAMethodTypes.UserSigningMaterial;
} else {
let userPrv;
try {
userPrv = await this.bitgo.decrypt({
input: userKey,
password: params.walletPassphrase,
});
} catch (e) {
throw new Error(`Error decrypting user keychain: ${e.message}`);
}
const userSigningMaterial = JSON.parse(userPrv) as EDDSAMethodTypes.UserSigningMaterial;

let backupPrv;
try {
backupPrv = await this.bitgo.decrypt({
input: backupKey,
password: params.walletPassphrase,
});
} catch (e) {
throw new Error(`Error decrypting backup keychain: ${e.message}`);
}
const backupSigningMaterial = JSON.parse(backupPrv) as EDDSAMethodTypes.BackupSigningMaterial;
/* ********************** END ***********************************/

// add signature
const signatureHex = await EDDSAMethods.getTSSSignature(
userSigningMaterial,
backupSigningMaterial,
derivationPath,
unsignedTransaction
);
const publicKeyObj = { pub: senderAddress };
txBuilder.addSignature(publicKeyObj as PublicKey, signatureHex);
let backupPrv;
try {
backupPrv = await this.bitgo.decrypt({
input: backupKey,
password: params.walletPassphrase,
});
} catch (e) {
throw new Error(`Error decrypting backup keychain: ${e.message}`);
}
const backupSigningMaterial = JSON.parse(backupPrv) as EDDSAMethodTypes.BackupSigningMaterial;

signatureHex = await EDDSAMethods.getTSSSignature(
userSigningMaterial,
backupSigningMaterial,
derivationPath,
unsignedTransaction
);
}

txBuilder.addSignature({ pub: senderAddress } as PublicKey, signatureHex);
const completedTransaction = await txBuilder.build();
return completedTransaction.toBroadcastFormat();
}

private async isMpcv2SigningMaterial(
userKey?: string,
backupKey?: string,
walletPassphrase?: string
): Promise<boolean> {
if (!walletPassphrase) return false;
if (!userKey) throw new Error('missing userKey');
if (!backupKey) throw new Error('missing backupKey');
const material = await getEddsaSigningMaterial(userKey.replace(/\s/g, ''), walletPassphrase, this.bitgo);
return material.version === 'v2';
}

async createBroadcastableSweepTransaction(params: MPCSweepRecoveryOptions): Promise<MPCTxs> {
const req = params.signatureShares;
const broadcastableTransactions: MPCTx[] = [];
Expand Down
Loading
Loading