Skip to content

Commit

Permalink
Avoid using 1.0 Nimiq lib for transaction handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sisou committed Nov 23, 2023
1 parent ee39208 commit 34d9033
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 52 deletions.
14 changes: 8 additions & 6 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,16 @@
<link rel="icon" href="<%= htmlWebpackPlugin.options.domain %>/favicon.ico">

<script type="module">
import init, { Client, ClientConfiguration } from '/albatross-client/web/index.js';
import init, * as Nimiq from '/albatross-client/web/index.js';

/** @type {Promise<Nimiq> | undefined} */
let initPromise;

window.loadAlbatross = async function() {
await init();
return {
Client,
ClientConfiguration,
};
return initPromise || (initPromise = new Promise(async resolve => {
await init();
resolve(Nimiq);
}));
}
</script>
</head>
Expand Down
15 changes: 15 additions & 0 deletions src/lib/BufferUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export function hexToBytes(hex: string) {
return new Uint8Array((hex.match(/.{2}/g) || []).map(byte => parseInt(byte, 16)));
}

export function bytesToHex(bytes: Uint8Array) {
const HEX_ALPHABET = '0123456789abcdef';

let hex = '';
for (let i = 0; i < bytes.length; i++) {
const code = bytes[i];
hex += HEX_ALPHABET[code >>> 4];
hex += HEX_ALPHABET[code & 0x0F];
}
return hex;
}
15 changes: 4 additions & 11 deletions src/lib/NetworkClient.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
import type {
Client as AlbatrossClient,
ClientConfiguration as AlbatrossClientConfiguration,
PlainVestingContract,
} from '@nimiq/albatross-wasm';
import type * as Nimiq from '@nimiq/albatross-wasm';
import Config from 'config';

declare global {
interface Window {
loadAlbatross: () => Promise<{
Client: typeof AlbatrossClient,
ClientConfiguration: typeof AlbatrossClientConfiguration,
}>;
loadAlbatross: () => Promise<typeof Nimiq>;
}
}

Expand All @@ -22,7 +15,7 @@ export class NetworkClient {
return this._instance;
}

private _clientPromise?: Promise<AlbatrossClient>;
private _clientPromise?: Promise<Nimiq.Client>;

// constructor() {}

Expand Down Expand Up @@ -62,7 +55,7 @@ export class NetworkClient {
}

public async getGenesisVestingContracts() {
return [] as Array<PlainVestingContract & { address: string }>; // TODO
return [] as Array<Nimiq.PlainVestingContract & { address: string }>; // TODO
}

public get innerClient() {
Expand Down
50 changes: 29 additions & 21 deletions src/views/SignStakingSuccess.vue
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
<template>
<Network ref="network" :visible="false"/>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Network from '@/components/Network.vue';
import { SignedTransaction } from '../../client/PublicRequestTypes';
import { State } from 'vuex-class';
import { Static } from '../lib/StaticStore';
import KeyguardClient from '@nimiq/keyguard-client';
import { StakingSignallingTypes, StakingTransactionType } from '../lib/Constants';
import { bytesToHex, hexToBytes } from '../lib/BufferUtils';
@Component({components: {Network}})
@Component({})
export default class SignStakingSuccess extends Vue {
@Static private keyguardRequest!: KeyguardClient.SignStakingRequest;
@State private keyguardResult!: KeyguardClient.SignStakingResult;
private async mounted() {
const tx = await (this.$refs.network as Network).createTx(Object.assign(
{ signerPubKey: this.keyguardResult.publicKey },
this.keyguardRequest,
this.keyguardResult,
StakingSignallingTypes.includes(this.keyguardRequest.type) ? { value: 0, flags: 0b10 } : {},
this.keyguardRequest.type === StakingTransactionType.UNSTAKE
? { proofPrefix: new Uint8Array([StakingTransactionType.UNSTAKE])}
: {},
));
const result: SignedTransaction = await (this.$refs.network as Network).makeSignTransactionResult(tx);
const { Transaction } = await window.loadAlbatross();
const hex = bytesToHex(this.keyguardResult.serializedTx);
const tx = Transaction.fromAny(hex);
const plain = tx.toPlain();
// Overwrite serializedTx with Albatross serialization format
result.serializedTx = Nimiq.BufferUtils.toHex(this.keyguardResult.serializedTx);
const result: SignedTransaction = {
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)
: 'creatorPublicKey' in plain.proof
? hexToBytes(plain.proof.creatorPublicKey)
: new Uint8Array(0),
signature: 'signature' in plain.proof
? hexToBytes(plain.proof.signature)
: 'creatorSignature' in plain.proof
? hexToBytes(plain.proof.creatorSignature)
: new Uint8Array(0),
extraData: tx.data,
networkId: tx.networkId,
},
};
this.$rpc.resolve(result);
}
Expand Down
43 changes: 29 additions & 14 deletions src/views/SignTransactionSuccess.vue
Original file line number Diff line number Diff line change
@@ -1,28 +1,43 @@
<template>
<Network ref="network" :visible="false"/>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import Network from '@/components/Network.vue';
import { SignedTransaction } from '../../client/PublicRequestTypes';
import { State } from 'vuex-class';
import { Static } from '../lib/StaticStore';
import KeyguardClient from '@nimiq/keyguard-client';
import { bytesToHex, hexToBytes } from '../lib/BufferUtils';
@Component({components: {Network}})
@Component({})
export default class SignTransactionSuccess extends Vue {
@Static private keyguardRequest!: KeyguardClient.SignTransactionRequest;
@State private keyguardResult!: KeyguardClient.SignTransactionResult;
private async mounted() {
const tx = await (this.$refs.network as Network).createTx(Object.assign({
signerPubKey: this.keyguardResult.publicKey,
}, this.keyguardResult, this.keyguardRequest));
const result: SignedTransaction = await (this.$refs.network as Network).makeSignTransactionResult(tx);
const { Transaction } = await window.loadAlbatross();
const hex = bytesToHex(this.keyguardResult.serializedTx);
const tx = Transaction.fromAny(hex);
const plain = tx.toPlain();
// Overwrite serializedTx with Albatross serialization format
result.serializedTx = Nimiq.BufferUtils.toHex(this.keyguardResult.serializedTx);
const result: SignedTransaction = {
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)
: 'creatorPublicKey' in plain.proof
? hexToBytes(plain.proof.creatorPublicKey)
: new Uint8Array(0),
signature: 'signature' in plain.proof
? hexToBytes(plain.proof.signature)
: 'creatorSignature' in plain.proof
? hexToBytes(plain.proof.creatorSignature)
: new Uint8Array(0),
extraData: tx.data,
networkId: tx.networkId,
},
};
this.$rpc.resolve(result);
}
Expand Down

0 comments on commit 34d9033

Please sign in to comment.