const account = await Account.fromPrivateKey(key)
try {
const headers = {
- method: 'detached',
- msg: this.hash
+ method: 'detached'
}
const data = {
- privateKey: hex.toBytes(account.privateKey).buffer
+ privateKey: hex.toBytes(account.privateKey).buffer,
+ msg: hex.toBytes(this.hash).buffer
}
const result = await NanoNaClWorker.add(headers, data)
- this.signature = result[0].signature
+ this.signature = result[0]
} catch (err) {
throw new Error(`Failed to sign block`, { cause: err })
}
}
try {
const headers = {
- method: 'verify',
- msg: this.hash,
- signature: this.signature ?? '',
- publicKey: key
+ method: 'verify'
}
- const result = await NanoNaClWorker.add(headers)
- return result.isVerified[0]
+ const data = {
+ msg: hex.toBytes(this.hash).buffer,
+ signature: hex.toBytes(this.signature ?? '').buffer,
+ publicKey: hex.toBytes(key).buffer
+ }
+ const result = await NanoNaClWorker.add(headers, data)
+ return result[0]
} catch (err) {
throw new Error(`Failed to derive public key from private key`, { cause: err })
}
message: string
}
-function hash (data: string | string[], encoding?: 'hex'): string {
+function hash (data: string | string[], encoding?: 'hex', format?: 'hex'): string | Uint8Array {
if (!Array.isArray(data)) data = [data]
const hash = new Blake2b(32)
if (encoding === 'hex') {
const enc = new TextEncoder()
data.forEach(str => hash.update(enc.encode(str)))
}
- return hash.digest('hex').toUpperCase()
+ return format === 'hex'
+ ? hash.digest('hex').toUpperCase()
+ : hash.digest()
}
/**
let signature: string
try {
const headers = {
- method: 'detached',
- msg: hash(input)
+ method: 'detached'
}
const data = {
- privateKey: key.buffer
+ privateKey: key.buffer,
+ msg: (hash(input) as Uint8Array<ArrayBuffer>).buffer
}
const result = await NanoNaClWorker.add(headers, data)
- signature = result.publicKey[0]
+ signature = result[0]
} catch (err) {
throw new Error(`Failed to sign message with private key`, { cause: err })
} finally {
let isVerified: boolean
try {
const headers = {
- method: 'verify',
- msg: hash(input),
- signature
+ method: 'verify'
}
const data = {
- privateKey: key.buffer
+ msg: (hash(input) as Uint8Array<ArrayBuffer>).buffer,
+ signature: hex.toBytes(signature).buffer,
+ publicKey: new Uint8Array(key).buffer
}
- isVerified = await NanoNaClWorker.add(headers, data)
+ const result = await NanoNaClWorker.add(headers, data)
+ isVerified = result[0]
} catch (err) {
console.log(err)
isVerified = false
}\r
\r
static async work (headers: Headers, data: Data): Promise<boolean | string> {\r
- const { method, msg, signature, publicKey } = headers\r
+ const { method } = headers\r
+ const msg = new Uint8Array(data.msg)\r
const privateKey = new Uint8Array(data.privateKey)\r
+ const publicKey = new Uint8Array(data.publicKey)\r
+ const signature = new Uint8Array(data.signature)\r
switch (method) {\r
case 'convert': {\r
return bytes.toHex(await this.convert(privateKey))\r
// import './test.calculate-pow.mjs'
// import './test.create-wallet.mjs'
// import './test.derive-accounts.mjs'
-import './test.import-wallet.mjs'
+// import './test.import-wallet.mjs'
import './test.ledger.mjs'
// import './test.lock-unlock.mjs'
// import './test.manage-rolodex.mjs'