Changeset View
Changeset View
Standalone View
Standalone View
web/cashtab/src/utils/cashMethods.js
Show First 20 Lines • Show All 1,019 Lines • ▼ Show 20 Lines | try { | ||||
); | ); | ||||
return addrHash160; | return addrHash160; | ||||
} catch (err) { | } catch (err) { | ||||
console.log('Error converting address to hash160'); | console.log('Error converting address to hash160'); | ||||
throw err; | throw err; | ||||
} | } | ||||
} | } | ||||
export function toLegacyCash(addr) { | |||||
// Confirm input is a valid ecash address | |||||
const isValidInput = isValidXecAddress(addr); | |||||
if (!isValidInput) { | |||||
return new Error(`${addr} is not a valid ecash address`); | |||||
} | |||||
// Check for ecash: prefix | |||||
const isPrefixedXecAddress = addr.slice(0, 6) === 'ecash:'; | |||||
// If no prefix, assume it is checksummed for an ecash: prefix | |||||
const testedXecAddr = isPrefixedXecAddress ? addr : `ecash:${addr}`; | |||||
let legacyCashAddress; | |||||
try { | |||||
const { type, hash } = cashaddr.decode(testedXecAddr); | |||||
legacyCashAddress = cashaddr.encode(currency.legacyPrefix, type, hash); | |||||
} catch (err) { | |||||
return err; | |||||
} | |||||
return legacyCashAddress; | |||||
} | |||||
export function toLegacyCashArray(addressArray) { | |||||
let cleanArray = []; // array of bch converted addresses to be returned | |||||
if ( | |||||
addressArray === null || | |||||
addressArray === undefined || | |||||
!addressArray.length || | |||||
addressArray === '' | |||||
) { | |||||
return new Error('Invalid addressArray input'); | |||||
} | |||||
const arrayLength = addressArray.length; | |||||
for (let i = 0; i < arrayLength; i++) { | |||||
let addressValueArr = addressArray[i].split(','); | |||||
let address = addressValueArr[0]; | |||||
let value = addressValueArr[1]; | |||||
// NB that toLegacyCash() includes address validation; will throw error for invalid address input | |||||
const legacyAddress = toLegacyCash(address); | |||||
if (legacyAddress instanceof Error) { | |||||
return legacyAddress; | |||||
} | |||||
let convertedArrayData = legacyAddress + ',' + value + '\n'; | |||||
cleanArray.push(convertedArrayData); | |||||
} | |||||
return cleanArray; | |||||
} | |||||
/* Converts a serialized buffer containing encrypted data into an object | /* Converts a serialized buffer containing encrypted data into an object | ||||
* that can be interpreted by the ecies-lite library. | * that can be interpreted by the ecies-lite library. | ||||
* | * | ||||
* For reference on the parsing logic in this function refer to the link below on the segment of | * For reference on the parsing logic in this function refer to the link below on the segment of | ||||
* ecies-lite's encryption function where the encKey, macKey, iv and cipher are sliced and concatenated | * ecies-lite's encryption function where the encKey, macKey, iv and cipher are sliced and concatenated | ||||
* https://github.com/tibetty/ecies-lite/blob/8fd97e80b443422269d0223ead55802378521679/index.js#L46-L55 | * https://github.com/tibetty/ecies-lite/blob/8fd97e80b443422269d0223ead55802378521679/index.js#L46-L55 | ||||
* | * | ||||
* A similar PSF implmentation can also be found at: | * A similar PSF implmentation can also be found at: | ||||
▲ Show 20 Lines • Show All 115 Lines • Show Last 20 Lines |