Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

External addresses #19

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions src/address/ExternalAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@
*/

import inspectSymbol from 'symbol.inspect';
import {BitString} from "../boc/BitString";

export class ExternalAddress {

static isAddress(src: any): src is ExternalAddress {
return src instanceof ExternalAddress;
}

readonly value: bigint;
readonly bits: number;
readonly bits: BitString;

constructor(value: bigint, bits: number) {
this.value = value;
constructor(bits: BitString) {
this.bits = bits;
}

toString() {
return `External<${this.bits}:${this.value}>`;
return `External<${this.bits}>`;
}

[inspectSymbol] = () => this.toString()
Expand Down
13 changes: 6 additions & 7 deletions src/boc/BitBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
* LICENSE file in the root directory of this source tree.
*/

import { Address } from "../address/Address";
import { ExternalAddress } from "../address/ExternalAddress";
import { bitsForNumber } from "../utils/bitsForNumber";
import { Maybe } from "../utils/maybe";
import { BitString } from "./BitString";
import {Address} from "../address/Address";
import {ExternalAddress} from "../address/ExternalAddress";
import {Maybe} from "../utils/maybe";
import {BitString} from "./BitString";

/**
* Class for building bit strings
Expand Down Expand Up @@ -293,8 +292,8 @@ export class BitBuilder {
// Is External Address
if (ExternalAddress.isAddress(address)) {
this.writeUint(1, 2); // External address
this.writeUint(address.bits, 9);
this.writeUint(address.value, address.bits);
this.writeUint(address.bits.length, 9);
this.writeBits(address.bits);
return;
}

Expand Down
21 changes: 14 additions & 7 deletions src/boc/BitReader.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
* LICENSE file in the root directory of this source tree.
*/

import { randomInt } from 'crypto';
import {randomInt} from 'crypto';
import Prando from 'prando';
import { ExternalAddress } from '../address/ExternalAddress';
import { testAddress } from '../utils/testAddress';
import { BitBuilder } from './BitBuilder';
import { BitReader } from './BitReader';
import {ExternalAddress} from '../address/ExternalAddress';
import {testAddress} from '../utils/testAddress';
import {BitBuilder} from './BitBuilder';
import {BitReader} from './BitReader';

describe('BitReader', () => {
it('should read uints from builder', () => {
Expand Down Expand Up @@ -165,9 +165,16 @@ describe('BitReader', () => {

it('should read external address from builder', () => {
for (let i = 0; i < 1000; i++) {
let a = randomInt(20) === 0 ? new ExternalAddress(BigInt(randomInt(10000000000)), 48) : null;
let b = new ExternalAddress(BigInt(randomInt(10000000000)), 48);
let builder = new BitBuilder();
builder.writeInt(randomInt(10000000000), 40)
let aBits = builder.build();
builder = new BitBuilder();
builder.writeInt(randomInt(10000000000), 48)
let bBits = builder.build();

let a = randomInt(20) === 0 ? new ExternalAddress(aBits) : null;
let b = new ExternalAddress(bBits);
builder = new BitBuilder();
builder.writeAddress(a);
builder.writeAddress(b);
let bits = builder.build();
Expand Down
14 changes: 7 additions & 7 deletions src/boc/BitReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
* LICENSE file in the root directory of this source tree.
*/

import { Address } from "../address/Address";
import { ExternalAddress } from "../address/ExternalAddress";
import { BitString } from "./BitString";
import {Address} from "../address/Address";
import {ExternalAddress} from "../address/ExternalAddress";
import {BitString} from "./BitString";

/**
* Class for reading bit strings
Expand Down Expand Up @@ -499,14 +499,14 @@ export class BitReader {
}

// Load length
let bits = Number(this._preloadUint(9, this._offset + 2));
let len = Number(this._preloadUint(9, this._offset + 2));

// Load address
let value = this._preloadUint(bits, this._offset + 11);
let bits = this._bits.substring(this._offset + 11, len);

// Update offset
this._offset += 11 + bits;
this._offset += 11 + len;

return new ExternalAddress(value, bits);
return new ExternalAddress(bits);
}
}
10 changes: 5 additions & 5 deletions src/utils/testAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
*/

import Prando from "prando";
import { Address } from "../address/Address";
import { ExternalAddress } from "../address/ExternalAddress";
import { bitsForNumber } from "./bitsForNumber";
import {Address} from "../address/Address";
import {ExternalAddress} from "../address/ExternalAddress";
import {BitString} from "../boc/BitString";

export function testAddress(workchain: number, seed: string) {
const random = new Prando(seed);
Expand All @@ -27,6 +27,6 @@ export function testExternalAddress(seed: string) {
for (let i = 0; i < hash.length; i++) {
hash[i] = random.nextInt(0, 255);
}
let v = BigInt('0x' + hash.toString('hex'));
return new ExternalAddress(v, bitsForNumber(v, 'uint'));
let v = new BitString(hash, 0, hash.length * 8);
return new ExternalAddress(v);
}
Loading