Inline the iab_verifier

This commit is contained in:
Jonas Lochmann
2022-03-28 02:00:00 +02:00
parent 2ab23ea811
commit d778df9df0
4 changed files with 52 additions and 39 deletions
+48
View File
@@ -0,0 +1,48 @@
/*
* The MIT License
*
* Copyright (c) Paul Crawford
* Copyright (c) 2020 Jonas Lochmann
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
import { createPublicKey, createVerify, KeyObject } from 'crypto'
const ALGORITHM = 'RSA-SHA1'
export class IABVerifier {
private readonly publicKey: KeyObject
constructor(publicKeyString: string) {
this.publicKey = createPublicKey({
key: Buffer.from(publicKeyString, 'base64'),
format: 'der',
type: 'spki'
})
}
verifyReceipt(signedData: string, signature: string) {
const verifier = createVerify(ALGORITHM)
verifier.update(signedData)
return verifier.verify(this.publicKey, signature, 'base64')
}
}
+4 -7
View File
@@ -15,21 +15,18 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
const IABVerifier: new (publicKey: string) => {
verifyReceipt: (data: string, signature: string) => boolean
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require('iab_verifier')
import { IABVerifier } from './iab_verifierr'
export const googlePlayPublicKey = process.env.GOOGLE_PLAY_PUBLIC_KEY || ''
const verifier = new IABVerifier(googlePlayPublicKey)
const verifier = googlePlayPublicKey !== '' ? new IABVerifier(googlePlayPublicKey) : null
export const areGooglePlayPaymentsPossible = !!googlePlayPublicKey
export const areGooglePlayPaymentsPossible = !!verifier
export const isGooglePlayPurchaseSignatureValid = ({ receipt, signature }: {
receipt: string
signature: string
}) => {
if (googlePlayPublicKey) {
if (verifier) {
return verifier.verifyReceipt(receipt, signature)
} else {
return false