Skip to content
Merged

Vault #423

Show file tree
Hide file tree
Changes from all 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
3,627 changes: 3,028 additions & 599 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"types": "npm run types --if-present --workspaces"
},
"dependencies": {
"@endo/static-module-record": "^1.0.4",
"@endo/static-module-record": "1.1.2",
"esbuild": "^0.24.0"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion packages/TBC20/build/src/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export class Token extends Contract {
}
if (this.amount >= amount) {
this.amount -= amount;
return new Token(to, amount, this.name, this.symbol);
const ctor = this.constructor;
return new ctor(to, amount, this.name, this.symbol);
}
throw new Error('Insufficient funds');
}
Expand Down
6 changes: 3 additions & 3 deletions packages/TBC20/src/token.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@


type Constructor<T> = new (...args: any[]) => T

export class Token extends Contract {
amount: bigint
Expand All @@ -20,7 +19,8 @@ export class Token extends Contract {
if (this.amount >= amount) {
// Send partial amount in a new object
this.amount -= amount
return new Token(to, amount, this.name, this.symbol)
const ctor = this.constructor as Constructor<this>
return new ctor(to, amount, this.name, this.symbol)
}
throw new Error('Insufficient funds')
}
Expand Down
5 changes: 2 additions & 3 deletions packages/TBC20/test/token.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import { expect } from 'chai'
import { Computer } from '@bitcoin-computer/lib'
import dotenv from 'dotenv'
Expand Down Expand Up @@ -115,8 +114,8 @@ describe('Token', async () => {
expect(token2._id).eq(token2After._id)
expect(token2._rev).not.eq(token2After._rev)

expect(await sender.query({ ids: [token1._id] })).deep.eq([token1._rev])
expect(await sender.query({ ids: [token2._id] })).deep.eq([token2After._rev])
expect(await sender.latest(token1._id)).deep.eq(token1._rev)
expect(await sender.latest(token2._id)).deep.eq(token2After._rev)
})

it('Sender merges their two tokens', async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/TBC721/build/src/nft.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ export class NftHelper {
return objects.length;
}
async ownersOf(tokenId) {
const [rev] = await this.computer.query({ ids: [tokenId] });
const rev = await this.computer.latest(tokenId);
const obj = await this.computer.sync(rev);
return obj._owners;
}
async transfer(to, tokenId) {
const [rev] = await this.computer.query({ ids: [tokenId] });
const rev = await this.computer.latest(tokenId);
const obj = await this.computer.sync(rev);
await obj.transfer(to);
}
Expand Down
6 changes: 2 additions & 4 deletions packages/TBC721/src/nft.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


export class NFT extends Contract {
name: string
artist: string
Expand Down Expand Up @@ -60,13 +58,13 @@ export class NftHelper implements ITBC721 {
}

async ownersOf(tokenId: string): Promise<string[]> {
const [rev] = await this.computer.query({ ids: [tokenId] })
const rev = await this.computer.latest(tokenId)
const obj = await this.computer.sync(rev)
return obj._owners
}

async transfer(to: string, tokenId: string): Promise<void> {
const [rev] = await this.computer.query({ ids: [tokenId] })
const rev = await this.computer.latest(tokenId)
const obj = await this.computer.sync(rev)
await obj.transfer(to)
}
Expand Down
4 changes: 2 additions & 2 deletions packages/chat/src/components/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ const ChatInput = ({
publicKey: computer.getPublicKey(),
time: Date.now().toString(),
}
const latesRev = await computer.getLatestRev(chatId)
const latesRev = await computer.latest(chatId)
const chatObj = (await computer.sync(latesRev)) as ChatSc
await chatObj.post(JSON.stringify(messageData))
await sleep(2000)
Expand Down Expand Up @@ -289,7 +289,7 @@ export function Chat({ chatId }: { chatId: string }) {
const refreshChat = async () => {
try {
showLoader(true)
const latesRev = await computer.getLatestRev(id)
const latesRev = await computer.latest(id)
const synced = (await computer.sync(latesRev)) as ChatSc
setChatObj(synced)
const messagesData: messageI[] = []
Expand Down
2 changes: 1 addition & 1 deletion packages/chess-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"LEGAL.md"
],
"scripts": {
"build:prod": "tsc -b && NODE_OPTIONS=\"--max-old-space-size=4096\" vite build",
"build:contract": "tsc --project tsconfig.build.json && mv ./build-contract/chess-module.js ./src/contracts/chess.mjs && rm -rf build-contract",
"build:prod": "tsc -b && NODE_OPTIONS=\"--max-old-space-size=4096\" vite build",
"build:turbo": "turbo run build:prod",
"deploy": "npm run build:contract && node --loader ts-node/esm ./scripts/deploy.ts",
"dev": "vite",
Expand Down
46 changes: 9 additions & 37 deletions packages/chess-contracts/build/src/main.js

Large diffs are not rendered by default.

71 changes: 36 additions & 35 deletions packages/chess-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
"license": "MIT",
"author": "Clemens Ley <clemens@bitcoincomputer.io>",
"type": "module",
"main": "./build/src/main.js",
"module": "./build/src/main.js",
"exports": {
".": "./build/src/main.js"
},
"main": "./build/src/main.js",
"module": "./build/src/main.js",
"types": "./build/src/index.d.ts",
"files": [
"LICENSE.md",
"LEGAL.md"
],
"types": "./build/src/index.d.ts",
"scripts": {
"prebuild": "npm run lint",
"build": "tsc -p tsconfig.json && parcel build --target main",
Expand All @@ -27,37 +27,6 @@
"test:show": "npm run test 2>&1 | tee chess-contracts.log; if [ ${PIPESTATUS[0]} -ne 0 ]; then open chess-contracts.log; fi",
"types": "tsc --noEmit"
},
"alias": {
"buffer": "buffer/index.js",
"assert": "assert/build/assert.js",
"crypto": "crypto-browserify/index.js",
"process": "process/browser.js",
"events": "events/events.js",
"stream": "stream-browserify/index.js",
"string_decoder": "string_decoder/lib/string_decoder.js",
"querystring": "querystring-es3",
"punycode": "punycode/punycode.js",
"util": "util/util.js"
},
"targets": {
"main": {
"source": "./build/src/index.js",
"distDir": "./build/src",
"outputFormat": "esmodule",
"context": "browser",
"includeNodeModules": {
"buffer": true,
"stream": true,
"string_decoder": true,
"events": true,
"process": true,
"@bitcoin-computer/secp256k1": false,
"@bitcoin-computer/lib": false
},
"sourceMap": false,
"optimize": true
}
},
"dependencies": {
"@bitcoin-computer/lib": "^0.26.0-beta.0",
"@bitcoin-computer/secp256k1": "^0.26.0-beta.0",
Expand All @@ -70,6 +39,7 @@
"path": "^0.12.7",
"process": "^0.11.10",
"punycode": "^2.1.1",
"parcel": "^2.15.4",
"querystring-es3": "^0.2.1",
"stream-browserify": "^3.0.0",
"string_decoder": "^1.3.0",
Expand All @@ -90,5 +60,36 @@
"volta": {
"node": "16.13.0"
},
"gitHead": "af40117bcb9616e631910f68c3b617aef629f027"
"alias": {
"buffer": "buffer/index.js",
"assert": "assert/build/assert.js",
"crypto": "crypto-browserify/index.js",
"process": "process/browser.js",
"events": "events/events.js",
"stream": "stream-browserify/index.js",
"string_decoder": "string_decoder/lib/string_decoder.js",
"querystring": "querystring-es3",
"punycode": "punycode/punycode.js",
"util": "util/util.js"
},
"gitHead": "af40117bcb9616e631910f68c3b617aef629f027",
"targets": {
"main": {
"source": "./build/src/index.js",
"distDir": "./build/src",
"outputFormat": "esmodule",
"context": "browser",
"includeNodeModules": {
"buffer": true,
"stream": true,
"string_decoder": true,
"events": true,
"process": true,
"@bitcoin-computer/secp256k1": false,
"@bitcoin-computer/lib": false
},
"sourceMap": false,
"optimize": true
}
}
}
3 changes: 1 addition & 2 deletions packages/components/built/Auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ export type ComputerOptions = Partial<{
path: string;
url: string;
satPerByte: number;
dustRelayFee: number;
addressType: AddressType;
moduleStorageType: ModuleStorageType;
thresholdBytes: number;
cache: boolean;
mode: 'prod' | 'dev';
}>;
declare function isLoggedIn(): boolean;
declare function logout(): void;
Expand Down
4 changes: 2 additions & 2 deletions packages/components/built/Transaction.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions packages/components/built/Wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ const Balance = ({ computer, modSpecs, isOpen, }) => {
try {
showLoader(true);
const publicKey = computer.getPublicKey();
const dust = computer.db.wallet.getDustThreshold(false);
const allPayments = [];
const balances = await Promise.all(modSpecs.map(async (mod) => {
const paymentRevs = modSpecs ? await computer.query({ publicKey, mod }) : [];
const payments = (await Promise.all(paymentRevs.map((rev) => computer.sync(rev))));
allPayments.push(...payments); // Accumulate payments
allPayments.push(...payments);
return payments && payments.length
? payments.reduce((total, pay) => total + (pay._satoshis - BigInt(computer.getMinimumFees())), 0n)
? payments.reduce((total, pay) => total + (pay._satoshis - BigInt(dust)), 0n)
: 0n;
}));
const amountsInPayments = balances.reduce((acc, curr) => acc + BigInt(curr), 0n);
Expand Down
3 changes: 1 addition & 2 deletions packages/components/src/Auth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ export type ComputerOptions = Partial<{
path: string
url: string
satPerByte: number
dustRelayFee: number
addressType: AddressType
moduleStorageType: ModuleStorageType
thresholdBytes: number
cache: boolean
mode: 'prod' | 'dev'
}>

function isLoggedIn(): boolean {
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/Transaction.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useContext, useEffect, useState } from 'react'
import { Link, useLocation, useParams } from 'react-router-dom'
import reactStringReplace from 'react-string-replace'
import { Computer } from '@bitcoin-computer/lib'
import { Transaction as BCTransaction } from '@bitcoin-computer/lib'
import { Card } from './Card'
import { ComputerContext } from './ComputerContext'

Expand Down Expand Up @@ -38,7 +38,7 @@ function Component() {
const fetch = async () => {
setTxn(params.txn)
const [hex] = await computer.db.wallet.restClient.getRawTxs([params.txn as string])
const tx = Computer.txFromHex({ hex })
const tx = BCTransaction.fromHex(hex)
setTxnData(tx)

const { result } = await computer.rpcCall('getrawtransaction', `${params.txn} 2`)
Expand All @@ -54,7 +54,7 @@ function Component() {
} catch (err) {
if (err instanceof Error) {
setTransition('')

console.log('Error parsing transaction', err.message)
}
}
Expand Down
8 changes: 3 additions & 5 deletions packages/components/src/Wallet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,19 +79,17 @@ const Balance = ({
try {
showLoader(true)
const publicKey = computer.getPublicKey()
const dust = computer.db.wallet.getDustThreshold(false)
const allPayments: any[] = []
const balances: bigint[] = await Promise.all(
modSpecs.map(async (mod) => {
const paymentRevs = modSpecs ? await computer.query({ publicKey, mod }) : []
const payments = (await Promise.all(
paymentRevs.map((rev: string) => computer.sync(rev)),
)) as any[]
allPayments.push(...payments) // Accumulate payments
allPayments.push(...payments)
return payments && payments.length
? payments.reduce(
(total, pay) => total + (pay._satoshis - BigInt(computer.getMinimumFees())),
0n,
)
? payments.reduce((total, pay) => total + (pay._satoshis - BigInt(dust)), 0n)
: 0n
}),
)
Expand Down
2 changes: 1 addition & 1 deletion packages/docs/Lib/Computer/broadcast.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ If the broadcast is successful, it returns the transaction id. Otherwise, an err

:::code source="../../../lib/test/lib/computer/broadcast.test.ts" :::

<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/broadcast.test.ts" target=_blank>Sources</a>
<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/broadcast.test.ts" target=_blank>Source</a>
2 changes: 1 addition & 1 deletion packages/docs/Lib/Computer/constructor.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ An instance of the `Computer` class

:::code source="../../../lib/test/lib/computer/constructor.test.ts" :::

<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/constructor.test.ts" target=_blank>Sources</a>
<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/constructor.test.ts" target=_blank>Source</a>
2 changes: 1 addition & 1 deletion packages/docs/Lib/Computer/decode.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ The function `decode` is the inverse of `encode` when the latter is called with

:::code source="../../../lib/test/lib/computer/decode.test.ts" :::

<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/decode.test.ts" target=_blank>Sources</a>
<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/decode.test.ts" target=_blank>Source</a>
2 changes: 1 addition & 1 deletion packages/docs/Lib/Computer/deploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,4 @@ import { c3 } from 'dc63fbf200595012b239d69936ac63e4155040042ef7d2e6dff4ca49dec3
export const s = c0 + c1 + c2 + c3
```

<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/deploy.test.ts" target=_blank>Sources</a>
<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/deploy.test.ts" target=_blank>Source</a>
26 changes: 13 additions & 13 deletions packages/docs/Lib/Computer/encode.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ An object with a specification to build a transaction according to the Bitcoin C

{.compact}

| Key | Description | Default |
| ----------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| exp | A JavaScript expression. | - |
| env | A Blockchain environment mapping variables names to revisions. | `{}` |
| mod | A string of the form `<id>:<num>` specifying the location of a module. | `undefined` |
| fund | Whether the transaction should be funded. | `true` |
| include | UTXOs to include when funding. | `[]` |
| exclude | UTXOs to exclude when funding. | `[]` |
| sign | Whether to sign the transaction. | `true` |
| sighashType | The signature hash type. | `SIGHASH_ALL` |
| inputIndex | If set to an number only the corresponding input is signed. If undefined all inputs are signed. | `undefined` |
| inputScript | If set to a string a custom input script can be provided. If undefined a signature script is generated. | `undefined` |
| Key | Description | Default |
| ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |
| exp | A JavaScript expression. | - |
| env | A Blockchain environment mapping variables names to revisions. | `{}` |
| mod | A string of the form `<id>:<num>` specifying the location of a module. | `undefined` |
| fund | Whether the transaction should be funded. | `true` |
| include | UTXOs to include when funding. | `[]` |
| exclude | UTXOs to exclude when funding. | `[]` |
| sign | Whether to sign the transaction. | `true` |
| sighashType | The signature hash type. | `SIGHASH_ALL` |
| inputIndex | If set to an number only the corresponding input is signed. If undefined all inputs are signed. | `undefined` |
| inputScript | If set to a string a custom input script can be provided. If undefined a signature script is generated. | `undefined` |
| mocks | A pair <name, object>. The object is an instance of a mocked class (A class that does not extends from Contract but has the keywords `_id`, `_root`, `_satoshis`,`_owners`) | `{}` |

### Return Value
Expand All @@ -76,4 +76,4 @@ The state update effected by a Bitcoin Computer transaction is completely predic

:::code source="../../../lib/test/lib/computer/encode.test.ts" :::

<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/encode.test.ts" target=_blank>Sources</a>
<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/encode.test.ts" target=_blank>Source</a>
2 changes: 1 addition & 1 deletion packages/docs/Lib/Computer/encodeCall.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,4 @@ See [`encode`](./encode.md).

:::code source="../../../lib/test/lib/computer/encode-call.test.ts" :::

<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/encode-call.test.ts" target=_blank>Sources</a>
<a href="https://github.com/bitcoin-computer/monorepo/blob/main/packages/lib/test/lib/computer/encode-call.test.ts" target=_blank>Source</a>
Loading