Skip to content
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

Connect & Multisig #521

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
18 changes: 18 additions & 0 deletions client/HubApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ import {
SetupSwapRequest,
SetupSwapResult,
RefundSwapRequest,
SignMultisigTransactionRequest,
PartialSignature,
ConnectAccountRequest,
ConnectedAccount,
} from './PublicRequestTypes';

export default class HubApi<
Expand Down Expand Up @@ -207,6 +211,20 @@ export default class HubApi<
return this._request(requestBehavior, RequestType.REFUND_SWAP, [request]);
}

public signMultisigTransaction<B extends BehaviorType = DB>(
request: Promise<SignMultisigTransactionRequest> | SignMultisigTransactionRequest,
requestBehavior: RequestBehavior<B> = this._defaultBehavior as any,
): Promise<B extends BehaviorType.REDIRECT ? void : PartialSignature> {
return this._request(requestBehavior, RequestType.SIGN_MULTISIG_TRANSACTION, [request]);
}

public connectAccount<B extends BehaviorType = DB>(
request: Promise<ConnectAccountRequest> | ConnectAccountRequest,
requestBehavior: RequestBehavior<B> = this._defaultBehavior as any,
): Promise<B extends BehaviorType.REDIRECT ? void : ConnectedAccount> {
return this._request(requestBehavior, RequestType.CONNECT_ACCOUNT, [request]);
}

/**
* Account Management
*
Expand Down
76 changes: 74 additions & 2 deletions client/PublicRequestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export enum RequestType {
CHECKOUT = 'checkout',
SIGN_MESSAGE = 'sign-message',
SIGN_TRANSACTION = 'sign-transaction',
SIGN_MULTISIG_TRANSACTION = 'sign-multisig-transaction',
ONBOARD = 'onboard',
SIGNUP = 'signup',
LOGIN = 'login',
Expand All @@ -35,6 +36,7 @@ export enum RequestType {
ACTIVATE_POLYGON = 'activate-polygon',
SETUP_SWAP = 'setup-swap',
REFUND_SWAP = 'refund-swap',
CONNECT_ACCOUNT = 'connect-account',
}

export type Bytes = Uint8Array | string;
Expand Down Expand Up @@ -241,6 +243,45 @@ export interface SignedTransaction {
};
}

export interface EncryptionKeyParams {
kdf: string;
iterations: number;
keySize: number;
}

export interface MultisigInfo {
publicKeys: Bytes[];
numberOfSigners: number;
signerPublicKeys?: Bytes[]; // Can be omitted when all publicKeys need to sign
secret: {
aggregatedSecret: Bytes;
} | {
encryptedSecrets: Bytes[];
bScalar: Bytes;
keyParams: EncryptionKeyParams;
};
aggregatedCommitment: Bytes;
userName?: string;
}

export interface SignMultisigTransactionRequest extends BasicRequest {
signer: string; // Address

sender: string;
senderType?: Nimiq.Account.Type;
senderLabel: string;
recipient: string;
recipientType?: Nimiq.Account.Type;
recipientLabel?: string;
value: number;
fee?: number;
extraData?: Bytes;
flags?: number;
validityStartHeight: number; // FIXME To be made optional when hub has its own network

multisigConfig: MultisigInfo;
}

export interface NimiqHtlcCreationInstructions {
type: 'NIM';
sender: string; // My address, must be redeem address of HTLC, or if contract, its owner must be redeem address
Expand Down Expand Up @@ -446,6 +487,8 @@ export interface SignedMessage {
signature: Uint8Array;
}

export type PartialSignature = SignedMessage;

export interface Address {
address: string; // Userfriendly address
label: string;
Expand Down Expand Up @@ -564,6 +607,29 @@ export interface ManageCashlinkRequest extends BasicRequest {
cashlinkAddress: string;
}

export interface ConnectAccountRequest extends BasicRequest {
appLogoUrl: string;
permissions: RequestType[];
requestedKeyPaths: string[];
challenge: string;
}

export interface ConnectedAccount {
signatures: SignedMessage[];
encryptionKey: {
format: 'spki',
keyData: Uint8Array,
algorithm: { name: string, hash: string },
keyUsages: ['encrypt'],
keyParams: EncryptionKeyParams,
};
account: {
label: string;
type: string;
// permissions: RequestType[];
};
}

/**
* Bitcoin
*/
Expand Down Expand Up @@ -643,6 +709,7 @@ export interface SignedPolygonTransaction {
}

export type RpcRequest = SignTransactionRequest
| SignMultisigTransactionRequest
| CreateCashlinkRequest
| ManageCashlinkRequest
| CheckoutRequest
Expand All @@ -657,9 +724,11 @@ export type RpcRequest = SignTransactionRequest
| AddBtcAddressesRequest
| SignPolygonTransactionRequest
| SetupSwapRequest
| RefundSwapRequest;
| RefundSwapRequest
| ConnectAccountRequest;

export type RpcResult = SignedTransaction
| PartialSignature
| Account
| Account[]
| SimpleResult
Expand All @@ -672,7 +741,8 @@ export type RpcResult = SignedTransaction
| SignedBtcTransaction
| AddBtcAddressesResult
| SignedPolygonTransaction
| SetupSwapResult;
| SetupSwapResult
| ConnectedAccount;

export type ResultByRequestType<T> =
T extends RequestType.RENAME ? Account :
Expand All @@ -682,6 +752,7 @@ export type ResultByRequestType<T> =
T extends RequestType.CHOOSE_ADDRESS ? ChooseAddressResult :
T extends RequestType.ADD_ADDRESS ? Address :
T extends RequestType.SIGN_TRANSACTION ? SignedTransaction :
T extends RequestType.SIGN_MULTISIG_TRANSACTION ? PartialSignature :
T extends RequestType.CHECKOUT ? SignedTransaction | SimpleResult :
T extends RequestType.SIGN_MESSAGE ? SignedMessage :
T extends RequestType.LOGOUT | RequestType.CHANGE_PASSWORD ? SimpleResult :
Expand All @@ -693,4 +764,5 @@ export type ResultByRequestType<T> =
T extends RequestType.ACTIVATE_POLYGON ? Account :
T extends RequestType.ADD_BTC_ADDRESSES ? AddBtcAddressesResult :
T extends RequestType.SETUP_SWAP ? SetupSwapResult :
T extends RequestType.CONNECT_ACCOUNT ? ConnectedAccount :
never;
Loading
Loading