Skip to content

Commit

Permalink
Merge pull request #605 from EdgeApp/william/multiple-memos
Browse files Browse the repository at this point in the history
- changed: Support the latest core memo API's.
  • Loading branch information
swansontec authored Sep 12, 2023
2 parents 1e23e3d + 4b62584 commit 344976e
Show file tree
Hide file tree
Showing 61 changed files with 1,011 additions and 662 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@
"babel-eslint": "^10.1.0",
"chai": "^4.2.0",
"crypto-browserify": "^3.12.0",
"edge-core-js": "^1.3.7",
"edge-core-js": "^1.6.0",
"esbuild-loader": "^2.20.0",
"eslint": "^7.14.0",
"eslint-config-standard-kit": "0.15.1",
Expand Down
66 changes: 44 additions & 22 deletions src/algorand/AlgorandEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
EdgeEngineActivationOptions,
EdgeEngineGetActivationAssetsOptions,
EdgeGetActivationAssetsResults,
EdgeMemo,
EdgeSpendInfo,
EdgeToken,
EdgeTransaction,
Expand All @@ -26,6 +27,8 @@ import { base16, base64 } from 'rfc4648'

import { CurrencyEngine } from '../common/CurrencyEngine'
import { PluginEnvironment } from '../common/innerPlugin'
import { upgradeMemos } from '../common/upgradeMemos'
import { utf8 } from '../common/utf8'
import {
asyncWaterfall,
cleanTxLogs,
Expand Down Expand Up @@ -241,12 +244,13 @@ export class AlgorandEngine extends CurrencyEngine<

processAlgorandTransaction(tx: BaseTransaction): void {
const {
fee,
'confirmed-round': confirmedRound,
id,
'round-time': roundTime,
sender,
'tx-type': txType
'tx-type': txType,
fee,
id,
note,
sender
} = tx

let currencyCode: string
Expand Down Expand Up @@ -330,17 +334,36 @@ export class AlgorandEngine extends CurrencyEngine<
}
}

const memos: EdgeMemo[] = []
if (note != null) {
const data = base64.parse(note)
try {
memos.push({
memoName: 'note',
type: 'text',
value: utf8.stringify(data)
})
} catch (e) {
memos.push({
memoName: 'note',
type: 'hex',
value: base16.stringify(data).toLowerCase()
})
}
}

const edgeTransaction: EdgeTransaction = {
txid: id,
date: roundTime,
currencyCode,
blockHeight: confirmedRound,
nativeAmount,
currencyCode,
date: roundTime,
isSend,
memos,
nativeAmount,
networkFee,
ourReceiveAddresses,
parentNetworkFee,
signedTx: '',
txid: id,
walletId: this.walletId
}

Expand Down Expand Up @@ -460,6 +483,7 @@ export class AlgorandEngine extends CurrencyEngine<
}

async getMaxSpendable(spendInfo: EdgeSpendInfo): Promise<string> {
spendInfo = upgradeMemos(spendInfo, this.currencyInfo)
let balance = this.getBalance({
currencyCode: spendInfo.currencyCode
})
Expand Down Expand Up @@ -508,8 +532,10 @@ export class AlgorandEngine extends CurrencyEngine<
}

