Changeset View
Changeset View
Standalone View
Standalone View
cashtab/src/wallet/fixtures/vectors.js
// Copyright (c) 2024 The Bitcoin developers | // Copyright (c) 2024 The Bitcoin developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
export const UNSAFE_INTEGER_STRING = '10000000000000000'; | |||||
export default { | export default { | ||||
getBalanceSatsVectors: { | getBalanceSatsVectors: { | ||||
expectedReturns: [ | expectedReturns: [ | ||||
{ | { | ||||
description: 'Kind of a normal balance calculation', | description: 'Kind of a normal balance calculation', | ||||
nonSlpUtxos: [ | nonSlpUtxos: [ | ||||
{ value: '546' }, | { value: '546' }, | ||||
{ value: '150000000' }, | { value: '150000000' }, | ||||
▲ Show 20 Lines • Show All 79 Lines • ▼ Show 20 Lines | toSatoshis: { | ||||
}, | }, | ||||
], | ], | ||||
}, | }, | ||||
hasEnoughToken: { | hasEnoughToken: { | ||||
expectedReturns: [ | expectedReturns: [ | ||||
{ | { | ||||
description: | description: | ||||
'Returns true if wallet has token in exactly required amount', | 'Returns true if wallet has token in exactly required amount', | ||||
tokens: [ | tokens: new Map([ | ||||
{ | [ | ||||
tokenId: | |||||
'28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | '28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | ||||
balance: '100', | '100', | ||||
}, | |||||
], | ], | ||||
]), | |||||
tokenId: | tokenId: | ||||
'28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | '28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | ||||
tokenQty: '100', | tokenQty: '100', | ||||
hasEnough: true, | hasEnough: true, | ||||
}, | }, | ||||
{ | { | ||||
description: | description: | ||||
'Returns false if wallet has token but less than required amount', | 'Returns false if wallet has token but less than required amount', | ||||
tokens: [ | tokens: new Map([ | ||||
{ | [ | ||||
tokenId: | |||||
'28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | '28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | ||||
balance: '99', | '99', | ||||
}, | |||||
], | ], | ||||
]), | |||||
tokenId: | tokenId: | ||||
'28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | '28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | ||||
tokenQty: '100', | tokenQty: '100', | ||||
hasEnough: false, | hasEnough: false, | ||||
}, | }, | ||||
{ | { | ||||
description: 'Returns false if wallet does not have this token', | description: 'Returns false if wallet does not have this token', | ||||
tokens: [ | tokens: new Map([ | ||||
{ | [ | ||||
tokenId: | |||||
'28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | '28eb601e438b1df2f49b3d783f7b236496ad9c07e4af35e8d6c5050732ef030a', | ||||
balance: '99', | '99', | ||||
}, | |||||
], | ], | ||||
]), | |||||
tokenId: | tokenId: | ||||
'50d8292c6255cda7afc6c8566fed3cf42a2794e9619740fe8f4c95431271410e', | '50d8292c6255cda7afc6c8566fed3cf42a2794e9619740fe8f4c95431271410e', | ||||
tokenQty: '100', | tokenQty: '100', | ||||
hasEnough: false, | hasEnough: false, | ||||
}, | }, | ||||
], | ], | ||||
}, | }, | ||||
createCashtabWallet: { | createCashtabWallet: { | ||||
expectedReturns: [ | expectedReturns: [ | ||||
{ | { | ||||
description: | description: | ||||
'Creates a Cashtab wallet from a valid bip39 mnemonic', | 'Creates a Cashtab wallet from a valid bip39 mnemonic', | ||||
mnemonic: | mnemonic: | ||||
'beauty shoe decline spend still weird slot snack coach flee between paper', | 'beauty shoe decline spend still weird slot snack coach flee between paper', | ||||
wallet: { | wallet: { | ||||
state: { | state: { | ||||
balanceSats: 0, | balanceSats: 0, | ||||
slpUtxos: [], | slpUtxos: [], | ||||
nonSlpUtxos: [], | nonSlpUtxos: [], | ||||
tokens: [], | tokens: new Map(), | ||||
parsedTxHistory: [], | parsedTxHistory: [], | ||||
}, | }, | ||||
mnemonic: | mnemonic: | ||||
'beauty shoe decline spend still weird slot snack coach flee between paper', | 'beauty shoe decline spend still weird slot snack coach flee between paper', | ||||
paths: [ | paths: new Map([ | ||||
[ | |||||
1899, | |||||
{ | { | ||||
path: 1899, | |||||
address: | address: | ||||
'ecash:qqa9lv3kjd8vq7952p7rq0f6lkpqvlu0cydvxtd70g', | 'ecash:qqa9lv3kjd8vq7952p7rq0f6lkpqvlu0cydvxtd70g', | ||||
hash: '3a5fb236934ec078b4507c303d3afd82067f8fc1', | hash: '3a5fb236934ec078b4507c303d3afd82067f8fc1', | ||||
wif: 'KywWPgaLDwvW1tWUtUvs13jgqaaWMoNANLVYoKcK9Ddbpnch7Cmw', | wif: 'KywWPgaLDwvW1tWUtUvs13jgqaaWMoNANLVYoKcK9Ddbpnch7Cmw', | ||||
}, | }, | ||||
], | ], | ||||
]), | |||||
name: 'qqa9l', | name: 'qqa9l', | ||||
}, | }, | ||||
}, | }, | ||||
], | ], | ||||
}, | }, | ||||
fiatToSatoshis: { | fiatToSatoshis: { | ||||
expectedReturns: [ | expectedReturns: [ | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | getLegacyPaths: { | ||||
'Determines legacy paths for a post-2.2.0 wallet with legacy paths', | 'Determines legacy paths for a post-2.2.0 wallet with legacy paths', | ||||
wallet: { | wallet: { | ||||
paths: [{ path: 1899 }, { path: 145 }, { path: 245 }], | paths: [{ path: 1899 }, { path: 145 }, { path: 245 }], | ||||
}, | }, | ||||
returned: [145, 245], | returned: [145, 245], | ||||
}, | }, | ||||
{ | { | ||||
description: | description: | ||||
'Determines legacy paths for a post-2.9.0 wallet with legacy paths', | |||||
wallet: { | |||||
paths: new Map([ | |||||
[1899, { address: 'string' }], | |||||
[145, { address: 'string' }], | |||||
[245, { address: 'string' }], | |||||
]), | |||||
}, | |||||
returned: [145, 245], | |||||
}, | |||||
{ | |||||
description: | |||||
'Determines legacy paths for a pre-2.2.0 wallet with legacy paths', | 'Determines legacy paths for a pre-2.2.0 wallet with legacy paths', | ||||
wallet: { | wallet: { | ||||
Path145: {}, | Path145: {}, | ||||
Path245: {}, | Path245: {}, | ||||
Path1899: {}, | Path1899: {}, | ||||
}, | }, | ||||
returned: [145, 245], | returned: [145, 245], | ||||
}, | }, | ||||
▲ Show 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | getWalletsForNewActiveWallet: { | ||||
{ name: 'beta', mnemonic: 'two' }, | { name: 'beta', mnemonic: 'two' }, | ||||
{ name: 'gamma', mnemonic: 'three' }, | { name: 'gamma', mnemonic: 'three' }, | ||||
{ name: 'alpha', mnemonic: 'one' }, | { name: 'alpha', mnemonic: 'one' }, | ||||
], | ], | ||||
errorMsg: `Error activating "alphaprime": Could not find wallet in wallets`, | errorMsg: `Error activating "alphaprime": Could not find wallet in wallets`, | ||||
}, | }, | ||||
], | ], | ||||
}, | }, | ||||
decimalizeTokenAmount: { | |||||
expectedReturns: [ | |||||
{ | |||||
description: | |||||
'Decimalizes amount for 0-decimal token amount larger than JS max safe integer', | |||||
amount: UNSAFE_INTEGER_STRING, | |||||
decimals: 0, | |||||
returned: UNSAFE_INTEGER_STRING, | |||||
}, | |||||
{ | |||||
description: | |||||
'Decimalizes amount for 9-decimal token amount larger than JS max safe integer', | |||||
amount: UNSAFE_INTEGER_STRING, | |||||
decimals: 9, | |||||
returned: '10000000.000000000', | |||||
}, | |||||
{ | |||||
description: | |||||
'Decimalizes amount for 9-decimal token amount larger than JS max safe integer with non-zero decimal places', | |||||
amount: '11111111123456789', | |||||
decimals: 9, | |||||
returned: '11111111.123456789', | |||||
}, | |||||
{ | |||||
description: 'Decimalizes 0 by adding expected decimal places', | |||||
amount: '0', | |||||
decimals: 5, | |||||
returned: '0.00000', | |||||
}, | |||||
{ | |||||
description: | |||||
'Decimalizes a number less than 1 by adding expected decimal places', | |||||
amount: '123', | |||||
decimals: 9, | |||||
returned: '0.000000123', | |||||
}, | |||||
{ | |||||
description: 'Decimalizes smallest amount of slpv1 spec', | |||||
amount: '1', | |||||
decimals: 9, | |||||
returned: '0.000000001', | |||||
}, | |||||
{ | |||||
description: | |||||
'Can decimalize for arbitrary decimals, as long as decimals is an integer', | |||||
amount: '11111111123456789123456789', | |||||
decimals: 18, | |||||
returned: '11111111.123456789123456789', | |||||
}, | |||||
], | |||||
expectedErrors: [ | |||||
{ | |||||
description: 'Throws error if input is not a string', | |||||
amount: 50, | |||||
decimals: 0, | |||||
error: 'amount must be a string', | |||||
}, | |||||
{ | |||||
description: | |||||
'Throws error if input is not a stringified integer', | |||||
amount: '123.45', | |||||
decimals: 0, | |||||
error: 'amount must be a stringified integer', | |||||
}, | |||||
{ | |||||
description: 'Throws error if decimals is not an integer', | |||||
amount: '123', | |||||
decimals: 1.1234, | |||||
error: 'decimals must be an integer', | |||||
}, | |||||
], | |||||
}, | |||||
undecimalizeTokenAmount: { | |||||
expectedReturns: [ | |||||
{ | |||||
description: | |||||
'Returns expected amount for a 0-decimal token that has a decimal point at the end', | |||||
decimalizedAmount: '100.', | |||||
decimals: 0, | |||||
returned: '100', | |||||
}, | |||||
{ | |||||
description: | |||||
'Handles a decimalized amount with no decimal place', | |||||
decimalizedAmount: '100', | |||||
decimals: 9, | |||||
returned: '100000000000', | |||||
}, | |||||
{ | |||||
description: | |||||
'Handles a decimalized amount with under-specified decimal places', | |||||
decimalizedAmount: '100.123', | |||||
decimals: 9, | |||||
returned: '100123000000', | |||||
}, | |||||
], | |||||
expectedErrors: [ | |||||
{ | |||||
description: | |||||
'Throws error if decimalizedAmount is not a string', | |||||
decimalizedAmount: 100, | |||||
decimals: 1, | |||||
error: 'decimalizedAmount must be a string', | |||||
}, | |||||
{ | |||||
description: | |||||
'Throws error if decimalizedAmount is an empty string', | |||||
decimalizedAmount: '', | |||||
decimals: 1, | |||||
error: `decimalizedAmount must be a non-empty string containing only decimal numbers and optionally one decimal point "."`, | |||||
}, | |||||
{ | |||||
description: | |||||
'Throws error if decimalizedAmount includes more than one decimal', | |||||
decimalizedAmount: '100..2', | |||||
decimals: 1, | |||||
error: `decimalizedAmount must be a non-empty string containing only decimal numbers and optionally one decimal point "."`, | |||||
}, | |||||
{ | |||||
description: | |||||
'Throws error if decimalizedAmount includes a decimal point that is not a period', | |||||
decimalizedAmount: '100,25', | |||||
decimals: 1, | |||||
error: `decimalizedAmount must be a non-empty string containing only decimal numbers and optionally one decimal point "."`, | |||||
}, | |||||
{ | |||||
description: | |||||
'Throws error if decimalizedAmount includes alphabet characters', | |||||
decimalizedAmount: 'not a valid decimalizedAmount', | |||||
decimals: 1, | |||||
error: `decimalizedAmount must be a non-empty string containing only decimal numbers and optionally one decimal point "."`, | |||||
}, | |||||
{ | |||||
description: 'Throws error if decimals is invalid', | |||||
decimalizedAmount: '100.123', | |||||
decimals: 1.23, | |||||
error: 'decimals must be an integer', | |||||
}, | |||||
{ | |||||
description: | |||||
'Throws precision error if decimals are over-specified for a 0-decimal token', | |||||
decimalizedAmount: '100.0', | |||||
decimals: 0, | |||||
error: 'decimalizedAmount specified at greater precision than supported token decimals', | |||||
}, | |||||
{ | |||||
description: | |||||
'Throws precision error if decimals are over-specified for a 9-decimal token', | |||||
decimalizedAmount: '100.1234567891', | |||||
decimals: 9, | |||||
error: 'decimalizedAmount specified at greater precision than supported token decimals', | |||||
}, | |||||
], | |||||
}, | |||||
removeLeadingZeros: { | |||||
expectedReturns: [ | |||||
{ | |||||
description: 'Removes leading zeros from a number string', | |||||
givenString: '00000123', | |||||
returned: '123', | |||||
}, | |||||
{ | |||||
description: 'Removes leading zeros from a string of all zeros', | |||||
givenString: '0000000', | |||||
returned: '0', | |||||
}, | |||||
{ | |||||
description: 'Preserves trailing zeros', | |||||
givenString: '0000123000', | |||||
returned: '123000', | |||||
}, | |||||
{ | |||||
description: | |||||
'Removes leading zeros and preserves trailing zeros from an arbitrary string', | |||||
givenString: '00000howaboutthisstring000', | |||||
returned: 'howaboutthisstring000', | |||||
}, | |||||
], | |||||
}, | |||||
}; | }; |