Skip to content

Bugfix/payment #89

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

Merged
merged 8 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions example/mvm.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { v4 } = require('uuid');
const { MixinApi, MVMMainnet, Registry, StorageContract, getExtra, getExtraWithStorageKey } = require('@mixin.dev/mixin-node-sdk');
const { MixinApi, MVMMainnet, Registry, StorageContract, getExtra, getExtraWithStorageKey, encodeMemo } = require('@mixin.dev/mixin-node-sdk');
const { keccak256 } = require('ethers/lib/utils');
const keystore = require('../keystore.json');

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

async function main() {
const contractReadCount = {
address: '0xAAD1736090A126687c318cB67B369A31eBcFc19a', // contract address
address: '0x2E8f70631208A2EcFC6FA47Baf3Fde649963baC7', // contract address
method: 'count', // contract function
};
const contractAddAnyCount = {
address: '0xAAD1736090A126687c318cB67B369A31eBcFc19a', // contract address
address: '0x2E8f70631208A2EcFC6FA47Baf3Fde649963baC7', // contract address
method: 'addAny', // contract function
types: ['uint256'], // function parameters type array
values: [2], // function parameters value array
};
const contractAddOneCount = {
address: '0xAAD1736090A126687c318cB67B369A31eBcFc19a', // contract address
address: '0x2E8f70631208A2EcFC6FA47Baf3Fde649963baC7', // contract address
method: 'addOne', // contract function
};
// contracts array to call
const contracts = [contractReadCount, contractAddOneCount, contractReadCount];

// 1 build extra for contracts
const extra = getExtra(contracts);
let finalExtra = extra;
console.log(extra.length);
const key = keccak256(finalExtra);
let finalExtra = extra;

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

Expand All @@ -64,7 +64,7 @@ async function main() {
asset_id: '965e5c6e-434c-3fa9-b780-c50f43cd955c',
amount: '0.00000001',
trace_id: v4(),
memo: finalExtra,
memo: encodeMemo(finalExtra, MVMMainnet.Registry.PID),
opponent_multisig: {
receivers: MVMMainnet.MVMMembers,
threshold: MVMMainnet.MVMThreshold,
Expand Down
5 changes: 2 additions & 3 deletions example/mvm_api.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
const { MVMApi, MVMMainnet, MVMApiURI, getExtra } = require('@mixin.dev/mixin-node-sdk');
const { MVMApi, MVMMainnet, MVMApiURI, getExtra, encodeMemo } = require('@mixin.dev/mixin-node-sdk');
const { v4 } = require('uuid');
const keystore = require('../keystore.json');

keystore.user_id = keystore.client_id;

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

async function main() {
Expand Down Expand Up @@ -35,7 +34,7 @@ async function main() {
asset_id: 'c94ac88f-4671-3976-b60a-09064f1811e8', // XIN asset_id
amount: '0.00000001',
trace_id: v4(),
memo: extra,
memo: encodeMemo(extra, MVMMainnet.Registry.PID),
opponent_multisig: {
receivers: MVMMainnet.MVMMembers,
threshold: MVMMainnet.MVMThreshold,
Expand Down
2 changes: 1 addition & 1 deletion src/client/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function http(keystore?: Keystore, config?: RequestConfig): AxiosInstance

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

Expand Down
10 changes: 1 addition & 9 deletions src/client/payment.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
import { AxiosInstance } from 'axios';
import { buildClient } from './utils/client';
import { PaymentRequestResponse, RawTransactionRequest, TransferRequest } from './types';
import { MVMMainnet, encodeMemo } from '../mvm';

export const PaymentBaseClient = (axiosInstance: AxiosInstance) => ({
// Generate code id for transaction/transfer or verify payments by trace id
request: (params: TransferRequest | RawTransactionRequest, process = MVMMainnet.Registry.PID) => {
const paymentRequest = { ...params };
if (paymentRequest.memo) {
paymentRequest.memo = encodeMemo(paymentRequest.memo.slice(0, 2) === '0x' ? paymentRequest.memo.slice(2) : paymentRequest.memo, process);
}

return axiosInstance.post<unknown, PaymentRequestResponse>('/payments', paymentRequest);
},
request: (params: TransferRequest | RawTransactionRequest) => axiosInstance.post<unknown, PaymentRequestResponse>('/payments', params),
});

export const PaymentClient = buildClient(PaymentBaseClient);
Expand Down
13 changes: 2 additions & 11 deletions src/client/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { TransferRequest } from './types/transfer';
import { GhostInputRequest, RawTransactionRequest, GhostKeysResponse } from './types/transaction';
import { signEd25519PIN } from './utils/pin';
import { buildClient } from './utils/client';
import { encodeMemo, MVMMainnet } from '../mvm';

/**
* Methods to transfer asset, withdraw and obtain transfer information
Expand All @@ -28,27 +27,19 @@ export const TransferKeystoreClient = (axiosInstance: AxiosInstance, keystore: K
* If you encounter 500 error, do it over again
* If you see the error 20119 password is wrong, do not try again. It is recommended to call the PIN Verification API to confirm
*/
toUser: (pin: string, params: TransferRequest, process = MVMMainnet.Registry.PID): Promise<SnapshotResponse> => {
let memo = params.memo ? params.memo : '';
memo = memo.slice(0, 2) === '0x' ? memo.slice(2) : memo;

toUser: (pin: string, params: TransferRequest): Promise<SnapshotResponse> => {
const request: TransferRequest = {
...params,
pin: signEd25519PIN(pin, keystore),
memo: encodeMemo(memo, process),
};
return axiosInstance.post<unknown, SnapshotResponse>('/transfers', request);
},

/** Send raw transactions to the mainnet or multisig address */
toAddress: (pin: string, params: RawTransactionRequest, process = MVMMainnet.Registry.PID): Promise<SnapshotResponse> => {
let memo = params.memo ? params.memo : '';
memo = memo.slice(0, 2) === '0x' ? memo.slice(2) : memo;

toAddress: (pin: string, params: RawTransactionRequest): Promise<SnapshotResponse> => {
const request: RawTransactionRequest = {
...params,
pin: signEd25519PIN(pin, keystore),
memo: encodeMemo(memo, process),
};
return axiosInstance.post<unknown, SnapshotResponse>('/transactions', request);
},
Expand Down
2 changes: 1 addition & 1 deletion src/mvm/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const MVMApi = (uri: string) => {
instance.interceptors.response.use(
async (res: AxiosResponse) => {
const { error } = res.data;
if (error) throw new ResponseError(error.code, error.description, error.status, error.extra, res.headers['X-Request-Id'], error);
if (error) throw new ResponseError(error.code, error.description, error.status, error.extra, error.requestId, error);
return res.data;
},
err => {
Expand Down
1 change: 0 additions & 1 deletion src/mvm/constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,4 @@ export const MVMTestnet = {
};

export const MVMApiTestURI = 'https://api.test.mvm.dev';
// FIXME: mvmapi for mainnet is not deploed yet
export const MVMApiURI = 'https://api.mvm.dev';
10 changes: 3 additions & 7 deletions src/mvm/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,16 @@ const OperationPurposeGroupEvent = 1;
// const OperationPurposeAddProcess = 11
// const OperationPurposeCreditProcess = 12

export const buildMemo = (contract: string, memo: string, isDelegateCall: boolean) => {
const op = isDelegateCall ? '01' : '00';
const address = contract.toLowerCase().slice(2);
return `${op}${address}${memo}`;
};

// TODO: writeBytes twice why?
export const encodeMemo = (extra: string, process: string): string => {
const pureExtra = extra.slice(0, 2) === '0x' ? extra.slice(2) : extra;

const enc = new Encoder(Buffer.from([]));
enc.writeInt(OperationPurposeGroupEvent);
enc.writeUUID(process);
enc.writeBytes(Buffer.from([]));
enc.writeBytes(Buffer.from([]));
enc.writeBytes(Buffer.from(extra, 'hex'));
enc.writeBytes(Buffer.from(pureExtra, 'hex'));
return base64RawURLEncode(enc.buf);
};

Expand Down
4 changes: 2 additions & 2 deletions test/extra.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { v4 } from 'uuid';
import { utils } from 'ethers';
import { MVMMainnet, getExtra } from '../src';
import { MVMMainnet, getExtra, encodeMemo } from '../src';
import { client } from './common';

const value = utils.id('GrayPigeon');
Expand Down Expand Up @@ -32,7 +32,7 @@ describe('address', () => {
receivers: MVMMainnet.MVMMembers,
threshold: MVMMainnet.MVMThreshold,
},
memo: extra,
memo: encodeMemo(extra, MVMMainnet.Registry.PID),
};

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