Skip to content

Commit 31d70ff

Browse files
authored
Merge pull request #89 from MixinNetwork/bugfix/payment
Bugfix/payment
2 parents e868458 + 8366449 commit 31d70ff

File tree

9 files changed

+19
-42
lines changed

9 files changed

+19
-42
lines changed

example/mvm.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const { v4 } = require('uuid');
2-
const { MixinApi, MVMMainnet, Registry, StorageContract, getExtra, getExtraWithStorageKey } = require('@mixin.dev/mixin-node-sdk');
2+
const { MixinApi, MVMMainnet, Registry, StorageContract, getExtra, getExtraWithStorageKey, encodeMemo } = require('@mixin.dev/mixin-node-sdk');
33
const { keccak256 } = require('ethers/lib/utils');
44
const keystore = require('../keystore.json');
55

@@ -21,32 +21,32 @@ const storage = new StorageContract();
2121

2222
async function main() {
2323
const contractReadCount = {
24-
address: '0xAAD1736090A126687c318cB67B369A31eBcFc19a', // contract address
24+
address: '0x2E8f70631208A2EcFC6FA47Baf3Fde649963baC7', // contract address
2525
method: 'count', // contract function
2626
};
2727
const contractAddAnyCount = {
28-
address: '0xAAD1736090A126687c318cB67B369A31eBcFc19a', // contract address
28+
address: '0x2E8f70631208A2EcFC6FA47Baf3Fde649963baC7', // contract address
2929
method: 'addAny', // contract function
3030
types: ['uint256'], // function parameters type array
3131
values: [2], // function parameters value array
3232
};
3333
const contractAddOneCount = {
34-
address: '0xAAD1736090A126687c318cB67B369A31eBcFc19a', // contract address
34+
address: '0x2E8f70631208A2EcFC6FA47Baf3Fde649963baC7', // contract address
3535
method: 'addOne', // contract function
3636
};
3737
// contracts array to call
3838
const contracts = [contractReadCount, contractAddOneCount, contractReadCount];
3939

4040
// 1 build extra for contracts
4141
const extra = getExtra(contracts);
42-
let finalExtra = extra;
4342
console.log(extra.length);
44-
const key = keccak256(finalExtra);
43+
let finalExtra = extra;
4544

4645
// 2 if extra.length > 200
4746
// write keccak256 hash of extra as key to Storage contract,
4847
// then build a new extra with key
4948
if (extra.length > 200) {
49+
const key = keccak256(finalExtra);
5050
const { error } = storage.writeValue(finalExtra, key);
5151
if (error) throw new Error(error);
5252

@@ -64,7 +64,7 @@ async function main() {
6464
asset_id: '965e5c6e-434c-3fa9-b780-c50f43cd955c',
6565
amount: '0.00000001',
6666
trace_id: v4(),
67-
memo: finalExtra,
67+
memo: encodeMemo(finalExtra, MVMMainnet.Registry.PID),
6868
opponent_multisig: {
6969
receivers: MVMMainnet.MVMMembers,
7070
threshold: MVMMainnet.MVMThreshold,

example/mvm_api.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
const { MVMApi, MVMMainnet, MVMApiURI, getExtra } = require('@mixin.dev/mixin-node-sdk');
1+
const { MVMApi, MVMMainnet, MVMApiURI, getExtra, encodeMemo } = require('@mixin.dev/mixin-node-sdk');
22
const { v4 } = require('uuid');
33
const keystore = require('../keystore.json');
44

55
keystore.user_id = keystore.client_id;
66

7-
// FIXME: mvmapi for mainnet is not deploed yet
87
const mvmClient = MVMApi(MVMApiURI);
98

109
async function main() {
@@ -35,7 +34,7 @@ async function main() {
3534
asset_id: 'c94ac88f-4671-3976-b60a-09064f1811e8', // XIN asset_id
3635
amount: '0.00000001',
3736
trace_id: v4(),
38-
memo: extra,
37+
memo: encodeMemo(extra, MVMMainnet.Registry.PID),
3938
opponent_multisig: {
4039
receivers: MVMMainnet.MVMMembers,
4140
threshold: MVMMainnet.MVMThreshold,

src/client/http.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export function http(keystore?: Keystore, config?: RequestConfig): AxiosInstance
3232

3333
ins.interceptors.response.use(async (res: AxiosResponse) => {
3434
const { data, error } = res.data;
35-
if (error) throw new ResponseError(error.code, error.description, error.status, error.extra, res.headers['X-Request-Id'], error);
35+
if (error) throw new ResponseError(error.code, error.description, error.status, error.extra, res.headers['x-request-id'], error);
3636
return data;
3737
});
3838

src/client/payment.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,10 @@
11
import { AxiosInstance } from 'axios';
22
import { buildClient } from './utils/client';
33
import { PaymentRequestResponse, RawTransactionRequest, TransferRequest } from './types';
4-
import { MVMMainnet, encodeMemo } from '../mvm';
54

65
export const PaymentBaseClient = (axiosInstance: AxiosInstance) => ({
76
// Generate code id for transaction/transfer or verify payments by trace id
8-
request: (params: TransferRequest | RawTransactionRequest, process = MVMMainnet.Registry.PID) => {
9-
const paymentRequest = { ...params };
10-
if (paymentRequest.memo) {
11-
paymentRequest.memo = encodeMemo(paymentRequest.memo.slice(0, 2) === '0x' ? paymentRequest.memo.slice(2) : paymentRequest.memo, process);
12-
}
13-
14-
return axiosInstance.post<unknown, PaymentRequestResponse>('/payments', paymentRequest);
15-
},
7+
request: (params: TransferRequest | RawTransactionRequest) => axiosInstance.post<unknown, PaymentRequestResponse>('/payments', params),
168
});
179

1810
export const PaymentClient = buildClient(PaymentBaseClient);

src/client/transfer.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { TransferRequest } from './types/transfer';
55
import { GhostInputRequest, RawTransactionRequest, GhostKeysResponse } from './types/transaction';
66
import { signEd25519PIN } from './utils/pin';
77
import { buildClient } from './utils/client';
8-
import { encodeMemo, MVMMainnet } from '../mvm';
98

109
/**
1110
* Methods to transfer asset, withdraw and obtain transfer information
@@ -28,27 +27,19 @@ export const TransferKeystoreClient = (axiosInstance: AxiosInstance, keystore: K
2827
* If you encounter 500 error, do it over again
2928
* If you see the error 20119 password is wrong, do not try again. It is recommended to call the PIN Verification API to confirm
3029
*/
31-
toUser: (pin: string, params: TransferRequest, process = MVMMainnet.Registry.PID): Promise<SnapshotResponse> => {
32-
let memo = params.memo ? params.memo : '';
33-
memo = memo.slice(0, 2) === '0x' ? memo.slice(2) : memo;
34-
30+
toUser: (pin: string, params: TransferRequest): Promise<SnapshotResponse> => {
3531
const request: TransferRequest = {
3632
...params,
3733
pin: signEd25519PIN(pin, keystore),
38-
memo: encodeMemo(memo, process),
3934
};
4035
return axiosInstance.post<unknown, SnapshotResponse>('/transfers', request);
4136
},
4237

4338
/** Send raw transactions to the mainnet or multisig address */
44-
toAddress: (pin: string, params: RawTransactionRequest, process = MVMMainnet.Registry.PID): Promise<SnapshotResponse> => {
45-
let memo = params.memo ? params.memo : '';
46-
memo = memo.slice(0, 2) === '0x' ? memo.slice(2) : memo;
47-
39+
toAddress: (pin: string, params: RawTransactionRequest): Promise<SnapshotResponse> => {
4840
const request: RawTransactionRequest = {
4941
...params,
5042
pin: signEd25519PIN(pin, keystore),
51-
memo: encodeMemo(memo, process),
5243
};
5344
return axiosInstance.post<unknown, SnapshotResponse>('/transactions', request);
5445
},

src/mvm/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const MVMApi = (uri: string) => {
2121
instance.interceptors.response.use(
2222
async (res: AxiosResponse) => {
2323
const { error } = res.data;
24-
if (error) throw new ResponseError(error.code, error.description, error.status, error.extra, res.headers['X-Request-Id'], error);
24+
if (error) throw new ResponseError(error.code, error.description, error.status, error.extra, error.requestId, error);
2525
return res.data;
2626
},
2727
err => {

src/mvm/constant.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,4 @@ export const MVMTestnet = {
4949
};
5050

5151
export const MVMApiTestURI = 'https://api.test.mvm.dev';
52-
// FIXME: mvmapi for mainnet is not deploed yet
5352
export const MVMApiURI = 'https://api.mvm.dev';

src/mvm/utils.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,16 @@ const OperationPurposeGroupEvent = 1;
1010
// const OperationPurposeAddProcess = 11
1111
// const OperationPurposeCreditProcess = 12
1212

13-
export const buildMemo = (contract: string, memo: string, isDelegateCall: boolean) => {
14-
const op = isDelegateCall ? '01' : '00';
15-
const address = contract.toLowerCase().slice(2);
16-
return `${op}${address}${memo}`;
17-
};
18-
1913
// TODO: writeBytes twice why?
2014
export const encodeMemo = (extra: string, process: string): string => {
15+
const pureExtra = extra.slice(0, 2) === '0x' ? extra.slice(2) : extra;
16+
2117
const enc = new Encoder(Buffer.from([]));
2218
enc.writeInt(OperationPurposeGroupEvent);
2319
enc.writeUUID(process);
2420
enc.writeBytes(Buffer.from([]));
2521
enc.writeBytes(Buffer.from([]));
26-
enc.writeBytes(Buffer.from(extra, 'hex'));
22+
enc.writeBytes(Buffer.from(pureExtra, 'hex'));
2723
return base64RawURLEncode(enc.buf);
2824
};
2925

test/extra.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { v4 } from 'uuid';
22
import { utils } from 'ethers';
3-
import { MVMMainnet, getExtra } from '../src';
3+
import { MVMMainnet, getExtra, encodeMemo } from '../src';
44
import { client } from './common';
55

66
const value = utils.id('GrayPigeon');
@@ -32,7 +32,7 @@ describe('address', () => {
3232
receivers: MVMMainnet.MVMMembers,
3333
threshold: MVMMainnet.MVMThreshold,
3434
},
35-
memo: extra,
35+
memo: encodeMemo(extra, MVMMainnet.Registry.PID),
3636
};
3737

3838
const tx = await client.payment.request(input);

0 commit comments

Comments
 (0)