Skip to content

Commit

Permalink
Rename message to transfer ID
Browse files Browse the repository at this point in the history
The message ID is actually identifying a transfer. This gets especially
clear when considering that it gets generated by the prepare payment
function.
Thereby also the extra data field has been renamed from `mgId` to
`trId`.

Related to: #375
  • Loading branch information
weilbith authored and berndbohmeier committed May 12, 2020
1 parent e768443 commit 5e9c2fe
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 77 deletions.
10 changes: 5 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
where the fee payer is the loaded user
- Add `extraData` to `TransferDetails`, it contains the raw `extraData` that was present in the transfer
for which details are returned
- Add `paymentRequestId` and `messageId` to `TransferDetails` corresponding to the decoded `extraData`
- Add option to `Payment.preparePayment` function `options.addMessageId: boolean = true` that signals whether
a messageId should be generated and added to the payment's extraData.
- Add `messageId` to the returned values of `Payment.preparePayment`
- Add `paymentRequestId` and `transferId` to `TransferDetails` corresponding to the decoded `extraData`
- Add option to `Payment.preparePayment` function `options.addTransferId: boolean = true` that signals whether
a `transferId` should be generated and added to the payment's extraData.
- Add `transferId` to the returned values of `Payment.preparePayment`
- Add function `Payment.confirmPayment` to confirm any `PaymentTxObject` returned by `prepare`
and potentially send a payment message along with the payment
- Add function `Messaging.paymentMessage` to send a payment message for a messageId to a counterparty address
- Add function `Messaging.paymentMessage` to send a payment message for a `transferId` to a counterparty address

### Deprecated