async makeSpend(edgeSpendInfoIn: EdgeSpendInfo): Promise<EdgeTransaction> {
edgeSpendInfoIn = upgradeMemos(edgeSpendInfoIn, this.currencyInfo)
const { edgeSpendInfo, currencyCode, nativeBalance } =
this.makeSpendCheck(edgeSpendInfoIn)
const { memos = [] } = edgeSpendInfo

const spendableAlgoBalance = sub(
this.getBalance({
Expand All @@ -522,11 +548,8 @@ export class AlgorandEngine extends CurrencyEngine<
throw new Error('Error: only one output allowed')
}

const {
nativeAmount: amount,
memo,
publicAddress
} = edgeSpendInfo.spendTargets[0]
const { nativeAmount: amount, publicAddress } =
edgeSpendInfo.spendTargets[0]

if (publicAddress == null)
throw new Error('makeSpend Missing publicAddress')
Expand All @@ -538,10 +561,8 @@ export class AlgorandEngine extends CurrencyEngine<
type: currencyCode === this.currencyInfo.currencyCode ? 'pay' : 'axfer'
}

let note: Uint8Array | undefined
if (memo != null) {
note = Uint8Array.from(Buffer.from(memo, 'ascii'))
}
const note =
memos[0]?.type === 'text' ? utf8.parse(memos[0].value) : undefined

const { customNetworkFee } = edgeSpendInfo
const customFee = asMaybeCustomFee(customNetworkFee).fee
Expand Down Expand Up @@ -623,17 +644,18 @@ export class AlgorandEngine extends CurrencyEngine<
}

const edgeTransaction: EdgeTransaction = {
txid: '',
date: 0,
currencyCode,
blockHeight: 0,
nativeAmount,
currencyCode,
date: 0,
isSend: true,
memos,
nativeAmount,
networkFee,
otherParams,
ourReceiveAddresses: [],
parentNetworkFee,
signedTx: '',
otherParams,
txid: '',
walletId: this.walletId
}

Expand Down
15 changes: 8 additions & 7 deletions src/algorand/algorandInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,30 @@ const networkInfo: AlgorandNetworkInfo = {
}

const currencyInfo: EdgeCurrencyInfo = {
// Basic currency information:
currencyCode: 'ALGO',
displayName: 'Algorand',
pluginId: 'algorand',
walletType: 'wallet:algorand',

defaultSettings: { customFeeSettings: ['fee'] },

memoType: 'text',

// Explorers:
addressExplorer: 'https://algoexplorer.io/address/%s',
transactionExplorer: 'https://algoexplorer.io/tx/%s',

denominations: [
// An array of Objects of the possible denominations for this currency
{
name: 'ALGO',
multiplier: '1000000',
symbol: 'Ⱥ'
}
],

metaTokens: makeMetaTokens(builtinTokens) // Deprecated
// https://developer.algorand.org/docs/get-details/transactions/transactions/
memoOptions: [{ type: 'text', memoName: 'note', maxLength: 1000 }],

// Deprecated:
defaultSettings: { customFeeSettings: ['fee'] },
memoType: 'text',
metaTokens: makeMetaTokens(builtinTokens)
}

export const algorand = makeOuterPlugin<AlgorandNetworkInfo, AlgorandTools>({
Expand Down
15 changes: 8 additions & 7 deletions src/algorand/algorandTestnetInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,30 @@ const networkInfo: AlgorandNetworkInfo = {
}

const currencyInfo: EdgeCurrencyInfo = {
// Basic currency information:
currencyCode: 'ALGO',
displayName: 'Algorand Testnet',
pluginId: 'algorandtestnet',
walletType: 'wallet:algorandtestnet',

defaultSettings: { customFeeSettings: ['fee'] },

memoType: 'text',

// Explorers:
addressExplorer: 'https://testnet.algoexplorer.io/address/%s',
transactionExplorer: 'https://testnet.algoexplorer.io/tx/%s',

denominations: [
// An array of Objects of the possible denominations for this currency
{
name: 'ALGO',
multiplier: '1000000',
symbol: 'Ⱥ'
}
],

metaTokens: makeMetaTokens(builtinTokens) // Deprecated
// https://developer.algorand.org/docs/get-details/transactions/transactions/
memoOptions: [{ type: 'text', memoName: 'note', maxLength: 1000 }],

// Deprecated:
defaultSettings: { customFeeSettings: ['fee'] },
memoType: 'text',
metaTokens: makeMetaTokens(builtinTokens)
}

export const algorandtestnet = makeOuterPlugin<
Expand Down
34 changes: 18 additions & 16 deletions src/binance/BinanceEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import { CurrencyEngine } from '../common/CurrencyEngine'
import { PluginEnvironment } from '../common/innerPlugin'
import { asErrorMessage } from '../common/types'
import { upgradeMemos } from '../common/upgradeMemos'
import {
asyncWaterfall,
cleanTxLogs,
Expand Down Expand Up @@ -183,18 +184,19 @@ export class BinanceEngine extends CurrencyEngine<
if (blockHeight < 0) blockHeight = 0
const unixTimestamp = new Date(tx.blockTime).getTime() / 1000
const edgeTransaction: EdgeTransaction = {
txid: tx.hash,
date: unixTimestamp,
currencyCode,
blockHeight,
nativeAmount: netNativeAmount,
currencyCode,
date: unixTimestamp,
isSend: netNativeAmount.startsWith('-'),
networkFee: nativeNetworkFee,
ourReceiveAddresses, // blank if you sent money otherwise array of addresses that are yours in this transaction
signedTx: '',
memos: [],
metadata: {
notes: tx.memo
},
nativeAmount: netNativeAmount,
networkFee: nativeNetworkFee,
ourReceiveAddresses, // blank if you sent money otherwise array of addresses that are yours in this transaction
signedTx: '',
txid: tx.hash,
walletId: this.walletId
}

Expand Down Expand Up @@ -382,7 +384,9 @@ export class BinanceEngine extends CurrencyEngine<
}

async makeSpend(edgeSpendInfoIn: EdgeSpendInfo): Promise<EdgeTransaction> {
edgeSpendInfoIn = upgradeMemos(edgeSpendInfoIn, this.currencyInfo)
const { edgeSpendInfo, currencyCode } = this.makeSpendCheck(edgeSpendInfoIn)
const { memos = [] } = edgeSpendInfo

const spendTarget = edgeSpendInfo.spendTargets[0]
const { publicAddress } = spendTarget
Expand All @@ -407,10 +411,7 @@ export class BinanceEngine extends CurrencyEngine<
throw new Error('Binance Beacon Chain token transfers not supported')
}

if (edgeSpendInfo.spendTargets[0].otherParams?.uniqueIdentifier != null) {
otherParams.memo =
edgeSpendInfo.spendTargets[0].otherParams.uniqueIdentifier
}
otherParams.memo = memos[0]?.type === 'text' ? memos[0].value : undefined

const nativeNetworkFee = NETWORK_FEE_NATIVE_AMOUNT
const ErrorInsufficientFundsMoreBnb = new Error(
Expand All @@ -432,16 +433,17 @@ export class BinanceEngine extends CurrencyEngine<
// Create the unsigned EdgeTransaction

const edgeTransaction: EdgeTransaction = {
txid: '', // txid
date: 0, // date
currencyCode, // currencyCode
blockHeight: 0, // blockHeight
nativeAmount, // nativeAmount
currencyCode, // currencyCode
date: 0, // date
isSend: nativeAmount.startsWith('-'),
memos,
nativeAmount, // nativeAmount
networkFee: nativeNetworkFee, // networkFee, supposedly fixed
otherParams, // otherParams
ourReceiveAddresses: [], // ourReceiveAddresses
signedTx: '', // signedTx
otherParams, // otherParams
txid: '', // txid
walletId: this.walletId
}

Expand Down
17 changes: 10 additions & 7 deletions src/binance/binanceInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,29 +15,32 @@ const networkInfo: BinanceNetworkInfo = {
}

const currencyInfo: EdgeCurrencyInfo = {
// Basic currency information:
currencyCode: 'BNB',
displayName: 'BNB Beacon Chain',
pluginId: 'binance',
walletType: 'wallet:binance',

defaultSettings: {},

memoMaxLength: 128,

// Explorers:
addressExplorer: 'https://explorer.binance.org/address/%s',
transactionExplorer: 'https://explorer.binance.org/tx/%s',
blockExplorer: 'https://explorer.binance.org/block/%s',

denominations: [
// An array of Objects of the possible denominations for this currency
{
name: 'BNB',
multiplier: '100000000',
symbol: 'B'
}
],
metaTokens: [] // Deprecated

// https://github.com/bnb-chain/javascript-sdk/blob/master/docs/api-docs/classes/bncclient.md#transfer
memoOptions: [{ type: 'text', memoName: 'memo', maxLength: 128 }],

// Deprecated:
defaultSettings: {},
memoMaxLength: 128,
memoType: 'text',
metaTokens: []
}

export const binance = makeOuterPlugin<BinanceNetworkInfo, BinanceTools>({
Expand Down
10 changes: 10 additions & 0 deletions src/common/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
asArray,
asCodec,
asEither,
asMaybe,
asNumber,
Expand All @@ -11,6 +12,7 @@ import {
Cleaner
} from 'cleaners'
import { EdgeToken, EdgeTokenInfo, EdgeTransaction } from 'edge-core-js/types'
import { base16 } from 'rfc4648'

export const DATA_STORE_FILE = 'txEngineFolder/walletLocalData.json'
export const TXID_MAP_FILE = 'txEngineFolder/txidMap.json'
Expand Down Expand Up @@ -75,6 +77,14 @@ export const asSafeCommonWalletInfo = asWalletInfo(
asObject({ publicKey: asString })
)

/**
* A string of hex-encoded binary data.
*/
export const asBase16: Cleaner<Uint8Array> = asCodec(
raw => base16.parse(asString(raw)),
clean => base16.stringify(clean).toLowerCase()
)

export function asIntegerString(raw: unknown): string {
const clean = asString(raw)
if (!/^\d+$/.test(clean)) {
Expand Down
Loading

0 comments on commit 344976e

Please sign in to comment.