Skip to content

Commit

Permalink
Allow passing two staking transactions for signing
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Feb 8, 2024
1 parent 60a2209 commit a1bf5f2
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 43 deletions.
2 changes: 1 addition & 1 deletion client/HubApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export default class HubApi<
public signStaking<B extends BehaviorType = DB>(
request: Promise<SignStakingRequest> | SignStakingRequest,
requestBehavior: RequestBehavior<B> = this._defaultBehavior as any,
): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction> {
): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction[]> {
return this._request(requestBehavior, RequestType.SIGN_STAKING, [request]);
}

Expand Down
5 changes: 3 additions & 2 deletions client/PublicRequestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export interface SignTransactionRequest extends BasicRequest {
export interface SignStakingRequest extends BasicRequest {
senderLabel?: string;
recipientLabel?: string;
transaction: Uint8Array;
transaction: Uint8Array | Uint8Array[];
}

export interface NimiqCheckoutRequest extends BasicRequest {
Expand Down Expand Up @@ -642,6 +642,7 @@ export type RpcRequest = SignTransactionRequest
| RefundSwapRequest;

export type RpcResult = SignedTransaction
| SignedTransaction[]
| Account
| Account[]
| SimpleResult
Expand All @@ -663,7 +664,7 @@ export type ResultByRequestType<T> =
T extends RequestType.LIST_CASHLINKS ? Cashlink[] :
T extends RequestType.CHOOSE_ADDRESS | RequestType.ADD_ADDRESS ? Address :
T extends RequestType.SIGN_TRANSACTION ? SignedTransaction :
T extends RequestType.SIGN_STAKING ? SignedTransaction :
T extends RequestType.SIGN_STAKING ? SignedTransaction[] :
T extends RequestType.CHECKOUT ? SignedTransaction | SimpleResult :
T extends RequestType.SIGN_MESSAGE ? SignedMessage :
T extends RequestType.LOGOUT | RequestType.CHANGE_PASSWORD ? SimpleResult :
Expand Down
2 changes: 1 addition & 1 deletion client/dist/src/HubApi.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class HubApi<DB extends BehaviorType = BehaviorType.POPUP, IB ext
} ? SimpleResult | SignedTransaction : SignedTransaction>;
chooseAddress<B extends BehaviorType = DB>(request: Promise<ChooseAddressRequest> | ChooseAddressRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : ChooseAddressResult>;
signTransaction<B extends BehaviorType = DB>(request: Promise<SignTransactionRequest> | SignTransactionRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction>;
signStaking<B extends BehaviorType = DB>(request: Promise<SignStakingRequest> | SignStakingRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction>;
signStaking<B extends BehaviorType = DB>(request: Promise<SignStakingRequest> | SignStakingRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedTransaction[]>;
signMessage<B extends BehaviorType = DB>(request: Promise<SignMessageRequest> | SignMessageRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedMessage>;
signBtcTransaction<B extends BehaviorType = DB>(request: Promise<SignBtcTransactionRequest> | SignBtcTransactionRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedBtcTransaction>;
signPolygonTransaction<B extends BehaviorType = DB>(request: Promise<SignPolygonTransactionRequest> | SignPolygonTransactionRequest, requestBehavior?: RequestBehavior<B>): Promise<B extends BehaviorType.REDIRECT ? void : SignedPolygonTransaction>;
Expand Down
6 changes: 3 additions & 3 deletions client/dist/src/PublicRequestTypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export interface SignTransactionRequest extends BasicRequest {
export interface SignStakingRequest extends BasicRequest {
senderLabel?: string;
recipientLabel?: string;
transaction: Uint8Array;
transaction: Uint8Array | Uint8Array[];
}
export interface NimiqCheckoutRequest extends BasicRequest {
version?: 1;
Expand Down Expand Up @@ -511,5 +511,5 @@ export interface SignedPolygonTransaction {
signature: string;
}
export declare type RpcRequest = SignTransactionRequest | SignStakingRequest | CreateCashlinkRequest | ManageCashlinkRequest | CheckoutRequest | BasicRequest | SimpleRequest | ChooseAddressRequest | OnboardRequest | RenameRequest | SignMessageRequest | ExportRequest | SignBtcTransactionRequest | AddBtcAddressesRequest | SignPolygonTransactionRequest | SetupSwapRequest | RefundSwapRequest;
export declare type RpcResult = SignedTransaction | Account | Account[] | SimpleResult | ChooseAddressResult | Address | Cashlink | Cashlink[] | SignedMessage | ExportResult | SignedBtcTransaction | AddBtcAddressesResult | SignedPolygonTransaction | SetupSwapResult;
export declare type ResultByRequestType<T> = T extends RequestType.RENAME ? Account : T extends RequestType.ONBOARD | RequestType.SIGNUP | RequestType.LOGIN | RequestType.MIGRATE | RequestType.LIST ? Account[] : T extends RequestType.LIST_CASHLINKS ? Cashlink[] : T extends RequestType.CHOOSE_ADDRESS | RequestType.ADD_ADDRESS ? Address : T extends RequestType.SIGN_TRANSACTION ? SignedTransaction : T extends RequestType.SIGN_STAKING ? SignedTransaction : T extends RequestType.CHECKOUT ? SignedTransaction | SimpleResult : T extends RequestType.SIGN_MESSAGE ? SignedMessage : T extends RequestType.LOGOUT | RequestType.CHANGE_PASSWORD ? SimpleResult : T extends RequestType.EXPORT ? ExportResult : T extends RequestType.CREATE_CASHLINK | RequestType.MANAGE_CASHLINK ? Cashlink : T extends RequestType.SIGN_BTC_TRANSACTION ? SignedBtcTransaction : T extends RequestType.SIGN_POLYGON_TRANSACTION ? SignedPolygonTransaction : T extends RequestType.ACTIVATE_BITCOIN ? Account : T extends RequestType.ACTIVATE_POLYGON ? Account : T extends RequestType.ADD_BTC_ADDRESSES ? AddBtcAddressesResult : T extends RequestType.SETUP_SWAP ? SetupSwapResult : never;
export declare type RpcResult = SignedTransaction | SignedTransaction[] | Account | Account[] | SimpleResult | ChooseAddressResult | Address | Cashlink | Cashlink[] | SignedMessage | ExportResult | SignedBtcTransaction | AddBtcAddressesResult | SignedPolygonTransaction | SetupSwapResult;
export declare type ResultByRequestType<T> = T extends RequestType.RENAME ? Account : T extends RequestType.ONBOARD | RequestType.SIGNUP | RequestType.LOGIN | RequestType.MIGRATE | RequestType.LIST ? Account[] : T extends RequestType.LIST_CASHLINKS ? Cashlink[] : T extends RequestType.CHOOSE_ADDRESS | RequestType.ADD_ADDRESS ? Address : T extends RequestType.SIGN_TRANSACTION ? SignedTransaction : T extends RequestType.SIGN_STAKING ? SignedTransaction[] : T extends RequestType.CHECKOUT ? SignedTransaction | SimpleResult : T extends RequestType.SIGN_MESSAGE ? SignedMessage : T extends RequestType.LOGOUT | RequestType.CHANGE_PASSWORD ? SimpleResult : T extends RequestType.EXPORT ? ExportResult : T extends RequestType.CREATE_CASHLINK | RequestType.MANAGE_CASHLINK ? Cashlink : T extends RequestType.SIGN_BTC_TRANSACTION ? SignedBtcTransaction : T extends RequestType.SIGN_POLYGON_TRANSACTION ? SignedPolygonTransaction : T extends RequestType.ACTIVATE_BITCOIN ? Account : T extends RequestType.ACTIVATE_POLYGON ? Account : T extends RequestType.ADD_BTC_ADDRESSES ? AddBtcAddressesResult : T extends RequestType.SETUP_SWAP ? SetupSwapResult : never;
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"@nimiq/electrum-client": "https://github.com/nimiq/electrum-client#build",
"@nimiq/fastspot-api": "^1.7.0",
"@nimiq/iqons": "^1.5.2",
"@nimiq/keyguard-client": "https://gitpkg.vercel.app/nimiq/keyguard?scripts.postinstall=cd%20client%20%26%26%20.%2Fbuild-gitpkg.sh&78bbdcbcdd2d2051eda083c417f97029d8789b1d",
"@nimiq/keyguard-client": "https://gitpkg.vercel.app/nimiq/keyguard?scripts.postinstall=cd%20client%20%26%26%20.%2Fbuild-gitpkg.sh&a26831706d45e1859b87835b44a0286392ec9fc1",
"@nimiq/ledger-api": "^2.3.0",
"@nimiq/network-client": "^0.6.2",
"@nimiq/oasis-api": "^1.1.1",
Expand Down
4 changes: 2 additions & 2 deletions src/i18n/en.po
Original file line number Diff line number Diff line change
Expand Up @@ -966,8 +966,8 @@ msgstr ""
msgid "Speed up your transaction"
msgstr ""

#: src/views/SignStaking.vue:45
#: src/views/SignStaking.vue:63
#: src/views/SignStaking.vue:47
#: src/views/SignStaking.vue:65
msgid "Staking Contract"
msgstr ""

Expand Down
18 changes: 14 additions & 4 deletions src/lib/RequestParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,18 @@ export class RequestParser {
// Nimiq.BufferUtils.toHex(signStakingRequest.transaction),
// );

if (!(signStakingRequest.transaction instanceof Uint8Array)) {
throw new Error('transaction must be a Uint8Array');
const transactions = Array.isArray(signStakingRequest.transaction)
? signStakingRequest.transaction
: [signStakingRequest.transaction];

if (transactions.length === 0) {
throw new Error('transaction array must not be empty');
}

for (const transaction of transactions) {
if (!(transaction instanceof Uint8Array)) {
throw new Error('transaction must be a Uint8Array');
}
}

return {
Expand All @@ -99,7 +109,7 @@ export class RequestParser {
senderLabel: signStakingRequest.senderLabel,
recipientLabel: signStakingRequest.recipientLabel,
// transaction: transaction.toPlain(),
transaction: signStakingRequest.transaction,
transactions,
} as ParsedSignStakingRequest;
case RequestType.CHECKOUT:
const checkoutRequest = request as CheckoutRequest;
Expand Down Expand Up @@ -746,7 +756,7 @@ export class RequestParser {
senderLabel: signStakingRequest.senderLabel,
recipientLabel: signStakingRequest.recipientLabel,
// transaction: transaction.serialize(),
transaction: signStakingRequest.transaction,
transaction: signStakingRequest.transactions,
} as SignStakingRequest;
case RequestType.CREATE_CASHLINK:
const createCashlinkRequest = request as ParsedCreateCashlinkRequest;
Expand Down
2 changes: 1 addition & 1 deletion src/lib/RequestTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface ParsedSignStakingRequest extends ParsedBasicRequest {
senderLabel?: string;
recipientLabel?: string;
// transaction: AlbatrossPlainTransaction;
transaction: Uint8Array;
transactions: Uint8Array[];
}

export type ParsedProtocolSpecificsForCurrency<C extends Currency> =
Expand Down
8 changes: 5 additions & 3 deletions src/views/SignStaking.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default class SignStaking extends Vue {
@Getter private findWalletByAddress!: (address: string, includeContracts: boolean) => WalletInfo | undefined;
public async created() {
// Determine signer and forward user to Keyguard
// Determine signer by last transaction and forward user to Keyguard
let senderLabel = this.request.senderLabel;
let keyId: string;
Expand All @@ -23,7 +23,9 @@ export default class SignStaking extends Vue {
let recipientLabel = this.request.recipientLabel;
const Albatross = await window.loadAlbatross();
const transaction = Albatross.Transaction.fromAny(Nimiq.BufferUtils.toHex(this.request.transaction)).toPlain();
const transaction = Albatross.Transaction.fromAny(Nimiq.BufferUtils.toHex(
this.request.transactions[this.request.transactions.length - 1],
)).toPlain();
console.log(transaction);
Expand Down Expand Up @@ -75,7 +77,7 @@ export default class SignStaking extends Vue {
senderLabel,
recipientLabel,
transaction: Albatross.Transaction.fromPlain(transaction).serialize(),
transaction: this.request.transactions,
};
const client = this.$rpc.createKeyguardClient(true);
Expand Down
48 changes: 25 additions & 23 deletions src/views/SignStakingSuccess.vue
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,36 @@ import { bytesToHex, hexToBytes } from '../lib/BufferUtils';
@Component({})
export default class SignStakingSuccess extends Vue {
@State private keyguardResult!: KeyguardClient.SignStakingResult;
@State private keyguardResult!: KeyguardClient.SignStakingResult[];
private async mounted() {
const Albatross = await window.loadAlbatross();
const hex = bytesToHex(this.keyguardResult.transaction);
const tx = Albatross.Transaction.fromAny(hex);
const plain = tx.toPlain();
const result: SignedTransaction[] = this.keyguardResult.map((signedTransaction) => {
const hex = bytesToHex(signedTransaction.transaction);
const tx = Albatross.Transaction.fromAny(hex);
const plain = tx.toPlain();
const result: SignedTransaction = {
transaction: this.keyguardResult.transaction,
serializedTx: hex,
hash: plain.transactionHash,
raw: {
...plain,
senderType: tx.senderType,
recipientType: tx.recipientType,
proof: tx.proof,
signerPublicKey: 'publicKey' in plain.proof
? hexToBytes(plain.proof.publicKey)
: new Uint8Array(0),
signature: 'signature' in plain.proof
? hexToBytes(plain.proof.signature)
: new Uint8Array(0),
extraData: tx.data,
networkId: tx.networkId,
},
};
return {
transaction: signedTransaction.transaction,
serializedTx: hex,
hash: plain.transactionHash,
raw: {
...plain,
senderType: tx.senderType,
recipientType: tx.recipientType,
proof: tx.proof,
signerPublicKey: 'publicKey' in plain.proof
? hexToBytes(plain.proof.publicKey)
: new Uint8Array(0),
signature: 'signature' in plain.proof
? hexToBytes(plain.proof.signature)
: new Uint8Array(0),
extraData: tx.data,
networkId: tx.networkId,
},
} as SignedTransaction;
});
this.$rpc.resolve(result);
}
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1508,9 +1508,9 @@
btoa "^1.1.2"
node-lmdb "^0.9.6"

"@nimiq/keyguard-client@https://gitpkg.vercel.app/nimiq/keyguard?scripts.postinstall=cd%20client%20%26%26%20.%2Fbuild-gitpkg.sh&78bbdcbcdd2d2051eda083c417f97029d8789b1d":
"@nimiq/keyguard-client@https://gitpkg.vercel.app/nimiq/keyguard?scripts.postinstall=cd%20client%20%26%26%20.%2Fbuild-gitpkg.sh&a26831706d45e1859b87835b44a0286392ec9fc1":
version "1.0.0"
resolved "https://gitpkg.vercel.app/nimiq/keyguard?scripts.postinstall=cd%20client%20%26%26%20.%2Fbuild-gitpkg.sh&78bbdcbcdd2d2051eda083c417f97029d8789b1d#a8ed1a9de3400e3f32332ddbf7de3d6b2e59dfc7"
resolved "https://gitpkg.vercel.app/nimiq/keyguard?scripts.postinstall=cd%20client%20%26%26%20.%2Fbuild-gitpkg.sh&a26831706d45e1859b87835b44a0286392ec9fc1#b823b6c5547e9128836bc6f1ed2af8ee87a0b9f2"

"@nimiq/ledger-api@^2.3.0":
version "2.3.0"
Expand Down

0 comments on commit a1bf5f2

Please sign in to comment.