Skip to content

Commit

Permalink
Merge branch 'bccAddTokenRmUtil' of github.com:kajoseph/bitcore
Browse files Browse the repository at this point in the history
  • Loading branch information
kajoseph committed Oct 4, 2024
2 parents b8e5b9b + 79449e7 commit 19122c7
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 4 deletions.
3 changes: 2 additions & 1 deletion packages/bitcore-client/bin/wallet
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ program
.command('paypro', 'pay using payment protocol').alias('p')
.command('send', 'simple send from wallet to an address').alias('s')
.command('sign', 'sign a transaction')
.command('token', 'add an ERC20 token to an eth wallet')
.command('token', 'add an ERC20 token to an EVM wallet')
.command('token-rm', 'remove an ERC20 token from an EVM wallet')
.command('flags', 'check or set wallet flags (XRP only)')
.command('storage', 'storage util for wallets')
.command('sign-message', 'sign a message with an address')
Expand Down
6 changes: 3 additions & 3 deletions packages/bitcore-client/bin/wallet-token
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ program
.version(require('../package.json').version)
.requiredOption('--name <name>', 'REQUIRED - Wallet name')
.requiredOption('--contractAddress <contractAddress>', 'REQUIRED - Token contract address')
.option('--tokenName <tokenName>', 'optional - Custom name for token')
.option('--tokenName <tokenName>', 'optional - Custom name for token (default - contract\'s ticker)')
.option('--storageType <storageType>', 'optional - Name of the database to use (Mongo | Level | TextFile)')
.option('--path <path>', 'optional - Custom wallet storage path')
.parse(process.argv);
Expand All @@ -19,8 +19,8 @@ const main = async () => {
const { name, path, contractAddress, storageType, tokenName } = program.opts();
try {
wallet = await Wallet.loadWallet({ name, path, storageType });
if (!['MATIC', 'ETH', 'ARB', 'OP', 'BASE'].includes(wallet.chain)) {
throw new Error('Cannot add token to non-ETH wallet.');
if (!wallet.isEvmChain()) {
throw new Error('Cannot add token to non-EVM wallet.');
}
const token = await wallet.getToken(contractAddress);
const tokenObj = {
Expand Down
33 changes: 33 additions & 0 deletions packages/bitcore-client/bin/wallet-token-rm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#!/usr/bin/env node

const program = require('commander');
const promptly = require('promptly');
const { Wallet } = require('../ts_build/src/wallet');

program
.version(require('../package.json').version)
.requiredOption('--name <name>', 'REQUIRED - Wallet name')
.requiredOption('--tokenName <tokenName>', 'REQUIRED - Name of token to remove')
.option('--storageType <storageType>', 'optional - Name of the database to use (Mongo | Level | TextFile)')
.option('--path <path>', 'optional - Custom wallet storage path')
.parse(process.argv);

let wallet;

const main = async () => {
const { name, path, tokenName, storageType } = program.opts();
try {
wallet = await Wallet.loadWallet({ name, path, storageType });
if (!wallet.isEvmChain()) {
throw new Error('Cannot remove token from non-EVM wallet.');
}
await wallet.rmToken({ tokenName });
console.log(`Successfully removed ${tokenName}`);
} catch (e) {
console.error(e);
}
};

main()
.catch(console.error)
.finally(() => wallet?.storage?.close());
19 changes: 19 additions & 0 deletions packages/bitcore-client/src/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,14 @@ export class Wallet {
return ['BTC', 'BCH', 'DOGE', 'LTC'].includes(this.chain?.toUpperCase() || 'BTC');
}

/**
* Is this wallet EVM compatible?
* @returns {Boolean}
*/
isEvmChain() {
return ['ETH', 'MATIC', 'ARB', 'OP', 'BASE'].includes(this.chain?.toUpperCase());
}

lock() {
this.unlocked = undefined;
return this;
Expand Down Expand Up @@ -442,6 +450,17 @@ export class Wallet {
await this.saveWallet();
}

async rmToken({ tokenName }) {
if (!this.tokens) {
return;
}
this.tokens = this.tokens.filter(tok =>
(tok.name && tok.name !== tokenName) ||
/* legacy object */ (!tok.name && tok.symbol !== tokenName)
);
await this.saveWallet();
}

async newTx(params: {
utxos?: any[];
recipients: { address: string; amount: number }[];
Expand Down
74 changes: 74 additions & 0 deletions packages/bitcore-client/test/unit/wallet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,5 +473,79 @@ describe('Wallet', function() {
}
});
});

describe('rmToken', function() {
walletName = 'BitcoreClientTestRmToken';
const usdcLegacyObj = {
symbol: 'USDC',
address: '0x123',
decimals: '6',
};

const usdcObj = {
symbol: 'USDC',
address: '0xabc',
decimals: '6',
name: 'USDCn'
};

const daiObj = {
symbol: 'DAI',
address: '0x1a2b3c',
decimals: '6',
name: 'DAIn'
};

beforeEach(async function() {
wallet = await Wallet.create({
chain: 'ETH',
network: 'mainnet',
name: walletName,
phrase: 'snap impact summer because must pipe weasel gorilla actor acid web whip',
password: 'abc123',
lite: false,
storageType,
baseUrl
});

wallet.tokens = [
usdcLegacyObj,
usdcObj,
daiObj
];
});

it('should remove a legacy token object', function() {
wallet.rmToken({ tokenName: 'USDC' });
wallet.tokens.length.should.equal(2);
wallet.tokens.filter(t => t.symbol === 'USDC').length.should.equal(1);
wallet.tokens.filter(t => t.symbol === 'USDC')[0].should.deep.equal(usdcObj);
});

it('should remove a token object', function() {
wallet.rmToken({ tokenName: 'USDCn' });
wallet.tokens.length.should.equal(2);
wallet.tokens.filter(t => t.symbol === 'USDC').length.should.equal(1);
wallet.tokens.filter(t => t.symbol === 'USDC')[0].should.deep.equal(usdcLegacyObj);
});

it('should remove the correct token object regardless of order', function() {
wallet.tokens = [
usdcObj,
daiObj,
usdcLegacyObj // this should be ordered after usdcObj
];

wallet.rmToken({ tokenName: 'USDC' });
wallet.tokens.length.should.equal(2);
wallet.tokens.filter(t => t.symbol === 'USDC').length.should.equal(1);
wallet.tokens.filter(t => t.symbol === 'USDC')[0].should.deep.equal(usdcObj);
});

it('should not remove any unmatched token object', function() {
wallet.rmToken({ tokenName: 'BOGUS' });
wallet.tokens.length.should.equal(3);
});
});
});

0 comments on commit 19122c7

Please sign in to comment.