@unspent/psi
Advanced tools
Comparing version 0.1.0 to 0.1.2
export * from "./interface.js"; | ||
export { Psi } from "./Psi.js"; | ||
export { PsiNetworkProvider } from "./PsiNetworkProvider.js"; | ||
export { BytecodePatternQueryDefaults, getChaingraphUnspentRecords, getHistory, getLockingBytecode, getRecords, getTransaction, getUnspentOutputs, prepareBytecodeQueryParameters } from "./query/index.js"; | ||
//# sourceMappingURL=index.js.map |
@@ -5,3 +5,3 @@ import { decodeTransaction, hexToBin } from '@bitauth/libauth'; | ||
// This class deals strictly with putting data into and getting it out of the database. | ||
import { parseOpReturn, prepareBytecodeQueryParameters } from "@unspent/phi"; | ||
import { parseOpReturn, prepareBytecodeQueryParameters } from "./query/index.js"; | ||
import { binToHex } from "@bitauth/libauth"; | ||
@@ -17,2 +17,3 @@ export class Psi extends Dexie { | ||
this.version(1).stores({ | ||
provider: 'id,endpoint', | ||
block: 'id,timestamp', | ||
@@ -26,3 +27,3 @@ contract: 'id,height,data.address,data.code,data.options', | ||
id: height, | ||
timestamp: new Date() | ||
timestamp: (new Date()).getTime() / 1000 | ||
}).catch(function (error) { | ||
@@ -47,3 +48,3 @@ // Log or display the error | ||
// -1 is no blockheight | ||
return lastBlock ? lastBlock : { id: -1, timestamp: new Date() }; | ||
return lastBlock ? lastBlock : { id: -1, timestamp: (new Date()).getTime() / 1000 }; | ||
} | ||
@@ -50,0 +51,0 @@ rawToTransactionI(raw, sha256) { |
@@ -1,5 +0,5 @@ | ||
import { deriveLockingBytecode, getChaingraphUnspentRecords, getHistory, prepareBytecodeQueryParameters } from "@unspent/phi"; | ||
import { getChaingraphUnspentRecords, getHistory, prepareBytecodeQueryParameters } from "./query/index.js"; | ||
import { | ||
// getMaxBlockHeight, | ||
asUtxo } from "./util.js"; | ||
asUtxo, deriveLockingBytecode, } from "./util.js"; | ||
import { Psi } from "./Psi.js"; | ||
@@ -10,3 +10,3 @@ import { binToHex } from "@bitauth/libauth"; | ||
chaingraphHost; | ||
failoverProviders; | ||
failoverProvider; | ||
debounce; | ||
@@ -17,10 +17,11 @@ fuzz; | ||
FUZZ = 300 * 1000; | ||
constructor(network, chaingraphHost, failoverProviders, debounce, fuzz) { | ||
constructor(network, chaingraphHost, failoverProvider, debounce, fuzz) { | ||
this.network = network; | ||
this.chaingraphHost = chaingraphHost; | ||
this.failoverProviders = failoverProviders; | ||
this.failoverProvider = failoverProvider; | ||
this.debounce = debounce; | ||
this.fuzz = fuzz; | ||
this.chaingraphHost = chaingraphHost ? chaingraphHost : "https://gql.chaingraph.pat.mn/v1/graphql"; | ||
this.db = new Psi(network); | ||
failoverProviders = failoverProviders ? failoverProviders : []; | ||
failoverProvider = failoverProvider ? failoverProvider : undefined; | ||
if (debounce) | ||
@@ -34,3 +35,3 @@ this.DEBOUNCE = debounce; | ||
if (block.id > 0 && block.timestamp) { | ||
const age = new Date().getTime() - block.timestamp.getTime(); | ||
const age = new Date().getTime() - block.timestamp; | ||
if (age < this.DEBOUNCE) { | ||
@@ -40,4 +41,4 @@ return block.id; | ||
} | ||
if (this.failoverProviders) { | ||
const currentHeight = await this.failoverProviders[0]?.getBlockHeight(); | ||
if (this.failoverProvider) { | ||
const currentHeight = await this.failoverProvider?.getBlockHeight(); | ||
await this.db.setBlockHeight(currentHeight); | ||
@@ -61,3 +62,3 @@ return currentHeight; | ||
else { | ||
const history = await getHistory(this.chaingraphHost, lockingBytecode, { after: 0, limit: 5 }); | ||
const history = await getHistory(this.chaingraphHost, lockingBytecode, { node: this.network, limit: 5 }); | ||
return (await this.db.bulkPutRawTransaction(history, lockingBytecodeHex)).map(u => asUtxo(u)); | ||
@@ -67,3 +68,3 @@ } | ||
async getRawTransaction(txid) { | ||
if (!this.failoverProviders) { | ||
if (!this.failoverProvider) { | ||
throw Error("No failover network providers specified. Cannot get tx from cache."); | ||
@@ -73,10 +74,8 @@ } | ||
// TODO replace with chaingraph raw transaction getter. | ||
for (const p of this.failoverProviders) { | ||
try { | ||
return await p.getRawTransaction(txid); | ||
} | ||
catch (e) { | ||
console.debug(e); | ||
} | ||
try { | ||
return await this.failoverProvider.getRawTransaction(txid); | ||
} | ||
catch (e) { | ||
console.debug(e); | ||
} | ||
throw Error("Failover Transaction (get) Network providers exhausted, bailing"); | ||
@@ -86,3 +85,3 @@ } | ||
async sendRawTransaction(txHex) { | ||
if (!this.failoverProviders) { | ||
if (!this.failoverProvider) { | ||
throw Error("No failover network providers specified. Cannot send from cache."); | ||
@@ -92,10 +91,8 @@ } | ||
// replace with chaingraph send | ||
for (const p of this.failoverProviders) { | ||
try { | ||
return await p.sendRawTransaction(txHex); | ||
} | ||
catch (e) { | ||
console.debug(e); | ||
} | ||
try { | ||
return await this.failoverProvider.sendRawTransaction(txHex); | ||
} | ||
catch (e) { | ||
console.debug(e); | ||
} | ||
throw Error("Failover Broadcast Network Providers exhausted, bailing"); | ||
@@ -102,0 +99,0 @@ } |
@@ -0,1 +1,2 @@ | ||
import { cashAddressToLockingBytecode } from "@bitauth/libauth"; | ||
const FLAG_TYPED_ARRAY = "FLAG_TYPED_ARRAY"; | ||
@@ -57,2 +58,9 @@ // @ts-ignore | ||
} | ||
// TODO move to @unspent/util | ||
export function deriveLockingBytecode(address) { | ||
const lock = cashAddressToLockingBytecode(address); | ||
if (typeof lock === "string") | ||
throw lock; | ||
return lock.bytecode; | ||
} | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "@unspent/psi", | ||
"version": "0.1.0", | ||
"version": "0.1.2", | ||
"description": "A persistent storage IndexedDB wrapper for ₿∙ϕ contracts and data using Dexie.js", | ||
@@ -10,3 +10,3 @@ "author": "2qx", | ||
"compile": "tsc", | ||
"publish:public": "yarn publish", | ||
"publish:public": "yarn publish --access public", | ||
"test": "npx jest --runInBand --logHeapUsage --forceExit" | ||
@@ -32,3 +32,3 @@ }, | ||
"@bitauth/libauth": "v2.0.0-alpha.8", | ||
"@unspent/phi": "0.1.0", | ||
"axios": "1.4.0", | ||
"cashscript": "0.8.0-next.2", | ||
@@ -35,0 +35,0 @@ "dexie": "4.0.1-alpha.8", |
export * from "./interface.js" | ||
export { Psi } from "./Psi.js"; | ||
export { PsiNetworkProvider } from "./PsiNetworkProvider.js"; | ||
export { PsiNetworkProvider } from "./PsiNetworkProvider.js"; | ||
export { | ||
BytecodePatternQueryI, | ||
BytecodePatternExtendedQueryI, | ||
BytecodePatternQueryDefaults, | ||
ChaingraphSearchOutputPrefixResponse, | ||
ChaingraphSearchOutputResult, | ||
getChaingraphUnspentRecords, | ||
getHistory, | ||
getLockingBytecode, | ||
getRecords, | ||
getTransaction, | ||
getUnspentOutputs, | ||
HistoryI, | ||
HistoryQueryI, | ||
prepareBytecodeQueryParameters | ||
} from "./query/index.js"; |
@@ -61,4 +61,4 @@ export interface ContractI { | ||
id: number; // height | ||
timestamp: Date; // new Date() | ||
timestamp: number; // epoch | ||
} | ||
@@ -12,4 +12,4 @@ import { Psi } from "./Psi.js"; | ||
//import { getChaingraphUnspentRecords } from "@unspent/phi"; | ||
import { getHistory, HistoryQueryI } from "@unspent/phi"; | ||
//import { getChaingraphUnspentRecords } from "./query/index.js"; | ||
import { getHistory, HistoryQueryI } from "./query/index.js"; | ||
@@ -16,0 +16,0 @@ beforeAll(async () => { |
@@ -23,3 +23,3 @@ import { | ||
prepareBytecodeQueryParameters | ||
} from "@unspent/phi"; | ||
} from "./query/index.js"; | ||
import { binToHex } from "@bitauth/libauth"; | ||
@@ -42,2 +42,3 @@ | ||
this.version(1).stores({ | ||
provider: 'id,endpoint', | ||
block: 'id,timestamp', | ||
@@ -49,8 +50,6 @@ contract: 'id,height,data.address,data.code,data.options', | ||
public async setBlockHeight(height: number): Promise<void> { | ||
await this.block.put({ | ||
id: height, | ||
timestamp: new Date() | ||
timestamp: (new Date()).getTime()/1000 | ||
}).catch(function (error) { | ||
@@ -76,3 +75,3 @@ // Log or display the error | ||
// -1 is no blockheight | ||
return lastBlock ? lastBlock : { id: -1, timestamp: new Date() } | ||
return lastBlock ? lastBlock : { id: -1, timestamp: (new Date()).getTime()/1000 } | ||
} | ||
@@ -79,0 +78,0 @@ |
@@ -53,3 +53,3 @@ import { ElectrumCluster, ClusterOrder, ElectrumTransport } from "electrum-cash"; | ||
const psiProvider = new PsiNetworkProvider("mainnet", host, [fulcrumProvider], 50) | ||
const psiProvider = new PsiNetworkProvider("mainnet", host, fulcrumProvider, 50) | ||
@@ -56,0 +56,0 @@ const utxos = await psiProvider.getUtxos("bitcoincash:pz6qg80k3tps0zexq0kkxreen2ndscvqwve8l0r6vn") |
import { ElectrumNetworkProvider, Network, NetworkProvider } from "cashscript"; | ||
import { | ||
BytecodePatternQueryI, | ||
deriveLockingBytecode, | ||
getChaingraphUnspentRecords, | ||
getHistory, | ||
prepareBytecodeQueryParameters | ||
} from "@unspent/phi" | ||
} from "./query/index.js" | ||
import { | ||
@@ -14,3 +14,4 @@ Utxo | ||
// getMaxBlockHeight, | ||
asUtxo | ||
asUtxo, | ||
deriveLockingBytecode, | ||
} from "./util.js" | ||
@@ -29,4 +30,4 @@ import { Psi } from "./Psi.js" | ||
public network: Network, | ||
public chaingraphHost: string, | ||
public failoverProviders?: ElectrumNetworkProvider[], | ||
public chaingraphHost?: string, | ||
public failoverProvider?: ElectrumNetworkProvider, | ||
public debounce?: number, | ||
@@ -36,5 +37,6 @@ public fuzz?: number | ||
this.chaingraphHost = chaingraphHost ? chaingraphHost : "https://gql.chaingraph.pat.mn/v1/graphql" | ||
this.db = new Psi(network) | ||
failoverProviders = failoverProviders ? failoverProviders : [] | ||
failoverProvider = failoverProvider ? failoverProvider : undefined | ||
if (debounce) this.DEBOUNCE = debounce | ||
@@ -50,3 +52,3 @@ if (fuzz) this.FUZZ = fuzz | ||
if (block.id > 0 && block.timestamp) { | ||
const age = new Date().getTime() - block.timestamp.getTime() | ||
const age = new Date().getTime() - block.timestamp | ||
if (age < this.DEBOUNCE) { | ||
@@ -57,4 +59,4 @@ return block.id | ||
if (this.failoverProviders) { | ||
const currentHeight = await this.failoverProviders[0]?.getBlockHeight()! | ||
if (this.failoverProvider) { | ||
const currentHeight = await this.failoverProvider?.getBlockHeight()! | ||
await this.db.setBlockHeight(currentHeight) | ||
@@ -78,3 +80,3 @@ return currentHeight | ||
} else { | ||
const history = await getHistory(this.chaingraphHost, lockingBytecode, { after: 0, limit: 5 }) | ||
const history = await getHistory(this.chaingraphHost!, lockingBytecode, { node: this.network, limit: 5 }) | ||
return (await this.db.bulkPutRawTransaction(history, lockingBytecodeHex)).map(u => asUtxo(u)) | ||
@@ -87,12 +89,10 @@ } | ||
public async getRawTransaction(txid: string): Promise<string> { | ||
if (!this.failoverProviders) { | ||
if (!this.failoverProvider) { | ||
throw Error("No failover network providers specified. Cannot get tx from cache.") | ||
} else { | ||
// TODO replace with chaingraph raw transaction getter. | ||
for (const p of this.failoverProviders) { | ||
try { | ||
return await p.getRawTransaction(txid) | ||
} catch (e: any) { | ||
console.debug(e) | ||
} | ||
try { | ||
return await this.failoverProvider.getRawTransaction(txid) | ||
} catch (e: any) { | ||
console.debug(e) | ||
} | ||
@@ -105,13 +105,12 @@ throw Error("Failover Transaction (get) Network providers exhausted, bailing") | ||
public async sendRawTransaction(txHex: string): Promise<string> { | ||
if (!this.failoverProviders) { | ||
if (!this.failoverProvider) { | ||
throw Error("No failover network providers specified. Cannot send from cache.") | ||
} else { | ||
// replace with chaingraph send | ||
for (const p of this.failoverProviders) { | ||
try { | ||
return await p.sendRawTransaction(txHex) | ||
return await this.failoverProvider.sendRawTransaction(txHex) | ||
} catch (e: any) { | ||
console.debug(e) | ||
} | ||
} | ||
throw Error("Failover Broadcast Network Providers exhausted, bailing") | ||
@@ -132,3 +131,3 @@ } | ||
const result = await getChaingraphUnspentRecords( | ||
this.chaingraphHost, | ||
this.chaingraphHost!, | ||
param | ||
@@ -135,0 +134,0 @@ ) |
@@ -7,2 +7,3 @@ | ||
import { cashAddressToLockingBytecode } from "@bitauth/libauth"; | ||
const FLAG_TYPED_ARRAY = "FLAG_TYPED_ARRAY"; | ||
@@ -73,1 +74,10 @@ | ||
} | ||
// TODO move to @unspent/util | ||
export function deriveLockingBytecode(address: string): Uint8Array { | ||
const lock = cashAddressToLockingBytecode(address); | ||
if (typeof lock === "string") throw lock; | ||
return lock.bytecode; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
321026
62
2721
+ Addedaxios@1.4.0
+ Added@cashscript/utils@0.8.2(transitive)
+ Added@types/node@22.9.3(transitive)
+ Addedaxios@1.4.0(transitive)
+ Addedproxy-from-env@1.1.0(transitive)
+ Addedundici-types@6.19.8(transitive)
- Removed@unspent/phi@0.1.0
- Removed@cashscript/utils@0.8.0-next.2(transitive)
- Removed@tsconfig/node16-strictest-esm@1.0.3(transitive)
- Removed@types/node@22.10.0(transitive)
- Removed@unspent/phi@0.1.0(transitive)
- Removedantlr4ts@0.5.0-dev(transitive)
- Removedaxios@0.27.2(transitive)
- Removedbuffer-from@1.1.2(transitive)
- Removedcashc@0.8.0-next.2(transitive)
- Removedcommander@7.2.0(transitive)
- Removedsemver@7.6.3(transitive)
- Removedsource-map@0.6.1(transitive)
- Removedsource-map-support@0.5.21(transitive)
- Removedundici-types@6.20.0(transitive)