Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

feat: License service #492

Merged
merged 6 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@
"tiki"
],
"scripts": {
"verify": "npm run verify:ios && npm run verify:android && npm run verify:web",
MiroBenicio marked this conversation as resolved.
Show resolved Hide resolved
"verify:ios": "cd ios && pod deintegrate && pod install && xcodebuild -workspace Plugin.xcworkspace -scheme Plugin -destination generic/platform=iOS && cd ..",
"verify:android": "cd android && ./gradlew clean build test && cd ..",
"verify:web": "npm run build",
"lint": "npm run eslint && npm run prettier -- --check && npm run swiftlint -- lint",
"fmt": "npm run eslint -- --fix && npm run prettier -- --write && npm run swiftlint -- --fix --format",
"eslint": "eslint \"src/**/*.{ts,js}\"",
Expand Down
85 changes: 85 additions & 0 deletions src/License/LicenseService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) TIKI Inc.
* MIT license. See LICENSE file in root directory.
*/

import * as TikiSdkLicensing from '@mytiki/tiki-sdk-capacitor';
import { TitleRecord, CommonTags, Tag, Usecase, CommonUsecases } from '@mytiki/tiki-sdk-capacitor';

export class LicenseService {
private _userId?: string;
private _providerId?: string;
private _terms?: string;
private _expiry?: Date;

constructor() {}

set userId(value: string | undefined) {
this._userId = value;
}

set providerId(value: string | undefined) {
this._providerId = value;
}

set terms(value: string | undefined) {
this._terms = value;
}

set expiry(value: Date | undefined) {
this._expiry = value;
}

/**
* Initializes the SDK service for the specified user.
* @param userId - The user's unique identifier to initialize the SDK for.
* @param providerId - The company's unique identifier to initialize the SDK for.
* @returns A Promise that resolves when the initialization is complete.
*/
async initialize(userId?: string, providerId?: string): Promise<void> {
if ((!userId && !this._userId) || (!providerId && this._providerId))
throw new Error('UserId/ProviderId is necessary to initialize');

await TikiSdkLicensing.instance.initialize(userId! ?? this._userId, providerId! ?? this.providerId!);
}

/**
* Creates a new license for the SDK service.
* @param userId - The user's unique identifier to initialize the SDK for.
* @param terms - The use terms for that license
* @param expiry - The expiry date for that license
* @returns A Promise that resolves when the license creation is complete.
*/
async createLicense(userId?: string, terms?: string, expiry?: Date): Promise<void> {
const isInitialized = await TikiSdkLicensing.instance.isInitialized();

if (!isInitialized) this.initialize();

const titleRecord: TitleRecord = await TikiSdkLicensing.instance
.createTitle(userId! ?? this._userId, [Tag.common(CommonTags.PURCHASE_HISTORY)])
.catch((error) => {
console.log(error);
throw new Error(error);
});

if(!titleRecord.id) throw new Error('Error to create a title!');


await TikiSdkLicensing.instance.createLicense(
titleRecord.id,
[
{
usecases: [Usecase.common(CommonUsecases.ATTRIBUTION)],
destinations: ['*'],
},
],
terms! ?? this._terms,
expiry! ?? this._expiry,
'Receipt data',
);
}

async verifyLicense(ptr: string, usecases: Usecase[], destinations?: string[]) {
return await TikiSdkLicensing.instance.guard(ptr, usecases, destinations);
}
}
MiroBenicio marked this conversation as resolved.
Show resolved Hide resolved