Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initial MTX Messaging layer proposal #71

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/bx.c
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,7 @@ int iszero(void *buff, int len)

int txmenu(BHEADER *bh, BTRAILER *bt)
{
char buff[80];
char buff[80], *memo;
TXQENTRY txq;
word32 j, k;
word32 tcount;
Expand Down Expand Up @@ -833,6 +833,10 @@ int txmenu(BHEADER *bh, BTRAILER *bt)
mtx = (MTX *) &txq;
for(j = k = 0; j < NR_DST; j++) {
if(iszero(mtx->dst[j].tag, ADDR_TAG_LEN)) break;
if(ismtxmemo(&txq) && *(mtx->dst[j].tag) == 0x00) {
memo = &(mtx->dst[j].tag[1]);
break;
}
if(++k >= 10) {
k = 0;
printf("Press RETURN or 'q': ");
Expand All @@ -858,6 +862,9 @@ int txmenu(BHEADER *bh, BTRAILER *bt)
printf("fee: %s\n", itoa64lj(txq.tx_fee, NULL, 9, 1));
printf("sig: 0x"); bytes2hex(txq.tx_sig, 32);

if(ismtxmemo(&txq))
printf("\nmemo: %s", memo);

printf("\nq=quit, g=goto TX, RETURN=next, b=back, "
"p=previous menu: "
);
Expand Down
35 changes: 34 additions & 1 deletion src/mtxval.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Date: 6 May 2019
*/

#include <ctype.h> /* for isprint() */

#define BAIL(m) { message = m; goto bail; }

Expand All @@ -15,13 +16,17 @@
* tx_val() sets fee parameter to Myfee and bval.c sets fee to Mfee.
* Returns 0 on valid, else error code.
*/
int mtx_val(MTX *mtx, word32 *fee)
int mtx_val(MTX *mtx, word32 *fee, char *outmemo)
{
int j, message;
byte total[8], mfees[8], *bp, *limit;
static byte addr[TXADDRLEN];
size_t memolen;
word32 memofees[2];
char *memo; /* temporary memo incase outmemo is NULL */

limit = &mtx->zeros[0];
memo = NULL;

/* Check that src and chg have tags.
* Check that src and chg have same tag.
Expand All @@ -44,6 +49,32 @@ int mtx_val(MTX *mtx, word32 *fee)
}
break;
}
/* dst[] tag with initial byte zero marks beginning of memo. */
if(ismtxmemo(mtx) && *(mtx->dst[j].tag) == 0x00) {
/* memo MUST have a null termination */
if(limit[-1] != 0) BAIL(11);
/* skip first byte, obtain and count memo */
memo = &(mtx->dst[j].tag[1]);
memolen = strlen(memo);
/* ensure memo exists, otherwise the wrong mtx type was used */
if(memolen == 0) BAIL(12);
/* check all message characters are printable characters */
for(bp = (byte *) memo; bp < limit; bp++) {
if(*bp == 0) break; /* a zero byte marks memo end */
if(isprint((int) *bp) == 0) BAIL(13);
}
/* ensure all bytes after the end of memo are zero */
for(bp++; bp < limit; bp++) if(*bp) BAIL(14);
/* count memofees and add to mfees */
memofees[1] = 0;
memofees[0] = 1 + ((memolen + 1) / sizeof(MDST));
if(mult64(fee, memofees, memofees)) BAIL(15); /* Mfee or Myfee */
if(add64(mfees, memofees, mfees)) BAIL(16);
/* pass memo pointer to outmemo */
outmemo = memo;
break;
}

if(iszero(mtx->dst[j].amount, 8)) BAIL(5); /* bad send amount */
/* no dst to src */
if(memcmp(mtx->dst[j].tag,
Expand All @@ -58,6 +89,8 @@ int mtx_val(MTX *mtx, word32 *fee)
if(tag_find(addr, NULL, NULL) != VEOK) mtx->zeros[j] = 1;
}
} /* end for j */
/* if the mtxmemo tx type was used, memo MUST exist */
if(ismtxmemo(mtx) && memo == NULL) BAIL(17);
/* Check tallies... */
if(cmp64(total, mtx->send_total) != 0) BAIL(9);
if(cmp64(mtx->tx_fee, mfees) < 0) BAIL(10);
Expand Down
6 changes: 5 additions & 1 deletion src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,11 @@ typedef struct {
/* for mtx */
/* takes TX * or TXQENTRY pointer */
#define ismtx(tx) ((tx)->dst_addr[2196] == 0x00 \
&& (tx)->dst_addr[2197] == 0x01)
&& ((tx)->dst_addr[2197] == 0x01 \
|| (tx)->dst_addr[2197] == 0x02))
/* identifies mtx/memo combination mtx */
#define ismtxmemo ((tx)->dst_addr[2196] == 0x00 \
&& (tx)->dst_addr[2197] == 0x02 )

#define ADDR_TAG_LEN 12
#define NR_DST 100 /* number of tags (MDST) in MTX dst[] */
Expand Down