Skip to content

Commit

Permalink
added raw sign back in
Browse files Browse the repository at this point in the history
  • Loading branch information
rh0delta committed Aug 21, 2024
1 parent 3de7ffd commit 90d4968
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,12 @@ program.command('sign')
.addOption(passwordOption('Password for the source account (if required)'))
.addOption(endpointOption())
.addOption(currentAccountAddressOption())
.addOption(
new Option(
'-r, --raw',
'Signs the provided message using the Raw Signing method. Output is a signature (string)'
)
)
.action(async (address, message, msgPath, opts) => {
if (address) {
await reloadEntropy(
Expand All @@ -189,6 +195,12 @@ program.command('sign')
)
}
const signingCommand = new SigningCommand(entropy, opts.endpoint)
// TO-DO: Add ability for raw signing here, maybe? new raw option can be used for the conditional
/**
* if (opts.raw) {
* implement raw sign here
* }
*/
const signature = await signingCommand.signMessage({ msg: message, msgPath })
writeOut(signature)
process.exit(0)
Expand Down
38 changes: 29 additions & 9 deletions src/signing/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,46 @@ import { u8aToHex } from '@polkadot/util'
import { readFileSync } from "fs";
import { BaseCommand } from "../common/base-command";
import { print } from "../common/utils";
import { interactionChoiceQuestions, messageActionQuestions, signWithAdapters, userInputQuestions } from "./utils";
import { SignWithAdapterResult } from "./types";
import { getMsgFromInputOrFile, interactionChoiceQuestions, messageActionQuestions, rawSign, signWithAdapters, userInputQuestions } from "./utils";
import { SignResult } from "./types";
import { FLOW_CONTEXT } from "./constants";

export class SigningCommand extends BaseCommand {
constructor (entropy: Entropy, endpoint: string) {
super(entropy, endpoint, FLOW_CONTEXT)
}

public async signMessage ({ msg, msgPath }: { msg?: string, msgPath?: string }): Promise<SignWithAdapterResult> {
if (!msg && msgPath) {
msg = readFileSync(msgPath, 'utf-8')
}
if (!msg && !msgPath) {
throw new Error('SigningError: You must provide a message or path to a file')
public async rawSignMessage ({ msg, msgPath, auxiliaryData, hashingAlgorithm }): Promise<SignResult> {
const message = getMsgFromInputOrFile(msg, msgPath)

try {
this.logger.log(`Msg to be signed: ${msg}`, 'SIGN_MSG')
this.logger.log( `Verifying Key used: ${this.entropy.signingManager.verifyingKey}`)
const signature = await rawSign(
this.entropy,
{
sigRequestHash: Buffer.from(message).toString('hex'),
hash: hashingAlgorithm,
auxiliaryData
}
)
const signatureHexString = u8aToHex(signature)
this.logger.log(`Signature: ${signatureHexString}`)

return { signature: signatureHexString, verifyingKey: this.entropy.signingManager.verifyingKey }
} catch (error) {
this.logger.error('Error signing message', error)
throw error
}
}

public async signMessage ({ msg, msgPath }: { msg?: string, msgPath?: string }): Promise<SignResult> {
const message = getMsgFromInputOrFile(msg, msgPath)

try {
this.logger.log(`Msg to be signed: ${msg}`, 'SIGN_MSG')
this.logger.log( `Verifying Key used: ${this.entropy.signingManager.verifyingKey}`)
const signature = await signWithAdapters(this.entropy, { msg })
const signature = await signWithAdapters(this.entropy, { msg: message })
const signatureHexString = u8aToHex(signature)
this.logger.log(`Signature: ${signatureHexString}`)

Expand Down
9 changes: 8 additions & 1 deletion src/signing/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@ export interface SignWithAdapterInput {
// auxillaryData: any
}

export interface SignWithAdapterResult {
export interface SignResult {
signature: string
verifyingKey: string
}

export interface RawSignPayload {
sigRequestHash: string
hash: any
auxiliaryData: any
signatureVerifyingKey?: string
}
19 changes: 18 additions & 1 deletion src/signing/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { readFileSync } from "fs"
import { SIGNING_CONTENT } from "./constants"
import { SignWithAdapterInput } from "./types"
import { RawSignPayload, SignWithAdapterInput } from "./types"

function stringToHex (str: string): string {
return Buffer.from(str).toString('hex')
Expand All @@ -25,6 +26,22 @@ export const userInputQuestions = [{
message: SIGNING_CONTENT.textInput.message,
}]

export function getMsgFromInputOrFile (msg?: string, msgPath?: string) {
let result: string = msg
if (!msg && msgPath) {
result = readFileSync(msgPath, 'utf-8')
}
if (!msg && !msgPath) {
throw new Error('SigningError: You must provide a message or path to a file')
}

return result
}

export async function rawSign (entropy, payload: RawSignPayload) {
return entropy.sign(payload)
}

export async function signWithAdapters (entropy, input: SignWithAdapterInput) {
return entropy.signWithAdaptersInOrder({
msg: {
Expand Down

0 comments on commit 90d4968

Please sign in to comment.