| 1050 | } |
| 1051 | |
| 1052 | static struct payment_result *tal_sendpay_result_from_json(const tal_t *ctx, |
| 1053 | const char *buffer, |
| 1054 | const jsmntok_t *toks) |
| 1055 | { |
| 1056 | const jsmntok_t *idtok = json_get_member(buffer, toks, "id"); |
| 1057 | const jsmntok_t *hashtok = json_get_member(buffer, toks, "payment_hash"); |
| 1058 | const jsmntok_t *partidtok = json_get_member(buffer, toks, "partid"); |
| 1059 | const jsmntok_t *senttok = json_get_member(buffer, toks, "amount_sent_msat"); |
| 1060 | const jsmntok_t *statustok = json_get_member(buffer, toks, "status"); |
| 1061 | const jsmntok_t *preimagetok = json_get_member(buffer, toks, "payment_preimage"); |
| 1062 | const jsmntok_t *codetok = json_get_member(buffer, toks, "code"); |
| 1063 | const jsmntok_t *datatok = json_get_member(buffer, toks, "data"); |
| 1064 | const jsmntok_t *erridxtok, *msgtok, *failcodetok, *rawmsgtok, |
| 1065 | *failcodenametok, *errchantok, *errnodetok, *errdirtok; |
| 1066 | struct payment_result *result; |
| 1067 | |
| 1068 | /* Check if we have an error and need to descend into data to get |
| 1069 | * details. */ |
| 1070 | if (codetok != NULL && datatok != NULL) { |
| 1071 | idtok = json_get_member(buffer, datatok, "id"); |
| 1072 | hashtok = json_get_member(buffer, datatok, "payment_hash"); |
| 1073 | partidtok = json_get_member(buffer, datatok, "partid"); |
| 1074 | senttok = json_get_member(buffer, datatok, "amount_sent_msat"); |
| 1075 | statustok = json_get_member(buffer, datatok, "status"); |
| 1076 | } |
| 1077 | |
| 1078 | /* Initial sanity checks, all these fields must exist. */ |
| 1079 | if (idtok == NULL || idtok->type != JSMN_PRIMITIVE || |
| 1080 | hashtok == NULL || hashtok->type != JSMN_STRING || |
| 1081 | senttok == NULL || |
| 1082 | statustok == NULL || statustok->type != JSMN_STRING) { |
| 1083 | return NULL; |
| 1084 | } |
| 1085 | |
| 1086 | result = tal(ctx, struct payment_result); |
| 1087 | |
| 1088 | if (codetok != NULL) |
| 1089 | json_to_u32(buffer, codetok, &result->code); |
| 1090 | else |
| 1091 | result->code = 0; |
| 1092 | |
| 1093 | /* If the partid is 0 it'd be omitted in waitsendpay, fix this here. */ |
| 1094 | if (partidtok != NULL) |
| 1095 | json_to_u32(buffer, partidtok, &result->partid); |
| 1096 | else |
| 1097 | result->partid = 0; |
| 1098 | |
| 1099 | json_to_u64(buffer, idtok, &result->id); |
| 1100 | json_to_msat(buffer, senttok, &result->amount_sent); |
| 1101 | if (json_tok_streq(buffer, statustok, "pending")) { |
| 1102 | result->state = PAYMENT_PENDING; |
| 1103 | } else if (json_tok_streq(buffer, statustok, "complete")) { |
| 1104 | result->state = PAYMENT_COMPLETE; |
| 1105 | } else if (json_tok_streq(buffer, statustok, "failed")) { |
| 1106 | result->state = PAYMENT_FAILED; |
| 1107 | } else { |
| 1108 | goto fail; |
| 1109 | } |
no test coverage detected