Skip to content

Commit e9bbbd8

Browse files
authored
add app migrate (#423)
1 parent e4f9da1 commit e9bbbd8

File tree

4 files changed

+25
-5
lines changed

4 files changed

+25
-5
lines changed

src/client/app.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import type {
1010
AppSecretResponse,
1111
AppBillingResponse,
1212
} from './types/app';
13+
import { Keystore } from './types';
1314
import { buildClient } from './utils/client';
15+
import { getOwnershipTransferTipBody, signEd25519PIN, signTipBody } from './utils';
1416

1517
// TODO add app api for developer document
1618
/**
@@ -20,7 +22,7 @@ import { buildClient } from './utils/client';
2022
* * Each Mixin user can only create two free apps
2123
* https://developers.mixin.one/
2224
*/
23-
export const AppKeystoreClient = (axiosInstance: AxiosInstance) => ({
25+
export const AppKeystoreClient = (axiosInstance: AxiosInstance, keystore: Keystore | undefined) => ({
2426
/** Get information of current user's a specific app */
2527
fetch: (appID: string): Promise<AppResponse> => axiosInstance.get<unknown, AppResponse>(`/apps/${appID}`),
2628

@@ -74,6 +76,19 @@ export const AppKeystoreClient = (axiosInstance: AxiosInstance) => ({
7476

7577
/** Removing from your share list */
7678
unfavorite: (appID: string): Promise<any> => axiosInstance.post<unknown, any>(`/apps/${appID}/unfavorite`),
79+
80+
// Migrate app to receiver id with the keystore of this app
81+
// pin is the spend private key of this app
82+
migrate: (pin: string, receiverID: string) => {
83+
if (!keystore) throw new Error('invalid keystore to migrate app');
84+
const msg = getOwnershipTransferTipBody(receiverID);
85+
const signedTipPin = signTipBody(pin, msg);
86+
const pin_base64 = signEd25519PIN(signedTipPin, keystore);
87+
return axiosInstance.post<unknown, AppResponse>(`/apps/${keystore.app_id}/transfer`, {
88+
pin_base64,
89+
user_id: receiverID,
90+
});
91+
},
7792
});
7893

7994
export const AppClient = buildClient(AppKeystoreClient);

src/client/mixin-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import { SafeKeystoreClient } from './safe';
2626

2727
const KeystoreClient = (axiosInstance: AxiosInstance, keystore: Keystore | undefined, config: HTTPConfig) => ({
2828
address: AddressKeystoreClient(axiosInstance, keystore),
29-
app: AppKeystoreClient(axiosInstance),
29+
app: AppKeystoreClient(axiosInstance, keystore),
3030
asset: AssetKeystoreClient(axiosInstance),
3131
blaze: BlazeKeystoreClient(keystore, config.blazeOptions),
3232
attachment: AttachmentKeystoreClient(axiosInstance),

src/client/types/utxo.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ export interface UtxoOutput {
1919
}
2020

2121
export interface KernelDeposit {
22-
chain: string;
23-
deposit_hash: string;
24-
deposit_index: number;
22+
chain: string;
23+
deposit_hash: string;
24+
deposit_index: number;
2525
}
2626

2727
export interface SafeUtxoOutput extends UtxoOutput {

src/client/utils/pin.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ export const getVerifyPinTipBody = (timestamp: number) => {
6969
return Buffer.from(msg);
7070
};
7171

72+
export const getOwnershipTransferTipBody = (user_id: string) => {
73+
const msg = `TIP:APP:OWNERSHIP:TRANSFER:${user_id}`;
74+
return sha256Hash(Buffer.from(msg));
75+
};
76+
7277
export const signTipBody = (pin: string, msg: Buffer) => {
7378
const signData = Buffer.from(ed25519.sign(msg, pin));
7479
return signData.toString('hex');

0 commit comments

Comments
 (0)