Skip to content

Commit

Permalink
wa
Browse files Browse the repository at this point in the history
  • Loading branch information
tbfleming committed May 3, 2019
1 parent 05bafa1 commit 7dea6c5
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
2 changes: 1 addition & 1 deletion external/eosjs
Submodule eosjs updated 1 files
+2 −0 src/eosjs-numeric.ts
2 changes: 1 addition & 1 deletion src/client/ClientRoot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ async function transfer(appState: AppState, from: string, to: string) {
}],
}, {
blocksBehind: 3,
expireSeconds: 10,
expireSeconds: 60 * 60,
});
appendMessage(appState, 'transaction pushed');
} catch (e) {
Expand Down
62 changes: 41 additions & 21 deletions src/client/wasig.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ApiInterfaces, Numeric, Serialize } from 'eosjs';
import { ec } from 'elliptic';
// const BN = require('bn.js');

export class WaSignatureProvider implements ApiInterfaces.SignatureProvider {
public keys = new Map<string, string>();
Expand All @@ -18,7 +19,8 @@ export class WaSignatureProvider implements ApiInterfaces.SignatureProvider {
signBuf.pushArray(new Uint8Array(await crypto.subtle.digest('SHA-256', serializedContextFreeData.buffer)));
else
signBuf.pushArray(new Uint8Array(32));
const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', signBuf.asUint8Array().buffer));
const digest = new Uint8Array(await crypto.subtle.digest('SHA-256', signBuf.asUint8Array().slice().buffer));
// console.log('digest ', Serialize.arrayToHex(digest));

const signatures = [] as string[];
for (const key of requiredKeys) {
Expand All @@ -30,39 +32,57 @@ export class WaSignatureProvider implements ApiInterfaces.SignatureProvider {
id,
type: 'public-key',
}],
challenge: hash.buffer,
challenge: digest.buffer,
},
});
console.log(assertion);
console.log('sig', assertion.response.signature);
console.log('sig', Serialize.arrayToHex(new Uint8Array(assertion.response.signature)));
console.log('auth', assertion.response.authenticatorData);
console.log('json', assertion.response.clientDataJSON);
console.log('json', (new TextDecoder()).decode(new Uint8Array(assertion.response.clientDataJSON)));
console.log('pub', key);
console.log('pub', Numeric.publicKeyToString(Numeric.stringToPublicKey(key)));
// console.log(assertion);
// console.log('sig', assertion.response.signature);
// console.log('sig', Serialize.arrayToHex(new Uint8Array(assertion.response.signature)));
// console.log('auth', assertion.response.authenticatorData);
// console.log('json', assertion.response.clientDataJSON);
// console.log('json', (new TextDecoder()).decode(new Uint8Array(assertion.response.clientDataJSON)));
// console.log('pub', key);
// console.log('pub', Numeric.publicKeyToString(Numeric.stringToPublicKey(key)));
const e = new ec('p256') as any; // .d.ts files for elliptic are wrong
// console.log(ec);
// console.log(ec.Signature);
// console.log((ec.Signature as any).fromDer);
const k = Numeric.stringToPublicKey(key).data.subarray(0, 33);
console.log('pub', Serialize.arrayToHex(k));
// console.log(xxx);
const k2 = e.keyFromPublic(k);
console.log('pub', k2.getPublic(true, 'hex'));
console.log('pub', k2.getPublic('hex'));
// console.log('pub', Serialize.arrayToHex(k));
const k2 = e.keyFromPublic(k).getPublic();
// console.log('pub', k2.getPublic(true, 'hex'));
// console.log('pub', k2.getPublic('hex'));
// const x = new ec.Signature(new Uint8Array(assertion.response.signature) as any);

// let s = new BN(new Uint8Array(assertion.response.signature, 38, 32));
// console.log(s, s.cmp(e.nh));
// if (s.cmp(e.nh) > 0) {
// console.log('.......');
// s = e.n.sub(s);
// }

const whatItReallySigned = new Serialize.SerialBuffer();
whatItReallySigned.pushArray(new Uint8Array(assertion.response.authenticatorData));
whatItReallySigned.pushArray(new Uint8Array(await crypto.subtle.digest('SHA-256', assertion.response.clientDataJSON)));
const s = new Uint8Array(await crypto.subtle.digest('SHA-256', whatItReallySigned.asUint8Array()));
console.log('s', s);

debugger
console.log('?', e.verify(s, new Uint8Array(assertion.response.signature), k2));
const recid = e.getKeyRecoveryParam(s, new Uint8Array(assertion.response.signature), k2);
const hash = new Uint8Array(await crypto.subtle.digest('SHA-256', whatItReallySigned.asUint8Array().slice()));
console.log('?', e.verify(hash, new Uint8Array(assertion.response.signature), k2));
const recid = e.getKeyRecoveryParam(hash, new Uint8Array(assertion.response.signature), k2);
console.log('recid', recid);


const sigData = new Serialize.SerialBuffer();
sigData.push(recid + 27 + 4);
sigData.pushArray(new Uint8Array(assertion.response.signature, 4, 32));
sigData.pushArray(new Uint8Array(assertion.response.signature, 38, 32));
sigData.pushBytes(new Uint8Array(assertion.response.authenticatorData));
sigData.pushBytes(new Uint8Array(assertion.response.clientDataJSON));

const sig = Numeric.signatureToString({
type: Numeric.KeyType.wa,
data: sigData.asUint8Array().slice(),
});
// console.log(sig);
signatures.push(sig);
}
return { signatures, serializedTransaction, serializedContextFreeData };
}
Expand Down

0 comments on commit 7dea6c5

Please sign in to comment.