Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix uvarint64 #6

Merged
merged 2 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 0 additions & 13 deletions src/app/crypto.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,6 @@ export class CryptoService {
return this.seedHexToPrivateKey(seedHex);
}

uintToBuf(uint: number): Buffer {
const result = [];

while (uint >= 0x80) {
result.push((uint & 0xFF) | 0x80);
uint >>>= 7;
}

result.push(uint | 0);

return new Buffer(result);
}

mnemonicToKeychain(mnemonic: string, extraText?: string): HDNode {
const seed = bip39.mnemonicToSeedSync(mnemonic, extraText);
return HDKey.fromMasterSeed(seed).derive('m/44\'/0\'/0\'/0/0');
Expand Down
3 changes: 2 additions & 1 deletion src/app/signing.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as jsonwebtoken from 'jsonwebtoken';
import * as ecies from '../lib/ecies';
import {CryptoService} from './crypto.service';
import * as sha256 from 'sha256';
import { uvarint64ToBuf } from '../lib/bindata/util';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -44,7 +45,7 @@ export class SigningService {
const transactionHash = new Buffer(sha256.x2(transactionBytes), 'hex');
const signature = privateKey.sign(transactionHash);
const signatureBytes = new Buffer(signature.toDER());
const signatureLength = this.cryptoService.uintToBuf(signatureBytes.length);
const signatureLength = uvarint64ToBuf(signatureBytes.length);

const signedTransactionBytes = Buffer.concat([
// This slice is bad. We need to remove the existing signature length field prior to appending the new one.
Expand Down
4 changes: 2 additions & 2 deletions src/lib/bindata/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ export const bufToUvarint64 = (buffer: Buffer): [number, Buffer] => {
for (let i = 0; true; i++) {
const byte = buffer[i];

if (i > 9 || i == 9 && byte > 1) throw new Error('uint64 overflow');
if (i > 9 || i == 9 && byte > 1) { throw new Error('uint64 overflow'); }

if (byte < 0x80) {
return [x | byte << s, buffer.slice(i+1)];
return [Number(BigInt(x) | BigInt(byte) << BigInt(s)), buffer.slice(i + 1)];
}

x |= (byte & 0x7F) << s;
Expand Down