Skip to content

Commit

Permalink
Merge pull request #133 from KeystoneHQ/fix-keyring-sol
Browse files Browse the repository at this point in the history
Fix keyring sol
  • Loading branch information
zhaomengru2015 authored Jul 12, 2023
2 parents f628174 + a9c538e commit 27f6cb2
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 105 deletions.
13 changes: 0 additions & 13 deletions packages/sol-keyring/build.sh

This file was deleted.

2 changes: 0 additions & 2 deletions packages/sol-keyring/config.ts.example

This file was deleted.

6 changes: 2 additions & 4 deletions packages/sol-keyring/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@keystonehq/sol-keyring",
"version": "0.19.2",
"version": "0.19.3",
"description": "sol-keyring interact with keystone",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand All @@ -10,9 +10,7 @@
"scripts": {
"clean": "rm -rf ./dist",
"start": "tsdx watch",
"ci:prepare": "./prepare.sh",
"copy:config": "cp ./config.ts ./src/Secret.ts",
"build": "npm run copy:config && ./build.sh"
"build": "tsdx build"
},
"publishConfig": {
"access": "public"
Expand Down
4 changes: 0 additions & 4 deletions packages/sol-keyring/prepare.sh

This file was deleted.

89 changes: 48 additions & 41 deletions packages/sol-keyring/src/BaseKeyring.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { Message, PublicKey, Transaction } from "@solana/web3.js";
import { InteractionProvider } from "./InteractionProvider";
import { CryptoMultiAccounts } from "@keystonehq/bc-ur-registry";
import { SolSignRequest, SignType } from "@keystonehq/bc-ur-registry-sol";
import { Tracker } from './Tracker';

const keyringType = "QR Hardware Wallet Device";

Expand Down Expand Up @@ -34,7 +33,6 @@ export class BaseKeyring {
protected keys: HDKey[];
protected name: string;
protected device: string;
public isTracking: boolean;

constructor() {
//common props
Expand All @@ -43,7 +41,6 @@ export class BaseKeyring {
this.initialized = false;
this.device = "";
this.xfp = "";
this.isTracking = true;
}

protected requestSignature = async (
Expand All @@ -67,14 +64,6 @@ export class BaseKeyring {
);
}
}
if (this.isTracking) {
Tracker.track("sign", {
distinctId: this.device,
time: Date.now(),
xfp: this.xfp,
requestId: _requestId,
});
}
return signature;
};

Expand All @@ -99,20 +88,18 @@ export class BaseKeyring {
index,
}));
this.initialized = true;
if (this.isTracking) {
Tracker.track("sync", {
distinctId: this.device,
time: Date.now(),
xfp: this.xfp,
});
}
}

public syncKeyringData({xfp, keys, name = "QR Hardware", device}: KeyringInitData): void {
this.xfp = xfp
this.name = name
this.keys = keys
this.device = device
public syncKeyringData({
xfp,
keys,
name = "QR Hardware",
device,
}: KeyringInitData): void {
this.xfp = xfp;
this.name = name;
this.keys = keys;
this.device = device;
this.initialized = true;
}

Expand All @@ -128,7 +115,11 @@ export class BaseKeyring {
}

async signTransaction(pubKey: string, tx: Transaction): Promise<Transaction> {
const signature = await this._getSignature(pubKey, Buffer.from(tx.serializeMessage() as unknown as ArrayBuffer), SignType.Transaction)
const signature = await this._getSignature(
pubKey,
Buffer.from(tx.serializeMessage() as unknown as ArrayBuffer),
SignType.Transaction
);
tx.addSignature(new PublicKey(pubKey), signature);
return tx;
}
Expand All @@ -137,38 +128,54 @@ export class BaseKeyring {
pubKey: string,
messageHex: Uint8Array
): Promise<Uint8Array> {
return await this._getSignature(pubKey, Buffer.from(messageHex), SignType.Message)
return await this._getSignature(
pubKey,
Buffer.from(messageHex),
SignType.Message
);
}

async createSignature(pubKey: string, messageHex: Uint8Array): Promise<Uint8Array>{
try{
async createSignature(
pubKey: string,
messageHex: Uint8Array
): Promise<Uint8Array> {
try {
const messageInstance = Message.from(messageHex);
const transaction = Transaction.populate(messageInstance, []);
if (transaction) {
return this._getSignature(pubKey, Buffer.from(messageHex),SignType.Transaction)
return this._getSignature(
pubKey,
Buffer.from(messageHex),
SignType.Transaction
);
}
}catch(e){
} catch (e) {
console.error(e);
}
return this.signMessage(pubKey, messageHex)
return this.signMessage(pubKey, messageHex);
}

async _getSignature(pubKey: string, messageHex: Buffer, signType): Promise<Buffer>{
async _getSignature(
pubKey: string,
messageHex: Buffer,
signType
): Promise<Buffer> {
const requestId = uuid.v4();
const account = this.getAccounts().find(
(account) => account.pubKey == pubKey
(account) => account.pubKey == pubKey
);
const solSignRequest = SolSignRequest.constructSOLRequest(
messageHex,
account.hdPath,
this.xfp,
signType,
requestId,
messageHex,
account.hdPath,
this.xfp,
signType,
requestId
);
return this.requestSignature(
requestId,
solSignRequest,
"Scan with your Keystone",
'After your Keystone has signed this message, click on "Scan Keystone" to receive the signature'
requestId,
solSignRequest,
"Scan with your Keystone",
'After your Keystone has signed this message, click on "Scan Keystone" to receive the signature'
);
}
}
2 changes: 1 addition & 1 deletion packages/sol-keyring/src/DefaultInteractionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export class DefaultInteractionProvider implements InteractionProvider {
hasNext: true,
title: requestTitle,
description: requestDescription,
maxFragmentLength: 400
maxFragmentLength: 400,
});
if (status === PlayStatus.canceled)
throw new Error("#ktek_error[play-cancel]: play canceled");
Expand Down
40 changes: 0 additions & 40 deletions packages/sol-keyring/src/Tracker.ts

This file was deleted.

0 comments on commit 27f6cb2

Please sign in to comment.