Expand Down
6 changes: 3 additions & 3 deletions src/Messaging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,18 @@ export class Messaging {
/**
* Sends a payment message to given `counterParty` and returns created message.
* @param counterPartyAddress Address of counter party.
* @param messageId Message id of the payment
* @param transferId Transfer ID of the payment
* @param subject Subject that will be sent to the counterparty
*/
public async paymentMessage(
counterPartyAddress: string,
messageId: string,
transferId: string,
subject: string
): Promise<PaymentMessage> {
const type = 'PaymentMessage'
const message = {
type,
messageId,
transferId,
subject
}
await this.sendMessage(counterPartyAddress, type, message)
Expand Down
42 changes: 21 additions & 21 deletions src/Payment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,34 +101,34 @@ export class Payment {
* @param options.feePayer Either `sender` or `receiver`. Specifies who pays network fees.
* @param options.extraData Extra data that will appear in the Transfer event when successful.
* @param options.paymentRequestId Payment request identifier that gets encoded to the extra data.
* @param options.addMessageId Whether a message id should be added to the payment, default to true.
* @param options.addTransferId Whether a transfer id should be added to the payment, default to true.
*/
public async prepare(
networkAddress: string,
receiverAddress: string,
value: number | string,
options: PaymentOptions = { addMessageId: true }
options: PaymentOptions = { addTransferId: true }
): Promise<PaymentTxObject> {
const {
gasPrice,
gasLimit,
networkDecimals,
extraData,
paymentRequestId,
addMessageId
addTransferId
} = options
const decimals = await this.currencyNetwork.getDecimals(networkAddress, {
networkDecimals
})
let messageId: string
if (addMessageId) {
let transferId: string
if (addTransferId) {
// 19 decimals fit in 64 bits
messageId = utils.convertToHexString(utils.generateRandomNumber(19))
transferId = utils.convertToHexString(utils.generateRandomNumber(19))
}
const encodedExtraData: string = encode({
extraData,
paymentRequestId,
messageId
transferId
})

const { path, maxFees, feePayer } = await this.getTransferPathInfo(
Expand Down Expand Up @@ -174,7 +174,7 @@ export class Payment {
path,
receiverAddress,
rawTx,
messageId: messageId || null
transferId: transferId || null
}
} else {
throw new Error('Could not find a path with enough capacity.')
Expand Down Expand Up @@ -293,28 +293,28 @@ export class Payment {

/**
* Signs the rawTx provided as returned by `prepare`
* and sends the signed transaction as well as the message with messageId
* and sends the signed transaction as well as the message with transferId
* Can be directly given a `PaymentTxObject` object as returned by `prepare`
* @param rawTx Raw transaction object.
* @param receiverAddress Address of the receiver of the message for the payment.
* @param messageId The messageId returned when preparing a payment with message.
* @param transferId The transfer id returned when preparing a payment with message.
* @param message The message to be sent.
*/
public async confirmPayment(
{
rawTx,
receiverAddress,
messageId
}: { rawTx: RawTxObject; receiverAddress: string; messageId?: string },
transferId
}: { rawTx: RawTxObject; receiverAddress: string; transferId?: string },
message?: string
) {
if (message) {
if (!messageId) {
if (!transferId) {
throw new Error(
'Cannot use `message` if payment was prepared without `messageId`.'
'Cannot use `message` if payment was prepared without `transferId`.'
)
}
await this.messaging.paymentMessage(receiverAddress, messageId, message)
await this.messaging.paymentMessage(receiverAddress, transferId, message)
}
return this.transaction.confirm(rawTx)
}
Expand Down Expand Up @@ -513,21 +513,21 @@ export class Payment {
function encode({
extraData,
paymentRequestId,
messageId
transferId
}: {
extraData: string
paymentRequestId: string
messageId?: string
transferId?: string
}) {
if (extraData && (paymentRequestId || messageId)) {
if (extraData && (paymentRequestId || transferId)) {
throw Error(
'Can not encode extraData and paymentRequestId or messageId at the same time currently'
'Can not encode extraData and paymentRequestId or transferId at the same time currently'
)
}
if (extraData) {
return extraData
} else if (paymentRequestId || messageId) {
return encodeExtraData({ paymentRequestId, messageId })
} else if (paymentRequestId || transferId) {
return encodeExtraData({ paymentRequestId, transferId })
} else {
return '0x'
}
Expand Down
18 changes: 9 additions & 9 deletions src/extraData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@ export const MSG_PACK_ID = '0x544c4d50' // hex encoded "TLMP" (Trustlines Messag

export interface ExtraData {
paymentRequestId?: string
messageId?: string
transferId?: string
}

interface ExtraDataEncoding {
prId?: ArrayBufferView
mgId?: ArrayBufferView
trId?: ArrayBufferView
}

export function encode(extraData: ExtraData): string {
const { paymentRequestId, messageId } = extraData
if (!paymentRequestId && !messageId) {
const { paymentRequestId, transferId } = extraData
if (!paymentRequestId && !transferId) {
return '0x'
} else {
const data: ExtraDataEncoding = {}
if (paymentRequestId) {
data.prId = hexToArray(paymentRequestId)
}
if (messageId) {
data.mgId = hexToArray(messageId)
if (transferId) {
data.trId = hexToArray(transferId)
}
const enc = msgPack.encode(data)
return MSG_PACK_ID + remove0xPrefix(arrayToHex(enc))
Expand Down Expand Up @@ -52,7 +52,7 @@ export function decode(encodedExtraData: string): ExtraData {

return {
paymentRequestId: arrayToHex(extraData.prId),
messageId: arrayToHex(extraData.mgId)
transferId: arrayToHex(extraData.trId)
}
}

Expand Down Expand Up @@ -85,8 +85,8 @@ export function processExtraData(object) {
object.paymentRequestId = decodedExtraData
? decodedExtraData.paymentRequestId || null
: null
object.messageId = decodedExtraData
? decodedExtraData.messageId || null
object.transferId = decodedExtraData
? decodedExtraData.transferId || null
: null
}
return object
Expand Down
10 changes: 5 additions & 5 deletions src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface PaymentOptions extends TLOptions {
feePayer?: FeePayer
extraData?: string
paymentRequestId?: string
addMessageId?: boolean
addTransferId?: boolean
}

export interface TrustlineUpdateOptions extends TLOptions {
Expand Down Expand Up @@ -137,7 +137,7 @@ export interface NetworkTransferEvent extends NetworkEvent {
amount: Amount
extraData: string
paymentRequestId: string
messageId: string
transferId: string
}

export interface NetworkTrustlineUpdateEventRaw extends NetworkEvent {
Expand Down Expand Up @@ -226,7 +226,7 @@ export interface PaymentRequestDeclineMessage {

export interface PaymentMessage {
type: string
messageId: string
transferId: string
subject: string
}

Expand Down Expand Up @@ -436,7 +436,7 @@ export interface PaymentTxObject extends TxObject {
receiverAddress: string
feePayer: FeePayer
maxFees: Amount
messageId?: string
transferId?: string
}

export enum FeePayer {
Expand Down Expand Up @@ -697,7 +697,7 @@ export interface TransferDetails {
feesPaid: Amount[]
extraData: string
paymentRequestId: string
messageId: string
transferId: string
}

export interface TransferIdentifier {
Expand Down
14 changes: 7 additions & 7 deletions tests/e2e/Event.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('e2e', () => {
1.5,
{
extraData,
addMessageId: false
addTransferId: false
}
)
await tl1.payment.confirm(rawTx)
Expand All @@ -98,7 +98,7 @@ describe('e2e', () => {
expect(last.amount).to.have.keys('raw', 'value', 'decimals')
expect(last.amount.value).to.eq('1.5')
expect(last.extraData).to.eq(extraData)
expect(last.messageId).to.be.a('null')
expect(last.transferId).to.be.a('null')
expect(last.paymentRequestId).to.be.a('null')
expect(last.logIndex).to.be.a('number')
expect(last.blockHash).to.be.a('string')
Expand All @@ -110,7 +110,7 @@ describe('e2e', () => {
let acceptTxHash
let cancelUpdateTxHash
let tlTransferTxHash
let tlTransferMessageId
let tlTransferTransferId
let depositTxHash
let withdrawTxHash
let transferTxHash
Expand Down Expand Up @@ -164,7 +164,7 @@ describe('e2e', () => {
1,
{ paymentRequestId }
)
tlTransferMessageId = tlTransferTx.messageId
tlTransferTransferId = tlTransferTx.transferId
tlTransferTxHash = await tl1.payment.confirm(tlTransferTx.rawTx)
await wait()

Expand Down Expand Up @@ -338,8 +338,8 @@ describe('e2e', () => {
(tlTransferEvents[0] as NetworkTransferEvent).paymentRequestId
).to.equal(paymentRequestId)
expect(
(tlTransferEvents[0] as NetworkTransferEvent).messageId
).to.equal(tlTransferMessageId)
(tlTransferEvents[0] as NetworkTransferEvent).transferId
).to.equal(tlTransferTransferId)

// events thrown on deposit
const depositEvents = allEvents.filter(
Expand Down Expand Up @@ -564,7 +564,7 @@ describe('e2e', () => {
'paymentRequestId',
paymentRequestId
)
expect(transferEvent).to.have.property('messageId')
expect(transferEvent).to.have.property('transferId')
expect(transferEvent.blockHash).to.be.a('string')
expect(transferEvent.logIndex).be.a('number')

Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/Messaging.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('e2e', () => {
'test message'
)
expect(sentPaymentMessage.type).to.equal('PaymentMessage')
expect(sentPaymentMessage.messageId).to.equal('0x10')
expect(sentPaymentMessage.transferId).to.equal('0x10')
expect(sentPaymentMessage.subject).to.equal('test message')
})
})
Expand Down Expand Up @@ -177,7 +177,7 @@ describe('e2e', () => {
it('should receive payment message', async () => {
expect(messages[4]).to.have.property('type', 'PaymentMessage')
expect(messages[4].timestamp).to.be.a('number')
expect(messages[4].messageId).to.be.a('string')
expect(messages[4].transferId).to.be.a('string')
expect(messages[4]).to.have.property('subject', 'payment message')
})

Expand Down
Loading

0 comments on commit 5e9c2fe

Please sign in to comment.