UNPKG

@swtc/common

Version:
94 lines 3.61 kB
import { convertHexToString, convertStringToHex } from "./serializer"; export function normalize_swt(tx_json, reverse = false) { if (tx_json.Fee >= 10) { tx_json.Fee = tx_json.Fee / 1000000; if (tx_json.Amount && !isNaN(tx_json.Amount)) { tx_json.Amount = `${tx_json.Amount / 1000000}`; } if (tx_json.SendMax && !isNaN(tx_json.SendMax)) { tx_json.SendMax = Number(tx_json.SendMax) / 1000000; } if (tx_json.TakerPays && !isNaN(tx_json.TakerPays)) { tx_json.TakerPays = Number(tx_json.TakerPays) / 1000000; } if (tx_json.TakerGets && !isNaN(tx_json.TakerGets)) { tx_json.TakerGets = Number(tx_json.TakerGets) / 1000000; } } if (reverse && tx_json.Fee < 10) { tx_json.Fee = tx_json.Fee * 1000000; if (tx_json.Amount && !isNaN(tx_json.Amount)) { tx_json.Amount = `${tx_json.Amount * 1000000}`; } if (tx_json.SendMax && !isNaN(tx_json.SendMax)) { tx_json.SendMax = Number(tx_json.SendMax) * 1000000; } if (tx_json.TakerPays && !isNaN(tx_json.TakerPays)) { tx_json.TakerPays = Number(tx_json.TakerPays) * 1000000; } if (tx_json.TakerGets && !isNaN(tx_json.TakerGets)) { tx_json.TakerGets = Number(tx_json.TakerGets) * 1000000; } } } export function normalize_memo(tx_json, reverse = false) { if (tx_json.Fee >= 10) { if (tx_json.Memos) { for (const memo of tx_json.Memos) { let data = memo.Memo.MemoData; let format = memo.Memo.MemoFormat; if (format === "json") { format = convertStringToHex(format); if (typeof data !== "string") { data = convertStringToHex(JSON.stringify(data)); } else { data = convertStringToHex(data); } } else if (format === "hex") { format = convertStringToHex(format); if (data.length % 2 > 0) { data = `${data}0`; } } else if (format) { if (/g-z/i.test(format)) { throw new Error("should be in hexdecial format now"); } } else { if (typeof data !== "string") { data = convertStringToHex(JSON.stringify(data)); format = convertStringToHex("json"); } else { data = convertStringToHex(data); } } memo.Memo.MemoData = data; if (format) { memo.Memo.MemoFormat = format; } } } } if (reverse) { if (tx_json.Memos) { for (const memo of tx_json.Memos) { let format = memo.Memo.MemoFormat; if (format) { format = convertHexToString(format); memo.Memo.MemoFormat = format; if (format !== "hex") { memo.Memo.MemoData = convertHexToString(memo.Memo.MemoData); } } else { memo.Memo.MemoData = convertHexToString(memo.Memo.MemoData); } } } } } //# sourceMappingURL=tx.js.map