-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add modern memo handling to all plugins
- Loading branch information
1 parent
b2996ee
commit a7d8561
Showing
52 changed files
with
398 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { EdgeCurrencyInfo, EdgeMemo, EdgeSpendInfo } from 'edge-core-js/types' | ||
|
||
import { validateMemos } from './validateMemos' | ||
|
||
/** | ||
* Upgrades the memo fields inside an EdgeSpendTarget, | ||
* since we need to be runtime-compatible with legacy core versions. | ||
*/ | ||
export function upgradeMemos( | ||
spendInfo: EdgeSpendInfo, | ||
currencyInfo: EdgeCurrencyInfo | ||
): EdgeSpendInfo { | ||
const { memoType } = currencyInfo | ||
|
||
const legacyMemos: EdgeMemo[] = [] | ||
|
||
// If this chain supports legacy memos, grab those: | ||
if (memoType === 'hex' || memoType === 'number' || memoType === 'text') { | ||
for (const target of spendInfo.spendTargets) { | ||
if (target.memo != null) { | ||
legacyMemos.push({ | ||
type: memoType, | ||
value: target.memo | ||
}) | ||
} else if (target.uniqueIdentifier != null) { | ||
legacyMemos.push({ | ||
type: memoType, | ||
value: target.uniqueIdentifier | ||
}) | ||
} else if (typeof target.otherParams?.uniqueIdentifier === 'string') { | ||
legacyMemos.push({ | ||
type: memoType, | ||
value: target.otherParams.uniqueIdentifier | ||
}) | ||
} | ||
} | ||
} | ||
|
||
// If we don't have modern memos, use the legacy ones: | ||
const out: EdgeSpendInfo = { | ||
...spendInfo, | ||
memos: spendInfo.memos ?? legacyMemos | ||
} | ||
|
||
validateMemos(out, currencyInfo) | ||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
import { gt } from 'biggystring' | ||
import { asMaybe } from 'cleaners' | ||
import { | ||
EdgeCurrencyInfo, | ||
EdgeMemo, | ||
EdgeMemoOption, | ||
EdgeSpendInfo | ||
} from 'edge-core-js/types' | ||
|
||
import { asBase16, asIntegerString } from './types' | ||
|
||
/** | ||
* Validates the memos on an spend request. | ||
* Throws an error if any of the memos are wrong. | ||
*/ | ||
export function validateMemos( | ||
spendInfo: EdgeSpendInfo, | ||
currencyInfo: EdgeCurrencyInfo | ||
): void { | ||
const { memos = [] } = spendInfo | ||
const { | ||
displayName, | ||
memoMaxLength, | ||
memoMaxValue, | ||
memoOptions = [], | ||
multipleMemos = false, | ||
memoType | ||
} = currencyInfo | ||
|
||
// Not all coins keep the legacy memo type in the modern list, | ||
// but we still need to validate the legacy type if present: | ||
const allOptions = [...memoOptions] | ||
if (memoType === 'text') { | ||
allOptions.push({ type: 'text', maxLength: memoMaxLength }) | ||
} | ||
if (memoType === 'number') { | ||
allOptions.push({ type: 'number', maxValue: memoMaxValue }) | ||
} | ||
if (memoType === 'hex') { | ||
allOptions.push({ | ||
type: 'hex', | ||
maxBytes: memoMaxLength == null ? undefined : memoMaxLength / 2 | ||
}) | ||
} | ||
|
||
// What we should call a "memo" in our error messages: | ||
const { memoName = 'memo' } = memoOptions[0] ?? {} | ||
|
||
// Now validate our memos: | ||
for (const memo of memos) { | ||
const options = allOptions.filter(option => memo.type === option.type) | ||
if (options.length < 1) { | ||
throw new Error(`${displayName} ${memoName}: cannot be type ${memo.type}`) | ||
} | ||
const problem = options | ||
.map(option => getMemoError(memo, option)) | ||
.find(problem => problem != null) | ||
if (problem != null) { | ||
throw new Error(`${displayName} {memoName}: ${problem}`) | ||
} | ||
} | ||
|
||
// Validate the number of memos: | ||
if (!multipleMemos && memos.length > 1) { | ||
throw new Error(`${displayName} only supports one ${memoName}`) | ||
} | ||
} | ||
|
||
/** | ||
* Checks a memo against a memo option. | ||
* Returns `undefined` if valid, or an error string if invalid. | ||
*/ | ||
function getMemoError( | ||
memo: EdgeMemo, | ||
option: EdgeMemoOption | ||
): string | undefined { | ||
if ( | ||
option.type === 'text' && | ||
option.maxLength != null && | ||
memo.value.length > option.maxLength | ||
) { | ||
return `cannot be longer than ${option.maxLength}` | ||
} | ||
|
||
if (option.type === 'number') { | ||
const value = asMaybe(asIntegerString)(memo.value) | ||
if (value == null) { | ||
return `is not a valid number` | ||
} | ||
if (option.maxValue != null && gt(value, option.maxValue)) { | ||
return `cannot be greater than ${option.maxValue}` | ||
} | ||
} | ||
|
||
if (option.type === 'hex') { | ||
const value = asMaybe(asBase16)(memo.value) | ||
if (value == null) { | ||
return `is not valid hexadecimal` | ||
} | ||
if (option.maxBytes != null && value.length > option.maxBytes) { | ||
return `cannot be longer than ${option.maxBytes} bytes` | ||
} | ||
if (option.minBytes != null && value.length < option.minBytes) { | ||
return `cannot be shorter than ${option.minBytes} bytes` | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { EdgeMemoOption } from 'edge-core-js/types' | ||
|
||
// https://developers.eos.io/manuals/eos/v2.1/cleos/command-reference/transfer | ||
export const eosMemoOptions: EdgeMemoOption[] = [ | ||
{ | ||
type: 'text', | ||
memoName: 'memo', | ||
maxLength: 256 | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.