import * as constants from "./Constants" const _tails = [null, Buffer.from([0x00]), Buffer.from([0x00, 0x00]), Buffer.from([0x00, 0x00, 0x00])]; export function getMemoBytes(memo: string): Buffer{ if(memo == ""){ return Buffer.concat([ constants.MEMO_TYPE_TEXT, //Memo_type = MEMO_TEXT constants.EMPTY_MEMO //Empty memo ]) }else{ let textBytes = stringToBytes(memo); let padd = Buffer.alloc(0); let tail = textBytes.length %4; if(tail != 0) { padd = _tails[4 - tail]; } let textLength = Buffer.from([textBytes.length]); while(textLength.length < 4){textLength = Buffer.concat([Buffer.alloc(1) , textLength])} return Buffer.concat([ constants.MEMO_TYPE_TEXT, //Memo_type = MEMO_TEXT textLength, textBytes, padd ]) } } function stringToBytes(data: string): Buffer{ var utf8 = unescape(encodeURIComponent(data)); var arr = []; for (var i = 0; i < utf8.length; i++) { arr.push(utf8.charCodeAt(i)); } return Buffer.from(arr); }