foundry-primitives
Advanced tools
Comparing version 0.1.1 to 0.2.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const buffer_1 = require("buffer"); | ||
const _ = require("lodash"); | ||
const utility_1 = require("../utility"); | ||
const H160_1 = require("../value/H160"); | ||
const bech32_1 = require("./bech32"); | ||
/** | ||
* Substitutes for asset owner data which consists of network id, | ||
* lockScriptHash, parameters. The network id is represented with prefix | ||
* "cca"(mainnet) or "tca"(testnet). Currently version 1 exists only. | ||
* | ||
* Refer to the spec for the details about AssetAddress. | ||
* https://github.com/CodeChain-io/codechain/blob/master/spec/CodeChain-Address.md | ||
*/ | ||
class AssetAddress { | ||
constructor(type, payload, address) { | ||
this.type = type; | ||
if (H160_1.H160.check(payload)) { | ||
this.payload = H160_1.H160.ensure(payload); | ||
} | ||
else { | ||
const { m, n, pubkeys } = payload; | ||
this.payload = { | ||
m, | ||
n, | ||
pubkeys: pubkeys.map(p => H160_1.H160.ensure(p)) | ||
}; | ||
} | ||
this.value = address; | ||
} | ||
static fromTypeAndPayload(type, payload, options) { | ||
const { networkId, version = 1 } = options; | ||
if (version !== 1) { | ||
throw Error(`Unsupported version for asset address: ${version}`); | ||
} | ||
if (type < 0x00 || type > 0x03) { | ||
throw Error(`Unsupported type for asset address: ${type}`); | ||
} | ||
const words = bech32_1.toWords(buffer_1.Buffer.from([version, type, ...encodePayload(payload)])); | ||
const address = bech32_1.encode(networkId + "a", words); | ||
return new AssetAddress(type, payload, address); | ||
} | ||
static fromString(address) { | ||
if (address.charAt(2) !== "a") { | ||
throw Error(`The prefix is unknown for asset address: ${address}`); | ||
} | ||
const { words } = bech32_1.decode(address, address.substr(0, 3)); | ||
const bytes = bech32_1.fromWords(words); | ||
const version = bytes[0]; | ||
if (version !== 1) { | ||
throw Error(`Unsupported version for asset address: ${version}`); | ||
} | ||
const type = bytes[1]; | ||
if (type < 0x00 || type > 0x03) { | ||
throw Error(`Unsupported type for asset address: ${type}`); | ||
} | ||
if (type < 0x03) { | ||
const payload = utility_1.toHex(buffer_1.Buffer.from(bytes.slice(2))); | ||
return new this(type, new H160_1.H160(payload), address); | ||
} | ||
else { | ||
const n = bytes[2]; | ||
const m = bytes[3]; | ||
const payload = buffer_1.Buffer.from(bytes.slice(4)); | ||
const pubkeys = _.chunk(payload, 20).map(chunk => H160_1.H160.ensure(utility_1.toHex(buffer_1.Buffer.from(chunk)))); | ||
return new this(type, { n, m, pubkeys }, address); | ||
} | ||
} | ||
static check(address) { | ||
return address instanceof AssetAddress | ||
? true | ||
: AssetAddress.checkString(address); | ||
} | ||
static ensure(address) { | ||
return address instanceof AssetAddress | ||
? address | ||
: AssetAddress.fromString(address); | ||
} | ||
static checkString(value) { | ||
// FIXME: verify checksum | ||
return /^.{2}a[qpzry9x8gf2tvdw0s3jn54khce6mua7l]{42}$/.test(value); | ||
} | ||
toString() { | ||
return this.value; | ||
} | ||
} | ||
exports.AssetAddress = AssetAddress; | ||
function encodePayload(payload) { | ||
if (H160_1.H160.check(payload)) { | ||
return buffer_1.Buffer.from(H160_1.H160.ensure(payload).value, "hex"); | ||
} | ||
else { | ||
const { m, n, pubkeys } = payload; | ||
return buffer_1.Buffer.from([ | ||
n, | ||
m, | ||
...[].concat(...pubkeys.map(key => [ | ||
...buffer_1.Buffer.from(H160_1.H160.ensure(key).value, "hex") | ||
])) | ||
]); | ||
} | ||
} | ||
// import * as _ from "lodash"; | ||
// import { H160 } from "../value/H160"; | ||
// export type PublicKeyHashValue = H160 | string; | ||
// export type PublicKeyHash = H160; | ||
// export interface MultisigValue { | ||
// n: number; | ||
// m: number; | ||
// pubkeys: PublicKeyHashValue[]; | ||
// } | ||
// export type PayloadValue = PublicKeyHashValue | MultisigValue; | ||
//# sourceMappingURL=AssetAddress.js.map |
import { H160 } from "../value/H160"; | ||
import { H512 } from "../value/H512"; | ||
import { H256Value } from "../value/H256"; | ||
export declare type PlatformAddressValue = PlatformAddress | string; | ||
@@ -13,3 +13,3 @@ /** | ||
export declare class PlatformAddress { | ||
static fromPublic(publicKey: H512 | string, options: { | ||
static fromPublic(publicKey: H256Value, options: { | ||
networkId: string; | ||
@@ -16,0 +16,0 @@ version?: number; |
@@ -8,3 +8,3 @@ "use strict"; | ||
const H160_1 = require("../value/H160"); | ||
const H512_1 = require("../value/H512"); | ||
const H256_1 = require("../value/H256"); | ||
const bech32_1 = require("./bech32"); | ||
@@ -25,6 +25,6 @@ /** | ||
static fromPublic(publicKey, options) { | ||
if (!H512_1.H512.check(publicKey)) { | ||
if (!H256_1.H256.check(publicKey)) { | ||
throw Error(`Invalid public key for creating PlatformAddress: ${publicKey}`); | ||
} | ||
return PlatformAddress.fromAccountId(getAccountIdFromPublic(H512_1.H512.ensure(publicKey).value), options); | ||
return PlatformAddress.fromAccountId(getAccountIdFromPublic(H256_1.H256.ensure(publicKey).value), options); | ||
} | ||
@@ -31,0 +31,0 @@ static fromAccountId(accountId, options) { |
@@ -1,3 +0,2 @@ | ||
export { AssetAddress, AssetAddressValue } from "./address/AssetAddress"; | ||
export { PlatformAddress, PlatformAddressValue } from "./address/PlatformAddress"; | ||
export { Address, AddressValue } from "./address/address"; | ||
export { H128, H128Value } from "./value/H128"; | ||
@@ -4,0 +3,0 @@ export { H160, H160Value } from "./value/H160"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var AssetAddress_1 = require("./address/AssetAddress"); | ||
exports.AssetAddress = AssetAddress_1.AssetAddress; | ||
var PlatformAddress_1 = require("./address/PlatformAddress"); | ||
exports.PlatformAddress = PlatformAddress_1.PlatformAddress; | ||
var address_1 = require("./address/address"); | ||
exports.Address = address_1.Address; | ||
var H128_1 = require("./value/H128"); | ||
@@ -8,0 +6,0 @@ exports.H128 = H128_1.H128; |
{ | ||
"name": "foundry-primitives", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "JavaScript classes for Foundry primitives", | ||
@@ -5,0 +5,0 @@ "engines": { |
@@ -44,4 +44,3 @@ # foundry-primitives-js | ||
- U64, U128, U256 | ||
- AssetAddres | ||
- PlatformAddress | ||
- Address | ||
@@ -48,0 +47,0 @@ ## API Documentation |
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
128601
59
2198
49