Skip to content
Open
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
118 changes: 77 additions & 41 deletions modules/sdk-coin-near/src/near.ts
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,7 @@
* @prettier
*/

import assert from 'assert';
import * as _ from 'lodash';
import BigNumber from 'bignumber.js';
import * as base58 from 'bs58';
Expand All@@ -18,6 +19,7 @@ import {
EDDSAMethods,
EDDSAMethodTypes,
Environments,
getEddsaSigningMaterial,
KeyPair,
MPCAlgorithm,
MPCRecoveryOptions,
Expand All@@ -32,6 +34,7 @@ import {
ParseTransactionOptions as BaseParseTransactionOptions,
PublicKey,
RecoveryTxRequest,
signEddsaMpcV2RecoveryTx,
SignedTransaction,
SignTransactionOptions as BaseSignTransactionOptions,
TokenEnablementConfig,
Expand DownExpand Up@@ -365,6 +368,13 @@ export class Near extends BaseCoin {
}
const bitgoKey = params.bitgoKey.replace(/\s/g, '');
const isUnsignedSweep = !params.userKey && !params.backupKey && !params.walletPassphrase;
let isMpcV2 = false;
if (!isUnsignedSweep) {
assert(params.userKey, 'missing userKey');
assert(params.backupKey, 'missing backupKey');
assert(params.walletPassphrase, 'missing wallet passphrase');
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 DownExpand Up@@ -440,7 +450,8 @@ export class Near extends BaseCoin {
bitgoKey,
isStorageDepositEnabled,
availableTokenBalance,
isUnsignedSweep
isUnsignedSweep,
isMpcV2
);
}

Expand DownExpand Up@@ -474,7 +485,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 DownExpand Up@@ -514,7 +525,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 DownExpand Up@@ -549,7 +561,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 DownExpand Up@@ -631,12 +649,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 +664,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