Skip to content

Commit

Permalink
Merge pull request #11 from CityOfZion/CU-86a0cb0v0
Browse files Browse the repository at this point in the history
CU-86a0cb0v0 - Add support to TestNet
  • Loading branch information
melanke authored Aug 10, 2023
2 parents 83e3c07 + ebd2f03 commit dc28518
Show file tree
Hide file tree
Showing 56 changed files with 5,178 additions and 2,574 deletions.
13 changes: 13 additions & 0 deletions .prettierrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"semi": false,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "es5",
"tabWidth": 2,
"printWidth": 120,
"arrowParens": "avoid",
"bracketSpacing": true,
"bracketSameLine": false,
"endOfLine": "lf"
}
3,436 changes: 2,788 additions & 648 deletions common/config/rush/npm-shrinkwrap.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/blockchain-service/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cityofzion/blockchain-service",
"version": "0.4.2",
"version": "0.5.0",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"repository": "https://github.com/CityOfZion/blockchain-services",
Expand Down
35 changes: 23 additions & 12 deletions packages/blockchain-service/src/BSAgreggator.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { exceptions } from './exceptions'
import { BlockchainAlreadyExist, InvalidBlockchainService } from './exceptions'
import { BlockchainService, Claimable } from "./interfaces";
export class BSAgreggator<BSCustom extends BlockchainService = BlockchainService, BSCustomName extends string = string> {
readonly blockchainservices: Record<BSCustomName, BSCustom>
private bsList: BlockchainService<BSCustomName>[]

constructor(blockchainservices: Record<BSCustomName, BSCustom>) {
this.blockchainservices = blockchainservices
this.bsList = Object.values(blockchainservices)
}

private haveBlockchainServices() {
const blockchainservices = Object.values(this.blockchainservices)
const rules = [
Expand All @@ -16,45 +18,54 @@ export class BSAgreggator<BSCustom extends BlockchainService = BlockchainService

return rules.every(rule => rule === true)
}

addBlockchain(name: BSCustomName, blockchain: BSCustom) {
if (this.blockchainservices[name]) exceptions.blockchainAlreadyExist(name)
if (this.blockchainservices[name]) throw new BlockchainAlreadyExist(name)
this.blockchainservices[name] = blockchain
this.bsList = Object.values(this.blockchainservices)
}

validateAddressesAllBlockchains(address: string) {
if (this.haveBlockchainServices()) exceptions.invalidBlockchainService(JSON.stringify(this.blockchainservices))
if (this.haveBlockchainServices()) throw new InvalidBlockchainService(JSON.stringify(this.blockchainservices))
return this.bsList.some(bs => bs.validateAddress(address))
}

validateTextAllBlockchains(text: string) {
if (this.haveBlockchainServices()) exceptions.invalidBlockchainService(JSON.stringify(this.blockchainservices))
if (this.haveBlockchainServices()) throw new InvalidBlockchainService(JSON.stringify(this.blockchainservices))
return this.bsList.some(bs => [bs.validateAddress(text), bs.validateEncryptedKey(text), bs.validateWif(text)].some(it => it === true))
}

validateWifAllBlockchains(wif: string) {
if (this.haveBlockchainServices()) exceptions.invalidBlockchainService(JSON.stringify(this.blockchainservices))
if (this.haveBlockchainServices()) throw new InvalidBlockchainService(JSON.stringify(this.blockchainservices))
return this.bsList.some(bs => bs.validateWif(wif))
}

validateEncryptedKeyAllBlockchains(encryptedKey: string) {
if (this.haveBlockchainServices()) exceptions.invalidBlockchainService(JSON.stringify(this.blockchainservices))
if (this.haveBlockchainServices()) throw new InvalidBlockchainService(JSON.stringify(this.blockchainservices))
return this.bsList.some(bs => bs.validateEncryptedKey(encryptedKey))
}

getBlockchainByAddress(address: string): BlockchainService<BSCustomName> | null {
if (this.haveBlockchainServices()) exceptions.invalidBlockchainService(JSON.stringify(this.blockchainservices))
if (this.haveBlockchainServices()) throw new InvalidBlockchainService(JSON.stringify(this.blockchainservices))
return this.bsList.find(bs => bs.validateAddress(address)) ?? null
}

getBlockchainByWif(wif: string): BlockchainService<BSCustomName> | null {
if (this.haveBlockchainServices()) exceptions.invalidBlockchainService(JSON.stringify(this.blockchainservices))
if (this.haveBlockchainServices()) throw new InvalidBlockchainService(JSON.stringify(this.blockchainservices))
return this.bsList.find(bs => bs.validateWif(wif)) ?? null
}

getBlockchainByEncryptedKey(encryptedKey: string): BlockchainService<BSCustomName> | null {
if (this.haveBlockchainServices()) exceptions.invalidBlockchainService(JSON.stringify(this.blockchainservices))
if (this.haveBlockchainServices()) throw new InvalidBlockchainService(JSON.stringify(this.blockchainservices))
return this.bsList.find(bs => bs.validateEncryptedKey(encryptedKey)) ?? null
}

getBlockchainsClaimable() {
const methodName = {claim: 'claim', getUnclaimed: 'getUnclaimed', tokenClaim: 'tokenClaim'}
const methodName = { claim: 'claim', getUnclaimed: 'getUnclaimed', tokenClaim: 'tokenClaim' }
const claimableBlockchains = this.bsList.filter(
blockchain => methodName.claim in blockchain &&
methodName.getUnclaimed in blockchain.dataService &&
methodName.tokenClaim in blockchain
methodName.getUnclaimed in blockchain.dataService &&
methodName.tokenClaim in blockchain
) as Array<BlockchainService<BSCustomName> & Claimable>
return claimableBlockchains
}
Expand Down
16 changes: 9 additions & 7 deletions packages/blockchain-service/src/exceptions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
export class BlockchainAlreadyExist extends Error {
constructor(blockchainName: string) {
super(`The blockchain ${blockchainName} already exist`);
}
}

export const exceptions = {
invalidBlockchainService: (message?: string) => {
throw new Error(`Invalid blockchainServices => ${message}`);
},
blockchainAlreadyExist: (blockchainName: string) => {
throw new Error(`The blockchain ${blockchainName} already exist`);
export class InvalidBlockchainService extends Error {
constructor(message?: string) {
super(`Invalid blockchainServices => ${message}`);
}
}
}
19 changes: 0 additions & 19 deletions packages/blockchain-service/src/exchanges/flamingo/Flamingo.ts

This file was deleted.

This file was deleted.

This file was deleted.

9 changes: 0 additions & 9 deletions packages/blockchain-service/src/exchanges/index.ts

This file was deleted.

12 changes: 8 additions & 4 deletions packages/blockchain-service/src/functions.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { BlockchainService, Claimable, NeoNameService } from "./interfaces"
import { BlockchainService, CalculableFee, Claimable, NeoNameService } from './interfaces'

export function hasNNS(service: BlockchainService): service is NeoNameService & BlockchainService {
return "getNNSRecord" in service && "getOwnerOfNNS" in service && "validateNNSFormat" in service
return 'getNNSRecord' in service && 'getOwnerOfNNS' in service && 'validateNNSFormat' in service
}

export function isClaimable(service: BlockchainService): service is Claimable & BlockchainService {
return "claim" in service && "tokenClaim" in service && "getUnclaimed" in service.dataService
}
return 'claim' in service && 'tokenClaim' in service && 'getUnclaimed' in service.dataService
}

export function isCalculableFee(service: BlockchainService): service is CalculableFee & BlockchainService {
return 'calculateTransferFee' in service
}
10 changes: 6 additions & 4 deletions packages/blockchain-service/src/helpers/keychain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// @ts-ignore
const SDK = typeof window !== 'undefined' ? require('../polyfills/asteroid-sdk') : require('@moonlight-io/asteroid-sdk-js')
import type AsteroidSDK from '@moonlight-io/asteroid-sdk-js'

export {SDK}
export const keychain = new SDK.Keychain()
const SDK: typeof AsteroidSDK =
// @ts-ignore
typeof window !== 'undefined' ? require('../polyfills/asteroid-sdk') : require('@moonlight-io/asteroid-sdk-js')

export { SDK }
export const keychain = new SDK.Keychain()
5 changes: 2 additions & 3 deletions packages/blockchain-service/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
export * from './interfaces'
export * from './BSAgreggator'
export * from './exchanges'
export * from "./functions"
export * from "./helpers"
export * from './functions'
export * from './helpers'
Loading

0 comments on commit dc28518

Please sign in to comment.