Skip to content

Commit

Permalink
Add a function to generate a random setup code
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelthomas2774 committed Oct 27, 2019
1 parent a6a4810 commit 6830baa
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Core.ts
Expand Up @@ -2,7 +2,8 @@ import path from 'path';

import storage from 'node-persist';

import { AccessoryLoader } from './';
import { AccessoryLoader, generateSetupCode } from './';
import { NodeCallback } from './types';

console.log("HAP-NodeJS starting...");

Expand Down Expand Up @@ -35,8 +36,12 @@ accessories.forEach((accessory) => {
port: targetPort++,
// @ts-ignore
username: accessory.username,
// @ts-ignore
pincode: accessory.pincode,
pincode: (callback: NodeCallback<string>) => generateSetupCode((err, pincode) => {
if (err) return callback(err);

console.log('[%s] Received pair request', accessory.displayName, pincode, accessory.setupURI(pincode));
callback(null, pincode);
}),
// @ts-ignore
category: accessory.category
});
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Expand Up @@ -22,6 +22,7 @@ export * from './lib/util/clone';
export * from './lib/util/encryption';
export * from './lib/util/hkdf';
export * from './lib/util/once';
export * from './lib/util/setupcode';
export * from './lib/util/tlv';

export * from './types';
Expand Down
2 changes: 2 additions & 0 deletions src/lib/gen/importAsClasses.ts
Expand Up @@ -179,6 +179,7 @@ function getCharacteristicFormatsKey(format: string) {

// look up the key in our known-formats dict
for (var key in Formats)
// @ts-ignore
if (Formats[key] == format)
return key;

Expand All @@ -188,6 +189,7 @@ function getCharacteristicFormatsKey(format: string) {
function getCharacteristicUnitsKey(units: string) {
// look up the key in our known-units dict
for (var key in Units)
// @ts-ignore
if (Units[key] == units)
return key;

Expand Down
49 changes: 49 additions & 0 deletions src/lib/util/setupcode.ts
@@ -0,0 +1,49 @@
import crypto from 'crypto';

import { NodeCallback } from '../../types';

const invalid = [
'000-00-000',
'111-11-111',
'222-22-222',
'333-33-333',
'444-44-444',
'555-55-555',
'666-66-666',
'777-77-777',
'888-88-888',
'999-99-999',
'123-45-678',
'876-54-321',
];

function toString(buffer: Buffer) {
return ('000' + buffer.readUInt16LE(0)).substr(-3) + '-' +
('00' + buffer.readUInt16LE(2)).substr(-2) + '-' +
('000' + buffer.readUInt16LE(4)).substr(-3);
}

export function generateSetupCode(callback: NodeCallback<string>): void
export function generateSetupCode(): Promise<string>
export function generateSetupCode(callback?: NodeCallback<string>): Promise<string> | void {
if (!callback) return new Promise((rs, rj) => generateSetupCode((err, result) => err ? rj(err) : rs(result)));

crypto.randomBytes(6, (err, buffer) => {
if (err) return callback(err);

const setupcode = toString(buffer);
if (!invalid.includes(setupcode)) return callback(null, setupcode);

generateSetupCode(callback);
});
}

export function generateSetupCodeSync() {
let setupcode;

do {
setupcode = toString(crypto.randomBytes(8));
} while (invalid.includes(setupcode));

return setupcode;
}

0 comments on commit 6830baa

Please sign in to comment.