Skip to content

Commit

Permalink
fix(utils): add writeInteraction types and update base64url logic
Browse files Browse the repository at this point in the history
  • Loading branch information
dtfiedler committed Apr 25, 2024
1 parent 46b7acc commit 4f5476b
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 35 deletions.
23 changes: 15 additions & 8 deletions src/common/ar-io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,11 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
qty: number;
denomination: DENOMINATIONS;
}): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
return this.contract.writeInteraction<{
target: WalletAddress;
qty: number;
denomination: DENOMINATIONS;
}>({
functionName: AR_IO_CONTRACT_FUNCTIONS.TRANSFER,
inputs: {
target,
Expand Down Expand Up @@ -632,7 +636,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
async joinNetwork(
params: JoinNetworkParams,
): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
return this.contract.writeInteraction<JoinNetworkParams>({
functionName: AR_IO_CONTRACT_FUNCTIONS.JOIN_NETWORK,
inputs: params,
signer: this.signer,
Expand All @@ -651,7 +655,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
async updateGatewaySettings(
params: UpdateGatewaySettingsParams,
): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
return this.contract.writeInteraction<UpdateGatewaySettingsParams>({
functionName: AR_IO_CONTRACT_FUNCTIONS.UPDATE_GATEWAY_SETTINGS,
inputs: params,
signer: this.signer,
Expand All @@ -672,7 +676,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
target: string;
qty: number;
}): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
return this.contract.writeInteraction<{ target: string; qty: number }>({
functionName: AR_IO_CONTRACT_FUNCTIONS.DELEGATE_STAKE,
inputs: params,
signer: this.signer,
Expand All @@ -693,7 +697,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
target: string;
qty: number;
}): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
return this.contract.writeInteraction<{ target: string; qty: number }>({
functionName: AR_IO_CONTRACT_FUNCTIONS.DECREASE_DELEGATE_STAKE,
inputs: params,
signer: this.signer,
Expand All @@ -712,7 +716,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
async increaseOperatorStake(params: {
qty: number;
}): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
return this.contract.writeInteraction<{ qty: number }>({
functionName: AR_IO_CONTRACT_FUNCTIONS.INCREASE_OPERATOR_STAKE,
inputs: params,
signer: this.signer,
Expand All @@ -731,7 +735,7 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
async decreaseOperatorStake(params: {
qty: number;
}): Promise<WriteInteractionResult> {
const res = this.contract.writeInteraction({
const res = this.contract.writeInteraction<{ qty: number }>({
functionName: AR_IO_CONTRACT_FUNCTIONS.DECREASE_OPERATOR_STAKE,
inputs: params,
signer: this.signer,
Expand All @@ -756,7 +760,10 @@ export class ArIOWritable extends ArIOReadable implements ArIOWriteContract {
reportTxId: TransactionId;
failedGateways: WalletAddress[];
}): Promise<WriteInteractionResult> {
return this.contract.writeInteraction({
return this.contract.writeInteraction<{
observerReportTxId: TransactionId;
failedGateways: WalletAddress[];
}>({
functionName: AR_IO_CONTRACT_FUNCTIONS.SAVE_OBSERVATIONS,
inputs: {
observerReportTxId: params.reportTxId,
Expand Down
2 changes: 1 addition & 1 deletion src/common/arweave.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import Arweave from 'arweave';

export const defaultArweave = Arweave.init({
host: 'arweave.net',
host: 'ar-io.dev',
port: 443,
protocol: 'https',
});
32 changes: 22 additions & 10 deletions src/common/contracts/warp-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ import { FailedRequestError, WriteInteractionError } from '../error.js';
import { DefaultLogger } from '../logger.js';
import { defaultWarp } from '../warp.js';

LoggerFactory.INST.setOptions({
logLevel: 'fatal',
});
LoggerFactory.INST.logLevel('debug');

export class WarpContract<T>
implements BaseContract<T>, ReadContract, WriteContract
Expand Down Expand Up @@ -89,12 +87,14 @@ export class WarpContract<T>
}
const warpSigner = new Signature(this.warp, {
signer: async (tx: Transaction) => {
const publicKey = await signer.publicKey;
const dataToSign = await tx.getSignatureData();
const signatureBuffer = Buffer.from(await signer.sign(dataToSign));
const signatureUint8Array = await signer.sign(dataToSign);
const signatureBuffer = Buffer.from(signatureUint8Array);
const id = sha256B64Url(signatureBuffer);
tx.setSignature({
id: id,
owner: toB64Url(signer.publicKey),
owner: toB64Url(publicKey),
signature: toB64Url(signatureBuffer),
});
},
Expand Down Expand Up @@ -204,15 +204,27 @@ export class WarpContract<T>
);
}

const writeResult = await this.contract.writeInteraction<Input>({
function: functionName,
...inputs,
});
const writeResult = await this.contract.writeInteraction<Input>(
{
function: functionName,
...inputs,
},
{
disableBundling: true,
},
);

if (!writeResult?.interactionTx) {
throw new Error(`Failed to write contract interaction ${functionName}`);
throw new Error(
`Failed to write contract interaction: ${functionName}`,
);
}

this.logger.debug('Successfully wrote contract interaction', {
contractTxId: this.contractTxId,
interactionTxId: writeResult.originalTxId,
});

return writeResult.interactionTx;
} catch (error) {
this.logger.error(
Expand Down
12 changes: 2 additions & 10 deletions src/utils/base64.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,14 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { bufferTob64Url } from 'arweave/node/lib/utils.js';
import { createHash } from 'crypto';

export function fromB64Url(input: string): Buffer {
const paddingLength = input.length % 4 === 0 ? 0 : 4 - (input.length % 4);

const base64 = input
.replace(/-/g, '+')
.replace(/_/g, '/')
.concat('='.repeat(paddingLength));

return Buffer.from(base64, 'base64');
return Buffer.from(input, 'base64url');
}

export function toB64Url(buffer: Buffer): string {
return bufferTob64Url(buffer);
return buffer.toString('base64url');
}

export function sha256B64Url(input: Buffer): string {
Expand Down
17 changes: 11 additions & 6 deletions tests/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import Arweave from 'arweave';
import { JWKInterface } from 'arweave/node/lib/wallet';
import * as fs from 'fs';
import path from 'path';
import { ContractDeploy, Warp } from 'warp-contracts';
import { ContractDeploy, SourceType, Warp } from 'warp-contracts';

import { WeightedObserver } from '../src/contract-state';

Expand All @@ -29,11 +29,6 @@ export async function deployANTContract({
),
);
return await warp.deploy({
evaluationManifest: {
evaluationOptions: {
useKVStorage: true,
},
},
wallet: jwk,
src: src,
initState: JSON.stringify({
Expand All @@ -42,6 +37,11 @@ export async function deployANTContract({
controllers: [address],
balances: { [address]: 1000000 },
}),
evaluationManifest: {
evaluationOptions: {
sourceType: SourceType.ARWEAVE,
},
},
});
}

Expand Down Expand Up @@ -93,6 +93,11 @@ export async function deployArIOContract({
0: updatedPrescribedObservers,
},
}),
evaluationManifest: {
evaluationOptions: {
sourceType: SourceType.ARWEAVE,
},
},
});
}

Expand Down

0 comments on commit 4f5476b

Please sign in to comment.